I've done a lot of searching, and found many post about this, but still no simple solution to this problem. I have a WPF DataGrid bound to a custom object implementing ObservableCollection< Example>. This generally works fine on editable properties with TwoWay binding; but some properties (such as V below) are supposed to be read-only, especially in the UI, and derive their value from other properties. However, as we know, a ReadOnly property cannot have TwoWay binding enabled. If I add a setter to V, and remove the ReadOnly property from the Binding, it works as expected, except users can, of course, edit that value which is not desired. I can't figure out the reason for this, as you could bind back read-only dataset columns in WinForms. I feel I'm missing something simple to allow this to work without overriding a bunch of classes. If I can't make that work, I might be able to work with a way to manually refresh the bound object on the DataGrid UI (if that works). I'm using .NET 4.6. Any help is appreciated, thanks.
Class Code:
public class Example
{
private double t = 0;
private double v = 0;
public double T
{
get { return t; }
set { t = value; }
}
public double V
{
get { return t + 1; }
}
}
Column Header Code:
new DataGridTextColumn()
{
Header = "V",
IsReadOnly = true,
Binding = new Binding (nameof(Example.V))
{
Mode = BindingMode.TwoWay //Needed to reflect changes back to DataGrid
NotifySourceUpdated = true //Doesn't seem to make a difference
}
};