zoukankan      html  css  js  c++  java
  • WPF简单数据绑定

    XAML:

            <!--#region 数据绑定控件-->
            <DataGrid x:Name="dataGrid" Grid.Column="2" Grid.ColumnSpan="4" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="编号" Width="*" Binding="{Binding Id}"></DataGridTextColumn>
                    <DataGridTextColumn Header="名称" Width="*" Binding="{Binding Name}"></DataGridTextColumn>
                    <DataGridTextColumn Header="时间" Width="*" Binding="{Binding CreateTime}"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
            <!--#endregion-->

    后台隐藏代码:

     public partial class AllControl : Window
        {
    
            List<Users> usl = new List<Users>();
    
            public AllControl()
            {
                usl.Add(new Users
                {
                    Id = 1,
                    Name = "maoyang",
                    CreateTime = DateTime.Now,
                });
                usl.Add(new Users
                {
                    Id = 2,
                    Name = "zhangfan",
                    CreateTime = Convert.ToDateTime("2018/7/30 8:30:00"),
                });
                usl.Add(new Users
                {
                    Id = 3,
                    Name = "kejing",
                    CreateTime = Convert.ToDateTime("2018/7/30 9:00:00"),
                });
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                this.dataGrid.ItemsSource = usl;
            }
    
        }
    
        public class Users
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public DateTime CreateTime { get; set; }
        }

    此时如果修改字段,UI界面数据不能实时更新,需要以下修改:

    1:把类继承INotifyPropertyChanged这个接口,并实现接口,然后添加定义OnPropertyChanged方法

     public class Users : INotifyPropertyChanged
        {
            private int id;
            private string name;
            private DateTime createTime;
    
            public int Id
            {
                get
                {
                    return id;
                }
    
                set
                {
                    id = value;
                    OnPropertyChanged("Id");
                }
            }
    
            public string Name
            {
                get
                {
                    return name;
                }
    
                set
                {
                    name = value;
                    OnPropertyChanged("Name");
                }
            }
    
            public DateTime CreateTime
            {
                get
                {
                    return createTime;
                }
    
                set
                {
                    createTime = value;
                    OnPropertyChanged("CreateTime");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

    写一个修改列表中的字段事件:

         /// <summary>
            /// 点击更改按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Update_Click(object sender, RoutedEventArgs e)
            {
                #region 
                Users usi = usl.Where(x => x.Id == 2).FirstOrDefault();
                usi.Id = 2;
                usi.Name = "老虎";
                usi.CreateTime = DateTime.Now;
                #endregion
            }

    XAML修改按钮:

     <!--#region 点击更改按钮-->
            <Button x:Name="Update" Width="100" Height="30" Background="AntiqueWhite" VerticalAlignment="Center" HorizontalAlignment="Center" Content="点击更改按钮" Click="Update_Click"></Button>
            <!--#endregion-->

    此时点击这个修改的按钮,UI界面数据即可实时更新

  • 相关阅读:
    201301 JAVA题目0-1级, 华为笔试题
    病毒传播, 美团笔试题
    OC基础 字符串
    IOS OC语言基础(一)类的创建
    pyautogui 鼠标精灵模拟登陆
    ajax hook注入
    aiohttp异步请求
    IOS自学教程大全
    提高python工具包
    Kafka与Flume区别
  • 原文地址:https://www.cnblogs.com/SeNaiTes/p/9390692.html
Copyright © 2011-2022 走看看