1

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
    }
};
  • 1
    You'd use `BindingMode.OneWay`. Also, it's highly advisable that you follow MVVM and don't use code-behind like that. Your DataGrid should not be constructed with C#. To put notifications to the UI, you'd use `INotifyPropertyChanged`, typically in a ViewModel, but sometimes acceptable in a Model. – Kcvin Aug 26 '15 at 15:35
  • @NETscape : I've tried using BindingMode.OneWay, but any changes made to T (an editable property/column) are not updated or reflected in the UI on V (the read only column). I'm guessing this is where INotifyPropertyChanged comes into play, but I'm unsure how to use it in this situation so that whenever T changes, the change in V is reflected in the UI (do you have an example?). As far as a MVVM paradigm goes, I'll look into doing that. The reason I approached it in C# is that the columns of this DataGrid are intended to be dynamic based on certain criteria (it will be bound to SQL and/or XML) – user5269172 Aug 26 '15 at 15:53
  • They aren't reflected in the UI because you're not telling the UI that the property changed. Example, John is friends with Bill, and they can both talk to each other (TwoWay). Bill wants to know when John turns a year older so he can publish it in the paper, but will never know John's birthday. The only way he will know when he is a year older is when John tells him. Bill can't tell John when he is a year older, so this should be one-way communication. Now all we have to worry about is John notifying Bill. You have to notify UI. – Kcvin Aug 26 '15 at 16:04
  • I recommend reading [this](http://reedcopsey.com/series/windows-forms-to-mvvm/). And you're more than welcome to hangout in [wpf chat](http://chat.stackoverflow.com/rooms/18165/wpf) and ask questions, assuming you're willing to learn ;) – Kcvin Aug 26 '15 at 16:06
  • @NETscape I didn't know such a chat existed, I will definitely use it! And I'm quite willing to learn, as I'm using this project to move away from WinForms to WPF for our company's internal application(s). I managed to get it to work utilizing INotifyPropertyChanged and implementing a handler that fires off V changing when T is set. I used [this post](http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist) as a guide. I guess I was surprised that there wasn't a cleaner way to do something so simple. Thanks for pointing me in the right direction! – user5269172 Aug 26 '15 at 16:14
  • There are frameworks out there to assist with MVVM (but I'd pass them up until you have full understanding of what is actually happening) and boilerplate code. In reality it pays off though. As you learn MVVM, you will see that properly designed applications are very easy to work with and change. I try to go with 0 code in my code-behind (xaml.cs) files, that'll usually help ensure you're doing MVVM right. – Kcvin Aug 26 '15 at 16:38

0 Answers0