0

I have a Data Trigger defined this way:

 <DataTrigger Binding="{Binding Path=DataContext.MyObject.MyProperty, Mode=OneWay, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="False">
    <Setter Property="IsEnabled" Value="False" />
 </DataTrigger>

MyObject is in the View Model. Now, when the MyObject's MyProperty changes, it does not Notify the UI, even though the MyProperty does Notify OnPropertyChange. The object MyObject is registered with the Container as we are using Microsoft Prism as the framework.

How should I get this to work?

EDIT: MyObject is defined this way:

public class MyObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool _myProperty;
    public bool MyProperty
    {
        get { return _myProperty; }
        set
        {
            if (value == _myProperty)
                return;

            _myProperty = value;
            OnPropertyChanged("MyProperty");
        }
    }
}
Shankar Raju
  • 4,356
  • 6
  • 33
  • 52
  • Post the definition of `MyObject` - hard to diagnose much with just the trigger. – JerKimball Mar 23 '13 at 03:04
  • done, all I am doing is MyProperty gets populated frequently and it should update the state on the UI – Shankar Raju Mar 23 '13 at 03:12
  • Hmm...nothing jumps out...why `Mode=OneWay` on the trigger binding? Oh, and are you seeing any binding errors in the output window with the debugger attached? – JerKimball Mar 23 '13 at 03:14
  • No, there are no binding errors, and the binding happens when the View is Loaded and the debugger enters the getter of MyProperty. However, on the subsequent change of the property, the trigger does not fire. – Shankar Raju Mar 23 '13 at 03:37
  • Can you post your xaml element this trigger is being used on fully. Cannot see if a default value missing in style or explicit value set in the element itself is making the binding value ignored – Viv Mar 23 '13 at 06:20

3 Answers3

0

I don't see anything "wrong" with the code you've posted - You're absolutely sure you are changing the property value, and doing any required invoking back to the proper Dispatcher?

Can you try this construct instead? (wild guess, not sure it'd fit in your context)

[edit: I'm going from memory here, so the syntax needs tweaking]

<Trigger Property="{Binding Path=DataContext.MyObject.MyProperty, Mode=OneWay, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="False">
    <Setter Property="IsEnabled" Value="False" />
</Trigger>
JerKimball
  • 16,584
  • 3
  • 43
  • 55
0

Are you sure the RelativeSource works without explicit FindAncestor? Try to change

RelativeSource={RelativeSource AncestorType=UserControl}}"

to:

RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Though I'd say its better to be explicit and state Mode=FindAncestor, than hope for defaults, according to http://stackoverflow.com/questions/84278/how-do-i-use-wpf-bindings-with-relativesource it should still work without. – Viv Mar 23 '13 at 06:22
  • Then I guess you'll have to give us some more context, I don't know, post the full UserControl XAML, and btw, why is your `OnPropertyChanged` marked with that `[NotifyPropertyChangedInvocator]` Attribute? – Federico Berasategui Mar 23 '13 at 06:32
0

If the DataContext is set to an instance of MyObject - then you just want DataContext.MyProperty - unless your datacontext really does have a property called MyObject which is also an instance of MyObject

DataContext.MyProperty (DataContext is instance of MyObject already)

vs

DataContext.MyObject.MyProperty (Does your datacontext object (viewmodel) really have a property called MyObject of type MyObject?)

Example :

<DataTrigger Binding="{Binding Path=DataContext.MyProperty, Mode=OneWay, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="False">
    <Setter Property="IsEnabled" Value="False" />
 </DataTrigger>

If i am wrong could you post the code for the ViewModel - ie what is set as the DataContext

Richard Friend
  • 15,800
  • 1
  • 42
  • 60