Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Wpf Editing Datagrid does not update it's 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'm setting Datagrid's itemsource 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, whenever i check or uncheck the checkbox on datagrid it should update my ObservableCollection.

What is the easiest way to do this ?

Thanks..

like image 865
Trax Avatar asked Sep 07 '14 00:09

Trax


2 Answers

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.

like image 136
Kelly Avatar answered Nov 15 '22 12:11

Kelly


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.

like image 22
Trax Avatar answered Nov 15 '22 11:11

Trax