I am working with WPF and trying to change the Enabled state of a button when we selected an Item of a ListView, or in other words, how to trigger the Enabled state of a button when we Select any ListViewItem?
And i am trying to do the reverse thing with another button, or in ot开发者_开发问答her words... Can i change the SelectedItem property from the ListView to null when i click in other button if i am using Commands? How?
Thanks in advance!
You could use a DataTrigger
to disable it when null.
e.g. (ListView
being named lv
)
<Button Content="Lorem Ipsum">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem, ElementName=lv}"
Value="{x:Null}">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Alternatively you could bind the IsEnabled
property directly and add a Converter
to the binding which returns a respective bool.
Example to clear selection:
<Button Content="!">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="lv"
Storyboard.TargetProperty="SelectedItem">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Null}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
精彩评论