首页 > 其他分享 >WPF 集合通知更改

WPF 集合通知更改

时间:2024-08-16 19:27:59浏览次数:6  
标签:set Name 更改 Students get 集合 new WPF public

集合通知更改,ObservableCollection。属性通知更改,适合单个属性,如果是多个属性的集合数据,使用ObservableCollection。

 

 public partial class ButtonWindow : Window
    {
        ObservableCollection<Students> infos;
        public ButtonWindow()
        {
            InitializeComponent();
            //this.DataContext = new ButtonViewModel();
            //cbEditingModel.ItemsSource = Enum.GetValues(typeof(InkCanvasEditingMode));
            infos = new ObservableCollection<Students>() {
            new Students(){ Id=1, Age=11, Name="Tom",B=false},
            new Students(){ Id=2, Age=12, Name="Darren",B=false},
            new Students(){ Id=3, Age=13, Name="Jacky",B=false},
            new Students(){ Id=4, Age=14, Name="Andy",B=false}
            };
            this.lbStudent.ItemsSource = infos;
            this.txtStudentId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Name")
            {
                Source = lbStudent
            }) ;
        }

         private void button1_Click(object sender, RoutedEventArgs e)
         {
            infos[1] = new Students() { Id = 4, Age = 14, Name = "这是一个集合改变",B=true };
            infos[2].Name = "这是一个属性改变";
            infos[3] = new Students() { Id = 666, Age = 66, Name = "666", B = true };
         }

    }
    public class Students
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public bool B { get; set; }
    }
    <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="Red"/>
                            <TextBlock Text="{Binding Name,Mode=TwoWay}" Margin="5"/>
                            <TextBlock  Text="{Binding Age,Mode=TwoWay}" Margin="5"/>
                            <TextBlock  Text="{Binding B,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>
 

第三行得Name值需要Students类实现INotifyPropertyChanged通知,界面就会变化了。 
public class Students : MyBindingBase
    {
        private string _name;
        public int Id { get; set; }
        public string Name
        {
            get { return _name; }
            set { _name = value;OnPropertyChanged(); }
        }
        public int Age { get; set; }
        public bool B { get; set; }
    }
public class MyBindingBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        //protected virtual void OnPropertyChanged(string propertyName)
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")//此处使用特性
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

结果如下:

 

来源:https://blog.csdn.net/u012563853/article/details/123429808

 

标签:set,Name,更改,Students,get,集合,new,WPF,public
From: https://www.cnblogs.com/ywtssydm/p/18363496

相关文章

  • C# WPF现代化开发:绑定、模板与动画进阶
    ......
  • WPF控件结构与Content理解
    WPF控件结构WPF中控件继承图我们平时所用的容器如Grid、StackPanel等都是继承Panel控件类型分为3组:内容控件、Items控件、TextBoxBase如何理解Content?凡是继承ContentControl的控件,定义内容为Content,除了TextBlock用text以外,大部分都是用Content设置显示类容。一个窗......
  • WPF 命令Command
    MVVM的目的是为了最大限度地降低了Xaml文件和CS文件的合度,分离界面和业务逻辑,所以我们要尽可能的在View后台不写代码。但是这个例子中,我们将更新ViewModel的代码写在了View里。我们能否把按钮的响应处理代码也不写在后台代码里呢?WPF引入Command(命令),通过为Button设置Command来......
  • WPF 绑定
    绑定就是Binding,是控件和数据之间交互的类。source={binding}和source={bindingRelativeSource={RelativeSourceself},Path=DataContext}效果相同。例如:直接绑定数据源前台xaml界面<Grid><StackPanelOrientation="Vertical"><TextBlock......
  • Clion控制台中文输出/报错信息乱码的最完美解决方案(无需更改注册表,beta版UTF-8)
    Clion控制台中文输出/报错信息乱码的最完美解决方案(无需更改注册表,beta版UTF-8)1.问题:clion控制台乱码2.错误解决方案:Ctrl+Shift+Alt+/,回车,打开注册表,取消勾选"run.processes.with.pty"(clion可能会卡死)3.正确方式:1.Ctrl+Alt+S打开设置2.找到编辑器Editor-文件编码Fil......
  • Java--集合
    目录集合框架类结构图List接口ArrayList(语法格式)LinkedListVectorStackSet接口Queue接口非阻塞队列阻塞队列Map接口集合中元素排序集合泛型集合遍历集合工具类集合框架类结构图List接口ArrayList(语法格式)方法描述add()将元素插入到指定位置的arraylist中a......
  • WPF Animation 动画变化值的监控
    WPF动画XXXAnimation即关键类继承制AnimationBase的动画类线性插值动画主要属性FromToDurationAcceleratorDecceleratorFillBehavior等这些常用属性里不做介绍,主要介绍一下几个故事板属性,以备忘记.名称说明Completed动画到达终点时触发,该事件中可以......
  • WPF 触发器
    一、样式触发器样式触发器可以在指定的控件属性满足某种条件后进行一些样式的变换,当触发条件不满足时恢复原样。样式触发器的简单使用<Window.Resources><Stylex:Key="checkBoxStyle"TargetType="CheckBox"><Style.Triggers><TriggerProperty="......
  • 常见集合面试篇
    常见集合面试篇LIST一、底层实现1、数据结构-数组1.1为什么数组索引从0开始?从1开始不行吗?1.2操作数组的时间复杂度查询插入和删除2、源码分析2.1成员变量2.2构造方法2.3扩容机制二、面试问题1、ArrayList底层实现原理是什么?2、ArrayList扩容?3、如何实现数组与List之......
  • WPF 窗体关闭的方式
    1.Close();关闭当前窗口在WPF应用程序的关闭是有ShutdownMode属性设置,具有3中枚举类型的值:1)OnLastWindowClose(默认值)---应用程序最后一个窗体关闭时关闭应用程序2)OnMainWindowClose---应用程序主窗体关闭时关闭应用程序3)OnxplicitShutdown---显示调用关闭这......