1
votes

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??

1
Go through this: Two-way binding in WPF. Seems to be related.publicgk
Problem is different in this case. I have implemented INPC already in my viewmodel.Rohit Vats

1 Answers

2
votes

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.