属性 struct 属性、DataGrid、Binding、INotify属性Changed、List 的行值更改的更改通知

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

我有一个 struct 名称 datapoints,它有 属性(int x 和 string y),构造函数采用一个整数和一个字符串来赋予它们的价值。我还制作了那个结构 datapointsobservablecollection。在制作集合时,我在第一个和第二个列表中传递了字符串 属性 FirstCell 和 SecondCell,而这个 FirstCell 和 SecondCell 是 属性,其中实施了 Inotify属性 更改。现在,当我更改此 FirstCell 和 SecondCell 时,它们不会在数据网格中发生更改。

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

我检查了 InotifyPropertychanged 是否已实现或未检查它们在下面标签中的值,它会相应更新,但数据网格不会更新。例如,如果我更改 luffy 的值并在第一个文本框中写入 Zoro,那么输出中的第一个标签(第一个单元格值(DataGrid 中的 Y1 是):Zoro 但 Y table 第一行中的输出仍然是路飞.

P.S- 我正在编写这个程序来模仿我在尝试使用 OxyPlot 中的数据点时的情况,因此我必须使用结构并且不能将 class 用于 数据点.

我将结构更改为 class 并且一切正常,但由于您的数据来自结构,我认为以下解决方案适合您。

Struct 是一种值类型,绑定将获取它的副本,因此永远不会更新原始对象。

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

更改它以在每次单元格值发生变化时创建集合。