mechbaral
mechbaral

Reputation: 133

Property change notification for struct property, DataGrid, Binding, INotifyPropertyChanged, List's row value change

I have a struct name datapoints which have the property (int x and string y) and the constructor takes a intger and a string to assign their value. I have also made a observablecollection of that struct datapoints. While making a collection i have passed string property FirstCell and SecondCell in first and second list and this FirstCell and SecondCell are the property which have Inotifyproperty change implemented. Now when i change this FirstCell and SecondCell they do not get changed in the datagrid.

Below is my code for MainWindow.xaml.cs file

public partial class MainWindow : Window,INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
        private string _FirstCell="LUFFY";
        private string _SecondCell= "SANJI";
        public string FirstCell
        {
            get { return _FirstCell; }

            set
            {
                _FirstCell = value;
                PropertyChanged(this, new PropertyChangedEventArgs(nameof(FirstCell)));
            }

        }
        public string SecondCell
        {
            get { return _SecondCell; }

            set
            {
                _SecondCell = value;
                PropertyChanged(this, new PropertyChangedEventArgs(nameof(SecondCell)));
            }
        }

        private ObservableCollection<datapoints> _CheckNotifyUpdate;
        public ObservableCollection<datapoints> CheckNotifyUpdate
        {
            get { return _CheckNotifyUpdate; }

            set
            {
                _CheckNotifyUpdate = value;
                PropertyChanged(this, new PropertyChangedEventArgs(nameof(CheckNotifyUpdate)));
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
       
            CheckNotifyUpdate = new ObservableCollection<datapoints>
            {
                new datapoints(1, FirstCell),
                new datapoints(2, SecondCell)
            };
        }
    } 

    public struct datapoints
    {
        public int x { get; set; }    
        public string y { get; set; }

        public datapoints(int X,string Y)
           
        {
              x = X;
              y = Y;
        }
    }

This is my XAML file

<Window x:Class="InotifyClassPropInsideList.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:InotifyClassPropInsideList"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type=local:MainWindow, IsDesignTimeCreatable=True}"
        Title="MainWindow" Height="450" Width="400">

    <StackPanel>
        <TextBox Text="{Binding FirstCell}" Margin="10"/>
        <TextBox Text="{Binding SecondCell}"  Margin="10"/>
        <StackPanel Orientation="Horizontal">
                <Label Content="The First Cell Value (Y1 in DataGrid) is : "/>
                <Label Content="{ Binding FirstCell}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
            <Label Content="The Second Cell Value is (Y2 in DataGrid) : "/>
                <Label Content="{ Binding SecondCell}"/>
            </StackPanel>
            <Button Content="Button" Margin="50"/>
        <DataGrid  SelectedIndex="{Binding SelectedDataIndex, Mode=TwoWay}"
                   ItemsSource="{Binding CheckNotifyUpdate }" AutoGenerateColumns="True"
                   HorizontalAlignment="Center"></DataGrid>
    </StackPanel>

</Window>

Current output is like this enter image description here

I checked if the InotifyPropertychanged is implemented or not checking their value in the label below and it updates accordingly but the datagrid doesnot update. For example if i change the value of luffy and write Zoro in the first textbox then the first label in output (The First Cell Value(Y1 in DataGrid is):Zoro but the output in the Y table first row is still LUFFY.

P.S- I am writing this program to imitate my situation when i am trying to use datapoints from the OxyPlot so I must use struct and cannot use class for datapoints.

Upvotes: 0

Views: 265

Answers (1)

Sats
Sats

Reputation: 1973

I changed the struct to class and everything work, but since your data comes by struct I think the below solution will for you.

Struct is a value type and binding will obtain a copy of it hence never update original object.

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
    private string _FirstCell = "LUFFY";
    private string _SecondCell = "SANJI";
    public string FirstCell
    {
        get { return _FirstCell; }

        set
        {
            _FirstCell = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(FirstCell)));
            RefreshGrid();
        }

    }
    public string SecondCell
    {
        get { return _SecondCell; }

        set
        {
            _SecondCell = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(SecondCell)));
            RefreshGrid();
        }
    }

    private ObservableCollection<datapoints> _CheckNotifyUpdate;
    public ObservableCollection<datapoints> CheckNotifyUpdate
    {
        get { return _CheckNotifyUpdate; }

        set
        {
            _CheckNotifyUpdate = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(CheckNotifyUpdate)));

        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        RefreshGrid();
    }

    private void RefreshGrid()
    {
        CheckNotifyUpdate = new ObservableCollection<datapoints>
        {
            new datapoints(1, FirstCell),
            new datapoints(2, SecondCell)
        };
    }
}

public struct datapoints
{
    public int x { get; set; }
    public string y { get; set; }

    public datapoints(int X, string Y)

    {
        x = X;
        y = Y;
    }
}

Change it to create the Collection every time when there is a change in cell value.

Upvotes: 1

Related Questions