zoukankan      html  css  js  c++  java
  • WPF绑定属性

    1.创建model类

    model类要继承接口INotifyPropertyChanged,用于通知客户端属性值已更改

        public class StudentModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private string name = "";
            public string Name
            {
                get => name; set { name = value; RaisePropertyChanged("Name"); }
            }
        }
    

      

    2.创建ViewModel类

        public class Window2ViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            private StudentModel studentModel = new StudentModel();
            public StudentModel StudentModel
            {
                get => studentModel; set { studentModel = value; RaisePropertyChanged("studentModel"); }
    
            }
        }
    

      

    3.View绑定属性

    (1)在Window2.xaml.cs中为DataContext赋值

        /// <summary>
        /// Window2.xaml 的交互逻辑
        /// </summary>
        public partial class Window2 : Window
        {
            public Window2()
            {
                InitializeComponent();
                this.DataContext = new Window2ViewModel();
            }
        }
    

    (2)在Window2.xaml中绑定数据

        <Grid>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="243,160,0,0" TextWrapping="Wrap" Text="{Binding StudentModel.Name,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
            <Label HorizontalAlignment="Left" Height="23" Margin="243,208,0,0" Content="{Binding StudentModel.Name}" VerticalAlignment="Top" Width="120"/>
        </Grid>
    

      

    4.效果

    在1中输入,2中会及时显示

  • 相关阅读:
    MySQL简单实现多字段模糊查询【转】
    PHP检测URL格式是否正确域名地址是否有效【转】
    php如何判断IP为有效IP地址【转】
    PHP isset() 函数使用【转】
    php生成唯一随机码【转】
    php判断一个值是否在数组中【转】
    Win10系统gpedit.msc文件找不到,如何解决【转】
    B
    【金色】种瓜得瓜,种豆得豆 Gym
    J
  • 原文地址:https://www.cnblogs.com/yaosj/p/11233538.html
Copyright © 2011-2022 走看看