C# Wpf集合双向绑定

摘要:
Xaml指定TwoWay双向绑定<Mode=TwoWay}“Margin=”5“/><后台代码创建ObservableCollection<ObservableCollection<Students>infos=newObservableCollection<publicBind7(){InitializeComponent();

说明:

msdn中   ObservableCollection<T> 类    表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

在许多情况下,所使用的数据是对象的集合。 例如,数据绑定中的一个常见方案是使用 ItemsControl(如 ListBoxListView 或 TreeView)来显示记录的集合。

可以枚举实现 IEnumerable 接口的任何集合。 但是,若要设置动态绑定,以便集合中的插入或删除操作可以自动更新 UI,则该集合必须实现 INotifyCollectionChanged 接口。 此接口公开 CollectionChanged 事件,只要基础集合发生更改,都应该引发该事件。

WPF 提供 ObservableCollection<T> 类,它是实现 INotifyCollectionChanged 接口的数据集合的内置实现。

还有许多情况,我们所使用的数据只是单纯的字段或者属性,此时我们需要为这些字段或属性实现INotifyPropertyChanged接口,实现了该接口,只要字段或属性的发生了改变,就会提供通知机制。

实例:

Xaml指定TwoWay双向绑定

复制代码
<Grid>
    <StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427">
        <TextBlock Height="23" Name="textBlock1" Text="学员编号:" />
        <TextBox Height="23" Name="txtStudentId" Width="301" HorizontalAlignment="Left"/>
        <TextBlock Height="23" Name="textBlock2" Text="学员列表:" />
        <ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Name="stackPanel2" Orientation="Horizontal">
                        <TextBlock  Text="{Binding Id,Mode=TwoWay}" Margin="5" Background="Beige"/>
                        <TextBlock Text="{Binding Name,Mode=TwoWay}" Margin="5"/>
                        <TextBlock  Text="{Binding Age,Mode=TwoWay}" Margin="5"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Button" Height="23" Name="button1" Width="75" HorizontalAlignment="Left" Click="button1_Click" />
    </StackPanel>
</Grid>
复制代码

后台代码创建ObservableCollection<T>实例:

复制代码
ObservableCollection<Students> infos = new ObservableCollection<Students>() {
    new Students(){ Id=1, Age=11, Name="Tom"},
    new Students(){ Id=2, Age=12, Name="Darren"},
    new Students(){ Id=3, Age=13, Name="Jacky"},
    new Students(){ Id=4, Age=14, Name="Andy"}
    };
public Bind7()
{
    InitializeComponent();

    this.lbStudent.ItemsSource = infos;
    this.txtStudentId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = lbStudent });
}
private void button1_Click(object sender, RoutedEventArgs e)
{
    infos[1] = new Students() { Id = 4, Age = 14, Name = "这是一个集合改变" };
    infos[2].Name = "这是一个属性改变";
}
public class Students
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
复制代码

显示结果:

说明,ObservableCollection<T>只针对列表数据项修改进行通知,对于列表项属性修改不进行通知

C# Wpf集合双向绑定第5张

如果想对于对象属性修改也通知到UI,需要类实现INotifyPropertyChanged接口

复制代码
public class Students : INotifyPropertyChanged
{
    string _name;
    public int Id { get; set; }
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }
    public int Age { get; set; }
    protected internal virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
复制代码

C# Wpf集合双向绑定第8张

免责声明:文章转载自《C# Wpf集合双向绑定》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用Unity开发Android的几种调试方法推荐一款JSON字符串查看器下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vue实现简单日历

1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>日历</title> 7 <style> 8 * {...

HTML5 创建热点图

通过HTML5 canvas画布创建简单的热点图,当鼠标划过时产生热点,停留时间越长,热点亮度越高。 下面是HTML部分: <!DOCTYPE html> <html> <head></head> <style type="text/css"> #heatmap { b...

避免浏览器自动填充表单的解决方式(转载请注明出处)

  以前在做项目的时候遇到过这个问题,当时年少太轻狂,没有想过是为什么会发生这样的问题,只觉得作为一个用户,每次在登录网站的时候很有用,很便捷,甚至觉得这个自动填充功能,嗯, 真棒!但是,这次又遇到了这个问题,我不禁陷入了沉思。。。为什么会有自动填充呢?为什么会变成黄色框框呢?作为一个开发者,在我不需要它自动填充的时候,它真的,很碍事!   于是乎~各种谷...

c++ 数组元素拷贝到容器(copy)

#include <iostream> // cout #include <algorithm> // copy #include <vector> // vector using namespace std; int main () { int myints[]={10,20...

vue的学习总结---事件处理

v-on的理解 监听DOM元素的事件,并在触发时执行一些js代码 <template> <div> <!-- v-on监听DOM事件,并在触发时做一些js的操作,如下代码可以将js操作直接放在事件中 --> <button v-on:click="num++">点击按钮+1</but...

vue组件之间的传值方式

vue是一个轻量级的渐进式框架,对于它的一些特性和优点在此不做阐述,本篇文章主要来学习一下Vue子父组件通信的问题。 gitHub地址:整个案例的Demo 首先先定义一下,相对本案例来说App.vue是父组件, Child.vue是子组件。 一、父组件向子组件传值  1、创建子组件,在src/components文件夹下新建一个Child.vue 2、C...