在WPF中绑定类的属性至Label控件实现自动更新显示
2012-10-16 15:56
519 查看
最近的项目需要更新自定义控件的属性值至控件的界面中一个Label里显示,查阅了各种实现方法,最后自己的实现如下。
1.class代码
2.xaml代码
3.实现效果
更新属性前
更新属性后
4.总结
a.核心是在用户控件中定义DependencyProperty,然后将其绑定至Label的Content。
b.也可以通过实现INotifyPropertyChanged类并将属性值改变事件作为Label的Content显示更新触发条件,可自行搜索。
1.class代码
#region used namespace using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; #endregion namespace CPAToolkit { /// <summary> /// MyControl.xaml 的交互逻辑 /// </summary> public partial class MyControl : UserControl { public MyControl() { InitializeComponent(); FileType = "None"; FileName = "None"; } public String FileType { get { return (string)GetValue(propFileType); } set { SetValue(propFileType, value); } } public static readonly DependencyProperty propFileType = DependencyProperty.Register("FileType", typeof(string), typeof(MyControl), new UIPropertyMetadata()); public String FileName { get { return (string)GetValue(propFileName); } set { SetValue(propFileName, value); } } public static readonly DependencyProperty propFileName = DependencyProperty.Register("FileName", typeof(string), typeof(MyControl), new UIPropertyMetadata()); private void btnChangePropertyValue_Click(object sender, RoutedEventArgs e) { //update property value FileName = "a.xml"; FileType="xml"; } } }
2.xaml代码
<UserControl x:Class="CPAToolkit.MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Root" d:DesignHeight="300" d:DesignWidth="300" mc:Ignorable="d"> <Grid> <Label Name="lbFileType" Height="28" Margin="12,14,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="FileType:" /> <Label Name="lbFileName" Height="28" Margin="12,62,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="FileName:" /> <Label Name="lbBindingFileName" Width="196" Height="28" Margin="92,62,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="{Binding ElementName=Root, Path=FileType}" /> <Label Name="lbBindingFileType" Width="196" Height="28" Margin="92,14,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="{Binding ElementName=Root, Path=FileName}" /> <Button Name="btnChangePropertyValue" Width="75" Height="23" Margin="116,133,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Click="btnChangePropertyValue_Click" Content="Change" /> </Grid> </UserControl>
3.实现效果
更新属性前
更新属性后
4.总结
a.核心是在用户控件中定义DependencyProperty,然后将其绑定至Label的Content。
b.也可以通过实现INotifyPropertyChanged类并将属性值改变事件作为Label的Content显示更新触发条件,可自行搜索。
相关文章推荐
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- Mvvm KeyDown的实现以及TextBox绑定的属性不更新问题的解决
- 模拟实现WPF的依赖属性及绑定通知机制(3)--依赖对象
- 模拟实现WPF的依赖属性及绑定通知机制(4)--模拟实现绑定连动机制 .
- 模拟实现WPF的依赖属性及绑定通知机制(1)--依赖属性
- wpf中利用多重绑定实现表中数据越界自动报警
- android开发进行自动更新时怎么下载软件到手机内存,显示实现下载到SD卡了
- 模拟实现WPF的依赖属性及绑定通知机制(1)--依赖属性
- 模拟实现WPF的依赖属性及绑定通知机制(2)--依赖对象的准备
- android通过service实现更新并显示下载进度条,最后自动安装实例
- 模拟实现WPF的依赖属性及绑定通知机制(2)--依赖对象的准备
- WPF -- 绑定显示多个属性
- 模拟实现WPF的依赖属性及绑定通知机制(3)--依赖对象
- 模拟实现WPF的依赖属性及绑定通知机制(3)--依赖对象
- 模拟实现WPF的依赖属性及绑定通知机制(4)--模拟实现绑定连动机制
- 如何实现xslt文件中节点属性的自动更新
- 模拟实现WPF的依赖属性及绑定通知机制(4)--模拟实现绑定连动机制
- DEVExpress XtraReport报表制作过程中绑定数据源的XRControl对象在值为0时不显示功能,通过代码自动实现
- 属性绑定:属性被绑定到一个表达式,这个属性就会被设置为自动更新,但是,分配静态值会移除绑定
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo