1

I was trying out some dummy application just to test binding modes. So, just curious to know how did the binding modes work. I have this xaml code-

<Button x:Name="btn"
                Height="20"
                Width="200"
                VerticalAlignment="Top">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="IsEnabled"
                            Value="{Binding CanEnable, Mode=TwoWay}" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding TextChanged}" Value="true">
                            <Setter Property="IsEnabled"
                                    Value="true" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

Here by button IsEanbled is binded to my viemodel property "CanEanble" whose default value is false. Now in my trigger i was listening to "TextChanged" property and setting button IsEnabled to true. Button gets enabled as it should be but the property "CanEnable" did not set to true even the biding mode is set to TwoWay.. Why this is happening??

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Go through this: [Two-way binding in WPF](http://stackoverflow.com/questions/320028/two-way-binding-in-wpf). Seems to be related. – publicgk Apr 21 '11 at 07:42
  • Problem is different in this case. I have implemented INPC already in my viewmodel. – Rohit Vats Apr 21 '11 at 08:06

1 Answers1

2

By setting the value in the trigger you basically remove the binding you previously set in the style setter. Take a closer look at the style. You will notice that you basically you set the property IsEnabled twice. First in the style setter, second in the trigger. It is logical that the second value overrides the previous value.

The desired effect can be achieved from code if you set the value of the dependency property using SetCurrentValue method:

SetCurrentValue(Button.IsEnabledProperty, true);

This way the bindings set on this property will not be removed and it will work as expected.

Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
  • Yeah, i was thinking down the same line too. Actually i want the pure xaml approach if it can be achieved otherwise i have to settle with code behind thing only... :( – Rohit Vats Apr 21 '11 at 08:18
  • Also if i bind the property directly over style then trigger didn't get fire thats why i moved that into style.. – Rohit Vats Apr 21 '11 at 08:19