C# Wpf Editing Datagrid does not update it's itemsource

心已入冬 提交于 2019-12-04 11:30:18

The problem is how you are binding to your collection. You are setting the ItemsSource explicitly therefore the ObservableCollection will not work the way you want it to.

Instead use binding like so:

<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>

Then ensure you do this in the background:

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

For the changes of each item to be reflected you need to use INotifyPropertyChanged like so:

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"));
            }
        }

    }

Now when an item is changed by the users the change can be seen in the ObservableCollection. This is thanks to that INotifyPropertyChanged.

I followed the instructions HERE.

I changed "Item" struct to "Item" class like this;

 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));
    }
}

Everyting works perfect now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!