闪电龟龟--笔记

万物寻其根,通其堵,便能解其困。
  博客园  :: 新随笔  :: 管理

WPF 数据绑定

Posted on 2022-01-27 12:41  闪电龟龟  阅读(270)  评论(0编辑  收藏  举报

学习者可前往笔记来源查看(https://blog.csdn.net/kenjianqi1647/article/details/90320569

1.wpf窗体内数据绑定

            <!--数据绑定Start-->
            <TextBlock x:Name="TBBloack" Text="{Binding Path=Value,ElementName=nslider}"></TextBlock>
            <Slider x:Name="nslider" Minimum="0" Maximum="1000" Margin="0,20,0,0"></Slider>
            <!--数据绑定End-->

等价于代码(2.代码-界面数据相互绑定(单向,用于数据展示))内绑定

this.TBBloack.SetBinding(TextBlock.TextProperty, new Binding("Value") { ElementName = "nslider" });

2.代码-界面数据相互绑定(单向,用于数据展示)

 

// 数据绑定集合
            List<StuDents_Model> stuDentsDatas = new List<StuDents_Model>()
            {
                new StuDents_Model(){ Id="1",Name="NameJR1",Age="18"},
                new StuDents_Model(){ Id="2",Name="NameJR2",Age="19"},
                new StuDents_Model(){ Id="3",Name="NameJR3",Age="20"},
                new StuDents_Model(){ Id="4",Name="NameJR4",Age="21"},
                new StuDents_Model(){ Id="5",Name="NameJR5",Age="22"}
            };
            this.LBItems.ItemsSource = stuDentsDatas;
            this.LBItems.DisplayMemberPath = "Name";  // 显示stuDentsDatas的Name
            Binding binding = new Binding("SelectedItem.Id") { Source = this.LBItems };  // 绑定选中项(SelectedItem)的ID
            this.TBBloackT.SetBinding(TextBlock.TextProperty, binding);

3.代码-界面数据相互绑定(双向绑定)

 

 

 3.1使用GalaSoft.MvvmLight进行绑定

<TextBlock x:Name="TBBloackF" Background="Yellow" Height="30" VerticalAlignment="Top" Margin="0,300,0,0" Text="{Binding Path=GetName}"></TextBlock>
            <Button x:Name="BtnName" Click="BtnName_Click" Width="100" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,330,0,0" Content="按钮"></Button>
//初始化
       stuDatas = new StuDatas_Model();
            Binding binding2 = new Binding();
            binding2.Source = stuDatas;
            binding2.Path = new PropertyPath("GetName");
            BindingOperations.SetBinding(this.TBBloackF, TextBlock.TextProperty, binding2);
private StuDatas_Model stuDatas;
        /// <summary>
        /// 数据绑定列表
        /// </summary>
        public class StuDatas_Model : ObservableObject
        {
            private string TName = "PrevName";

            /// <summary>
            ///
            /// </summary>
            public string GetName
            {
                get
                {
                    return TName;
                }
                set
                {
                    TName = value;
                    // 作用更新/通知界面值发生改变
                    RaisePropertyChanged(() => GetName);
                }
            }
        }
private void BtnName_Click(object sender, RoutedEventArgs e)
        {
            stuDatas.GetName += "NextName";
        }

 

3.2使用System.ComponentModel进行绑定

//初始化
       tempStuDatas = new TempStuDatas(); Binding binding1 = new Binding(); binding1.Source = tempStuDatas; binding1.Path = new PropertyPath("GetName"); BindingOperations.SetBinding(this.TBBloackTh, TextBlock.TextProperty, binding1);
private TempStuDatas tempStuDatas;
        public class TempStuDatas: INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            /// <summary>
            /// 
            /// </summary>
            private string TName = "PrevName";

            /// <summary>
            /// 
            /// </summary>
            public string GetName
            {
                get
                {
                    return TName;
                }
                set
                {
                    TName = value;
            // 作用更新/通知界面值发生改变
if (this.PropertyChanged != null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("GetName")); } } } }
private void BtnF_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            tempStuDatas.GetName += "NextName";

        }