WPF 之 DataGrid 入门实践二:数据保存

▪ 前言

《WPF 之 DataGrid 入门实践一》 中,我们已经初识 DataGrid 控件,也看到了 DataGrid 的样子。接下我们就要学习 DataGrid 最为重要的功能,就是保存修改后的 DataGrid 数据。

▪ 基础代码

《WPF 之 DataGrid 入门实践一》 中,我们已经构建显示在 DataGrid 表中的数据,为了适应本章的内容,我们稍微重构了 实践一 中的基础代码。

重构前台 XAML 代码:

# xaml
<DataGrid x:Name="uiStudentTable" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="姓名" Binding="{Binding Name, Mode=TwoWay}" Width="80" IsReadOnly="True"/>
        <DataGridTextColumn Header="年龄" Binding="{Binding Age, Mode=TwoWay}" Width="80"/>
        <DataGridTextColumn Header="生日" Binding="{Binding Birthday, Mode=TwoWay}"/>
    </DataGrid.Columns>
</DataGrid>

增加了 Mode=TwoWay 属性值,表示数据绑定的模式是双向绑定,说白就是 C# 中数据变量值改变时那么UI控件上的值也自动改变,反之亦然。当然也可以不写,因为默认就是 TwoWay 方式。

注意:并不是设置了 Mode=TwoWay 就马上实现双向绑定,这只是其中的一个配置项,还需要在 C# 中实现 INotifyPropertyChanged。

重构后台 C# 代码:

public partial class MainWindow : Window
{
    // 成员变量
    private List<Student> listStudent = new List<Student>();

    /// <summary>
    /// 构造函数
    /// </summary>
    public DataSettingWindow()
    {
        InitializeComponent();
        
        listStudent.Add(new Student("李毅", "28", "19900402"));
        listStudent.Add(new Student("张通", "28", "19900620"));
        listStudent.Add(new Student("焕荣", "38", "19800620"));
            
        uiStudentTable.ItemsSource = listStudent;
    }

    /// <summary>
    /// 学生类
    /// </summary>
    protected class Student  
    {  
        // 成员变量
        public string Age{ get; set; }
        public string Name{ get; set; }  
        public string Birthday{ get; set; }
    
        //构造函数
        public Student( string name, string age, string birthday )  
        {
            this.Age = age;  
            this.Name = name;  
            this.Birthday = birthday;  
        }  
    }
}

上述代码中新增了 this.listStudent 成员变量,方便后期的数据保存和查看。

▪ 数据保存案例一:基于 INotifyPropertyChanged

在 .NET 平台上,数据绑定是一项令人十分愉快的技术。利用数据绑定能减少很多的逻辑代码,简化控制逻辑。

通常,可以将某个对象的一个属性绑定到一个可视化的控件上,当属性值改变时,控件上的显示数据也随之发生变化。要实现这一功能,只需要为自定义对象实现 INotifyPropertyChanged 接口即可。此接口中定义了 PropertyChanged 事件,我们只需在属性值改变时触发该事件即可.

INotifyPropertyChanged 接口是 WPF/Silverlight 开发中非常重要的接口, 它构成了 ViewModel 的基础。这里我们直接以 INotifyPropertyChanged 来保存 DataGrid 表的数据。

重构 Student 学生类,代码如下:


/// <summary>
/// 学生类
/// </summary>
protected class Student : INotifyPropertyChanged
{  
    // 接口事件
    public event PropertyChangedEventHandler PropertyChanged;

    // 成员变量
    public string Age{
        get{
            return this.age;
        }
        set{
            if( this.age == value ) return;
            this.age = value; this.Notify("Age");
        }
    }
    private string age;

    // 成员变量
    public string Name{
        get{
            return this.name;
        }
        set{
            if( this.name == value ) return;
            this.name = value; this.Notify("Name");
        }
    }
    private string name;

    // 成员变量
    public string Birthday{
        get{
            return this.birthday;
        }
        set{
            if( this.birthday == value ) return;
            this.birthday = value; this.Notify("Birthday");
        }
    }
    private string birthday;

    // 构造函数
    public Student( string name, string age, string birthday )  
    {
        this.Age = age;  
        this.Name = name;  
        this.Birthday = birthday;  
    }

    // 属性通知
    public void Notify( string propertyName )
    {
        if( this.PropertyChanged != null ) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

很简单,只要稍微改造下 Student 就完工了。运行程序,修改 DataGrid 表中值,对应 C# 中的 this.listStudent 成员变量值也能自动改变。

怎么才能看到 this.listStudent 成员变量的值改变了呢?做一个按钮,然后再按钮的点击事件里设置一个断点,每次想看的时候点击按钮就进入断点,你可以查看断点的变量。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值