【问题标题】:C# Wpf Editing Datagrid does not update it's itemsourceC# Wpf 编辑 Datagrid 不更新它的 itemsource
【发布时间】:2014-09-07 00:45:00
【问题描述】:

我有一个像这样的 ObservableCollection,

ObservableCollection<Item> Found_Items = new ObservableCollection<Item>();

public struct Item
        {
            public bool Enabled { get; set; }
            public BitmapImage ItemIcon { get; set; }
            public string Path { get; set; }
            public string Size { get; set; }
        }

我正在像这样设置 Datagrid 的 itemsource,

FoundItemsDatagrid.ItemsSource = Found_Items;

我在 Datagrid 中有一个这样的复选框,

<DataGridTemplateColumn Header="Path" Width="*" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DockPanel> 
                            <CheckBox IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>                         
                        </DockPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

我想,每当我选中或取消选中 datagrid 上的复选框时,它都应该更新我的 ObservableCollection。

最简单的方法是什么?

谢谢..

【问题讨论】:

    标签: wpf datagrid propertychanged


    【解决方案1】:

    问题在于您如何绑定到您的收藏。您正在显式设置 ItemsSource,因此 ObservableCollection 不会按您希望的方式工作。

    改为像这样使用绑定:

    <DataGridTemplateColumn Header="Path" Width="*" ItemsSource="{Binding Found_Items}" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DockPanel> 
                                <CheckBox IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>                         
                            </DockPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
    

    然后确保您在后台执行此操作:

    public ObservableCollection<Item> Found_Items {get; set;}
    

    要反映每个项目的更改,您需要像这样使用 INotifyPropertyChanged:

    public class Item : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private bool enabled;
            private BitmapImage itemIcon;
            private string path;
            private string size;
    
    
            public string Size
            {
                get { return size; }
                set
                {
                    size = value;
                    if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Size"));
                }
            }
    
    
            public string Path
            {
                get { return path; }
                set
                {
                    path = value;
                    if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Path"));
                }
            }
    
    
            public BitmapImage ItemIcon
            {
                get { return itemIcon; }
                set
                {
                    itemIcon = value;
                    if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ItemIcon"));
                }
            }
    
    
    
            public bool Enabled
            {
                get { return enabled; }
                set
                {
                    enabled = value;
                    if(PropertyChanged!=null) PropertyChanged(this,new PropertyChangedEventArgs("Enabled"));
                }
            }
    
        }
    

    现在,当用户更改项目时,可以在 ObservableCollection 中看到更改。这要归功于 INotifyPropertyChanged。

    【讨论】:

    • 虽然您的修复工作有效,但您的解释是错误的。在代码中将 ItemsSource 设置为 ObservableCollection 没有任何问题,它可以正常工作。问题是他使用了一个结构,所以绑定更新了集合中元素的 copy
    【解决方案2】:

    我按照HERE的说明进行操作。

    我像这样将“Item”结构更改为“Item”类;

     public class Item : INotifyPropertyChanged
    {
        private bool _Enabled;
        private BitmapImage _ItemIcon;
        private string _Path;
        private string _Size;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public Item(bool enabled, BitmapImage itemIcon, string path, string size)
        {
            _Enabled = enabled;
            _ItemIcon = itemIcon;
            _Path = path;
            _Size = size;
        }
    
        public bool Enabled
        {
            get { return _Enabled; }
            set
            {
                _Enabled = value;
                this.NotifyPropertyChanged("Enabled");
            }
        }
    
        public BitmapImage ItemIcon
        {
            get { return _ItemIcon; }
            set
            {
                _ItemIcon = value;
                this.NotifyPropertyChanged("ItemIcon");
            }
        }
    
        public string Path
        {
            get { return _Path; }
            set
            {
                _Path = value;
                this.NotifyPropertyChanged("Path");
            }
        }
    
        public string Size
        {
            get { return _Size; }
            set
            {
                _Size = value;
                this.NotifyPropertyChanged("Size");
            }
        }
    
    
    
        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
    

    现在一切都很完美。

    【讨论】:

    • 这实际上比公认的答案更正确。问题确实是 Item 是一个结构的事实。使用结构将导致 WPF 更新项目的 副本,因此集合中的原始项目将保持不变。
    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多