struct属性,DataGrid,Binding,INotifyPropertyChanged,List的行值更改的属性更改通知

时间:2018-11-22 16:43:20

标签: wpf data-binding datagrid observablecollection inotifypropertychanged

我有一个struct名称 datapoints ,该属性具有属性(int x和字符串y),构造函数使用一个整数和一个字符串来分配其值。我还为该结构数据点创建了observablecollection。在进行收集时,我在第一和第二个列表中传递了字符串属性FirstCell和SecondCell,并且此FirstCell和SecondCell是实现了Inotifyproperty更改的属性。现在,当我更改此FirstCell和SecondCell时,它们不会在数据网格中更改。

  

下面是我的MainWindow.xaml.cs文件代码

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

  

这是我的XAML文件

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

  

当前输出是这样的   enter image description here

我检查了是否实现了InotifyPropertychanged或未检查下面标签中的值,并且它会相应更新,但datagrid不会更新。例如,如果我更改luffy的值并将Zoro写入第一个文本框中,则输出中的第一个标签(DataGrid中的第一个单元格值(Y1为):Zoro,但Y表第一行中的输出仍为LUFFY。

P.S-我正在编写此程序来模仿我在尝试使用OxyPlot中的数据点时的情况,因此我必须使用struct并且不能对数据点使用类。

1 个答案:

答案 0 :(得分:1)

我将结构更改为类,并且一切正常,但是由于您的数据来自结构,因此我认为以下解决方案将为您服务。

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

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

每次单元格值发生更改时,请对其进行更改以创建集合。

相关问题