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