WPF Binding相关的一些常见方式总结(一)

来源:互联网 发布:java继承笔记 编辑:程序博客网 时间:2024/05/16 08:04

前端:

        <StackPanel>            <Button Name="btnTest" Content="测试" Height="100" Click="btnTest_Click"/>            <TextBlock Name="tblkText" Text="init" TextAlignment="Center" Height="100"/>        </StackPanel>

Model:

    public class Student : INotifyPropertyChanged    {        private string stuName;        public string StuName        {            get { return stuName; }            set             {                stuName = value;                if (PropertyChanged != null)                {                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("StuName"));                }            }        }        public event PropertyChangedEventHandler PropertyChanged;    }

ViewModel:

    public partial class MainWindow : Window    {        private Student myStu=new Student();        public MainWindow()        {            InitializeComponent();            Binding bd = new Binding();            bd.Source = myStu;            bd.Path = new PropertyPath("StuName");            BindingOperations.SetBinding(tblkText, TextBlock.TextProperty, bd);        }        private void btnTest_Click(object sender, RoutedEventArgs e)        {            myStu.StuName += "ab";        }    }


0 0