I know this can be done in a converter, but I'd like to disable a button based on a particular item selected in a ComboBox, using only XAML.
The following below works, using the Visibility property. How come if I try to use IsEnabled it does not work? Is there something I'm missing? If that's the way it is, if someone could explain why, that would be great. Should I just always use converters?
<ComboBox Name="WidthTypeComboBox"
ItemsSource="{Binding Source={StaticResource WidthType}}"
SelectedItem="{Binding WidthTypeSelected}" />
Works:
<Button Content="Map Channels" Command="{Binding ShowChannelAction}">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedIndex,
ElementName=WidthTypeComboBox}" Value="0">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Does not work:
<Button Content="Map Channels" Command="{Binding ShowChannelAction}">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedIndex,
ElementName=WidthTypeComboBox}" Value="0">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
I'm actually not sure how the first one is working. It depends on what you've set for the properties on your button. Usually you need a default setter in the style and to remove any IsEnabled value set locally on the button:
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedIndex,
ElementName=WidthTypeComboBox}" Value="0">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
It comes down to the precedence of where the property value is set. For example, property values set locally override style setter/triggers. This article explains dependency property precendence with some examples.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.