WPF如何检测listviewitem已选定

本文关键字:listviewitem 检测 何检测 WPF | 更新日期: 2023-09-27 18:18:33

我有DataTemplate为我的ListView控件,它包含几个文本块和一个按钮。我希望按钮只在项目被选中时可见。下面是我的DataTemplate代码:

<ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid Background="AliceBlue">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="300" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="90" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock HorizontalAlignment="Left" Margin="10,0,10,0" 
                                           VerticalAlignment="Center" FontFamily="Verdana" FontSize="16" 
                                           FontWeight="Black" Grid.Column="0" Text="{Binding name}"/>
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Column="1">
                                        <TextBlock FontFamily="Verdana" FontSize="10" Grid.Column="1"
                                           VerticalAlignment="Center" HorizontalAlignment="Center"
                                           Text="Number of Chapters: " />
                                        <TextBlock FontFamily="Verdana" FontSize="12" Grid.Column="1"
                                           VerticalAlignment="Center" HorizontalAlignment="Center"
                                           Text="{Binding chaptersCount}" />
                                    </StackPanel>
                                    <Button HorizontalAlignment="Center" Height="50" Width="80" Content="Read" 
                                        Grid.Column="2" Visibility="Hidden" Click="Button_Click_3" Name="ReadButton"/>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>

我该怎么做呢?

WPF如何检测listviewitem已选定

你可以在DataTemplate中使用 DataTrigger 来实现这一点,它将检查模板父元素的 IsSelected 属性:

<ListView.ItemTemplate>
    <DataTemplate>
        .....
            <Button HorizontalAlignment="Center" Height="50"
                    Width="80" Content="Read" 
                    Grid.Column="2" Visibility="Hidden" Name="ReadButton"/>
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsSelected,
                         RelativeSource={RelativeSource Mode=TemplatedParent}}"
                         Value="True">
                <Setter TargetName="ReadButton" Property="Visibility"
                        Value="Visible"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ListView.ItemTemplate>

你需要稍微改变一下你的按钮:一个快速的XAML方法是在它的Visibility上添加一个Trigger,使它在不被选中时隐藏(注意:代码写在这里,希望没有打字错误):

<Button HorizontalAlignment="Center" Height="50" Width="80" Content="Read" 
                                        Grid.Column="2" Click="Button_Click_3" Name="ReadButton">
  <Button.Style>
    <Style TargetType="{x:Type Button}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}" Value="False">
          <Setter Property="Visibility" Value="Visible" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>