在WPF中绑定类的属性至Label控件实现自动更新显示

最近的项目需要更新自定义控件的属性值至控件的界面中一个Label里显示,查阅了各种实现方法,最后自己的实现如下。

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显示更新触发条件,可自行搜索。


展开阅读全文

没有更多推荐了,返回首页