C# Wpf 编辑 Datagrid 时,未更新其 itemsource

5

我有一个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;

我在数据表格中有一个复选框,如下所示:

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

我希望在数据表上勾选或取消勾选复选框时,能够更新我的ObservableCollection。您知道最简单的实现方式吗?

谢谢。

2个回答

3

我按照这里的说明进行操作。

我将“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));
    }
}

现在一切都运行良好。


2
这个回答实际上比被接受的回答更正确。问题确实是因为Item是一个结构体。使用结构体会导致WPF更新副本,因此集合中的原始项将保持不变。 - Zarat

3
问题在于你绑定集合的方式。你明确设置了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。

1
你的代码修复了问题,但你的解释是错误的。在代码中将ItemsSource设置为ObservableCollection没有任何问题,它可以正常工作。问题出在他使用了一个结构体,因此绑定更新了集合中元素的副本 - Zarat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接