简体   繁体   中英

Check if ListView item selected

I wanna make my Button's IsEnabled property to check if ListView has a selection. Is there any way to check if any ListView item is selected using only XAML? Something like:

<Button Content="Remove" Command="{Binding RemoveConditionCommand}"
                CommandParameter="{Binding ElementName=conditionsListView, Path=SelectedItem}"
                IsEnabled="{Binding ElementName=conditionsListView, Path=IsSelected}"
                />

You can achieve that using DataTrigger . Set IsEnabled to false in case selectedItem is null for ListView.

Sample:

<Button>
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=SelectedItem,
                                               ElementName=conditionsListView}"
                             Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM