开发者

Enable button when SelectedItem of ListView changes from null and do the reverse

开发者 https://www.devze.com 2023-04-05 18:54 出处:网络
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 List

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

精彩评论

暂无评论...
验证码 换一张
取 消