Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF DataGrid不更新只读值_Wpf_Datagrid - Fatal编程技术网

WPF DataGrid不更新只读值

WPF DataGrid不更新只读值,wpf,datagrid,Wpf,Datagrid,我有一个带有以下数据项的列表: class MyClass { public double MyValue { get; set; } = 0; public double MyReadonlyValue { get { return MyValue +1; } } } 以及以下数据绑定DataGrid: <DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False"

我有一个带有以下数据项的
列表

class MyClass
{
    public double MyValue { get; set; } = 0;
    public double MyReadonlyValue
    {
        get { return MyValue +1; }
    }
}
以及以下数据绑定
DataGrid

<DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=MyValue}" Header="myvalue"/>
        <DataGridTextColumn Binding="{Binding Path=MyReadonlyValue}" Header="myreadonlyvalue" IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>
myreadonly值得到正确更新

但我希望在myValue编辑操作(如
CellEditEnding
)结束时更新值,并且在编辑过程中,我无法调用
刷新
函数。我怎么办


谢谢。

每当
MyValue
设置为新值时,执行
INotifyPropertyChanged
并引发
MyReadonlyValue
属性的
PropertyChanged
事件:

class MyClass : INotifyPropertyChanged
{
    private double _myValue;
    public double MyValue
    {
        get { return _myValue; }
        set
        {
            _myValue = value;
            NotifyPropertyChanged();
            NotifyPropertyChanged(nameof(MyReadonlyValue));
        }
    }

    public double MyReadonlyValue
    {
        get { return MyValue + 1; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
class MyClass : INotifyPropertyChanged
{
    private double _myValue;
    public double MyValue
    {
        get { return _myValue; }
        set
        {
            _myValue = value;
            NotifyPropertyChanged();
            NotifyPropertyChanged(nameof(MyReadonlyValue));
        }
    }

    public double MyReadonlyValue
    {
        get { return MyValue + 1; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}