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中会及时显示

  • 相关阅读:
    poj 1417 True Liars(并查集+背包dp)
    CodeForces 760 C. Pavel and barbecue(dfs+思维)
    poj 2912 Rochambeau(枚举+带权并查集)
    lightoj 1245 Harmonic Number (II)(简单数论)
    thinkphp __PUBLIC__的定义 __ROOT__等常量的定义
    HTML5 画布参考
    HTML5 DTD
    HTML5 音频视频
    HTML5 事件
    HTML5 标准属性 NEW:HTML 5 中新的标准属性。 注释:HTML 4.01 不再支持 accesskey 属性:
  • 原文地址:https://www.cnblogs.com/yaosj/p/11233538.html
Copyright © 2011-2022 走看看