Enabling a button though Data Binding
I'm using the November CTP, and am having difficulty enabling a button with Data Binding. I've looked at code from previous CTPs, so it seems my code is right, and this should work, but I'm having no luck.
<Button x:Name="btnSave" Content="Save">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SaveButtonEnabled}" Value="true">
<Setter Property="Button.IsEnabled" Value="true"/>
<Setter Property="Button.FontSize" Value="18"/>
<Setter Property="Button.FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
When the SaveButtonEnabled property is set to true, the font size and wieght change, but the button does not become enabled. I've also tried using a named (keyed) style rather than an inline style, but didn't make any difference.
I tried an alterative approach, whcih is to bind IsEnabled directly to the SaveButtonEnabled property, which is of type bool.
<Button x:Name="btnSave" Content="Save" IsEnabled="Binding (Path=SaveButtonEnabled)/>
When the Page loads, I get a BamlException stating that the IsEnabled property requires a Boolean, and I need to provide a Boolean or a type that can be converted. As stated before my property is boolean.
I can provide more XAML and the DataContext object if needed.
Any advice will be welcome.
Gunther
My guess is that you are setting the Button's IsEnabled property to false sometime before but never set FontSize or FontWeight directly (only in the style). Is this the case? If so, the reason why you are seeing this behavior is that properties set directly in elements by the user have priority over properties set in a Style.
To fix this problem, I would recommend having those properties controled 100% by the data source and DataTrigger, and never set them directly. If the effect you want is to have the button's text big and the button enabled when SaveButtonEnabled = true and vv, this is what I would do:
<
Style.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource source}, Path=SaveButtonEnabled}" Value="true">
<Setter Property="Button.IsEnabled" Value="true"/>
<Setter Property="Button.FontSize" Value="18"/>
</DataTrigger>
<DataTrigger Binding="{Binding Source={StaticResource source}, Path=SaveButtonEnabled}" Value="false">
<Setter Property="Button.IsEnabled" Value="false"/>
<Setter Property="Button.FontSize" Value="10"/>
</DataTrigger>
</Style.Triggers>You can find a working sample with this markup here: http://www.beacosta.com/forum/EnablingButton.zipLet me know if this worked for you and if you have any other questions.