C # Wpf Editing Datagrid does not update its itemsource

I have an ObservableCollection like this,

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

      

I am setting up a datagrid datasource like this:

FoundItemsDatagrid.ItemsSource = Found_Items;

      

I have a checkbox in Datagrid like this,

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

      

I want that whenever I check or uncheck the checkbox on the datagrid, it should update my ObservableCollection.

What's the easiest way to do this?

Thank..

+3


source to share


2 answers


The problem is how you bind to your collection. You are setting the ItemSource explicitly, so the ObservableCollection won't work the way you want it to.

Instead, use a binding like this:

<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 make sure you do this in the background:



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

      

To change each item to be displayed, you need to use INotifyPropertyChanged like this:

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 the item is modified by users, this change can be seen in the ObservableCollection. This is thanks to INotifyPropertyChanged.

+1


source


I followed the instructions HERE .

I changed the "Item" structure to the "Item" class as follows:



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

      

Everything works fine now.

+3


source







All Articles