当前位置: 首页 > news >正文

【WPF】 自定义控件的自定义属性

文章目录

  • 前言
  • 一、自定义控件部分
  • 二、在页面中使用
  • 总结

前言

在一个页面,重复用到同一个自定义控件时,该如何对控件分别进行数据绑定呢?这时候可以赋予控件一个自定义的属性,来完成此操作。


一、自定义控件部分

为自定以控件设置 DataTag和SelectedI属性。

<UserControl x:Class="Module3DS.Components.DataList"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Module3DS.Components"mc:Ignorable="d" d:DesignHeight="250" d:DesignWidth="300"><GroupBox Grid.Column="0" Header="数据列表"><DataGrid x:Name="PrjDg" HeadersVisibility="Column" SelectedIndex="{Binding SelectedI}" ItemsSource="{Binding}" Tag="{Binding DataTag}"  ScrollViewer.VerticalScrollBarVisibility="Visible"   CanUserAddRows="False" AutoGenerateColumns="False" CanUserSortColumns="False" MouseDoubleClick="PrjDg_MouseDoubleClick"><DataGrid.Columns><DataGridTextColumn Binding="{Binding NUM}"  Header="一" Width="1*"  IsReadOnly="True"/><DataGridTextColumn Binding="{Binding STAKEKM}" Header="二" IsReadOnly="True" Width="2*"/></DataGrid.Columns></DataGrid></GroupBox>
</UserControl>

控件后端:

   public partial class DataList : UserControl{public DataList(){InitializeComponent();//BingdingFun("TagData", DataGrid.TagProperty);BingdingFun("SelectedI", DataGrid.SelectedIndexProperty);}private void BingdingFun(string name,DependencyProperty dp) {Binding bing = new Binding(name);bing.Source = this;bing.Mode = BindingMode.TwoWay;BindingOperations.SetBinding(PrjDg, dp, bing);}public static readonly DependencyProperty ISProperty = DependencyProperty.Register("TagData", typeof(Object), typeof(DataList));public static readonly DependencyProperty SIProperty = DependencyProperty.Register("SelectedI", typeof(int), typeof(DataList));public Object DataTag{get { return (Object)GetValue(ISProperty); }set { SetValue(ISProperty, value); }}public int SelectedI { get { return (int)GetValue(SIProperty); }set { SetValue(SIProperty, value); }}
}      

二、在页面中使用

<Components:DataList Grid.Column="0" x:Name="Data3DList" DataTag="Data3D" />
<Components:DataList x:Name="IndexDataList" DataTag="DataIndex" />

如此一来,我们的控件就有了DataTag和SelectedI属性,如果你使用的MVVM,还可以做到如下这样

<Components:DataList x:Name="IndexDataList" DataTag="{Binding 属性}" SelectedI= "{Binding 属性}"/>

总结

以上就是如何为自定义控件添加自定义属性了,我们下次再见,拜拜。

相关文章:

  • git常用修改命令
  • DDD架构设计
  • 数据结构与算法学习导航
  • 【python】pysharm常用快捷键使用-(1)
  • Linux 常用命令总结
  • 个人博客系统后端 - 用户信息管理功能实现指南(上)
  • PTA:模拟EXCEL排序
  • XCZU7EG‑L1FFVC1156I 赛灵思XilinxFPGA ZynqUltraScale+ MPSoC EG
  • jdk node redis nginx mysql直接部署
  • 性能测试方案设计思路总结
  • ADVB发送器设计
  • api护照查验-GO国内护照查验接口-身份安全卫士
  • 操作教程|通过DataEase制作MaxKB系统数据大屏
  • 【前端】跟着maxkb学习流程图画法
  • 【含文档+PPT+源码】基于微信小程序的非遗文化黄梅戏宣传平台的设计与实现
  • 读书笔记 -- MySQL架构
  • 音视频学习 - ffmpeg 编译与调试
  • webgl入门实例-09索引缓冲区示例
  • 【华为】OSPF震荡引起CPU占用率高怎么解决?
  • SvelteKit 最新中文文档教程(21)—— 最佳实践之图片
  • 婚姻登记“全国通办”首日,上海虹口登记中心迎来“甜蜜高峰”
  • 习近平会见古巴国家主席迪亚斯-卡内尔
  • 多人称华为手机忽现拍照模糊疑存缺陷,售后回应:主摄像头故障
  • 马上评|比余华与史铁生的友情更动人的是什么
  • 上海充电桩调研:须全盘考量、分步实现车网互动规模化
  • 宁合两大都市圈交汇之城含山:要想身体好,常往含山跑