代码之家  ›  专栏  ›  技术社区  ›  Trax

C#Wpf编辑数据网格不更新其项源

  •  5
  • Trax  · 技术社区  · 11 年前

    我有一个这样的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>
    

    我希望,每当我选中或取消选中数据网格上的复选框时,它就会更新我的ObservableCollection。

    最简单的方法是什么?

    谢谢

    2 回复  |  直到 11 年前
        1
  •  3
  •   Kelly    11 年前

    问题是如何绑定到集合。您正在显式设置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。

        2
  •  3
  •   Trax    11 年前

    我遵守了指示 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));
        }
    }
    

    现在一切都很完美。