2

I have a window with two checkboxes bound to properties of class Options:

public class Options
{
    public bool Option1 { get; set; }

    public bool Option2 { get; set; }

    public bool AnotherOption { get; set; }
}

xaml:

<CheckBox
    Content="Option #1"
    IsChecked="{Binding Path=Option1}"/>
<CheckBox
    Content="Option #2"
    IsChecked="{Binding Path=Option2}"/>

Also I have third checkbox that should be disabled when the other two are unchecked. To achieve this I used multibinding:

<CheckBox
    IsChecked="{Binding Path=AnotherOption}"
    Content="Another option">
<CheckBox.IsEnabled>
    <MultiBinding Converter="{StaticResource MultiValueLogicalOrConverter}">
        <Binding Path="Option1"/>
        <Binding Path="Option2"/>
    </MultiBinding>
</CheckBox.IsEnabled>
</CheckBox>

converter:

public class MultiValueLogicalOrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values.Cast<bool>().Any(value => value);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

This seems to work fine. But sometime it was pointed out that Option's properties are not dependency properties and don't fire PropertyChanged event, so I cannot explain why this works. Any ideas?

user10101
  • 1,704
  • 2
  • 20
  • 49
  • 4
    When you say "this seems to work fine" what behavior do you mean? I would expect: if I check a check box in the UI, the model updates; and the disabled state of the third box is set appropriately. That behavior doesn't require OnPropertyChanged. I would expect that if some other code modifies the model behind the scenes, your UI does not update. Is that true? – Mikeb Oct 24 '11 at 12:37
  • 5
    possible duplicate of [Why does the binding update without implementing INotifyPropertyChanged?](http://stackoverflow.com/questions/7767218/why-does-the-binding-update-without-implementing-inotifypropertychanged) – H.B. Oct 24 '11 at 12:45
  • Mikeb, thanks for your reply. When I said "works fine" I only meant that third checkbox is disabled/enabled properly. I suspected that if other code will modify the model, UI will not update. Just didn't know that it should work even in this particular case. Can I always rely on this behaviour when don't care about model updates from behind the scene? – user10101 Oct 24 '11 at 13:49

2 Answers2

1

If you don't need the view to care about the model, then there's no point in using the INotify interface. However, if for any reason the model's property changes, you'll need to if you want your view to be aware of it.

Yatrix
  • 13,361
  • 16
  • 48
  • 78
  • 1
    You're basically using OneWayToSource mode in your binding. – Yatrix Oct 24 '11 at 19:16
  • However in my example updates occur from model to view direction (for third checkbox). - The first two checkboxes modify the model and the state of third checkbox is updated accordingly. This was the question. – user10101 Oct 25 '11 at 08:11
  • Can you post the code for the converter? Is something being set in there? – Yatrix Oct 25 '11 at 13:15
  • So, let me just make sure I'm getting this. Does "Another Option" change when you start checking and unchecking the Option 1 and Option 2 boxes? Or is it just disabled when you open the window and that's what's confusing you? I copy-pasted everything you gave me into a fresh form and "Another Option" is disabled at the start, but it never changes state when I check Option 1 and Option 2. – Yatrix Oct 25 '11 at 13:47
  • H.B. may be right in the comments under your question. Did you check that link out? If you're just talking about when the window first launches, then it's disabled due to the converter - it checks initially. If you're talking about it changing state when you change the state of the other checkboxes, that link seems to explain why. – Yatrix Oct 25 '11 at 14:00
0

When you update a model with a binding from the GUI, the model is reloaded by the bindings to get the result value.

Sjoerd
  • 620
  • 5
  • 10