wpf - 在 WPF 列表框中选择项目时显示刻度图像

标签 wpf image listbox

我有这个简单的列表框,它显示水平和垂直的图像列表。 我还在每张图像上添加了刻度图像。现在我想仅当在列表框中选择该项目时才启用此刻度图像。

我怎样才能实现这个目标?

列表框 XAML:

    <ListBox x:Name="PhotoCollection"
         Grid.Row="2"
         Grid.ColumnSpan="4"
         ItemsSource="{Binding PhotoCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         SelectionMode="Multiple">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Vertical">
        <Border BorderBrush="White"
                BorderThickness="2"
                Margin="5"
                Background="LightGray">
          <Grid>
          <Image Source="{Binding}"
                 Stretch="Uniform"
                 Width="50"
                 Height="50"
                 Margin="5" />
            <Image Source="{StaticResource Check_24}"
                   Visibility="{Binding Converter={StaticResource VisibleConverter}, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1},Path=IsSelected}"
                   Stretch="Uniform"
                   Width="20"
                   Height="20"
                   Margin="5" 
                   HorizontalAlignment="Right"
                   VerticalAlignment="Bottom"/>
          </Grid>
        </Border>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel IsItemsHost="True"
                 Orientation="Horizontal" />
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>

编辑:这行代码就可以了

Visibility="{Binding Converter={StaticResource VisibleConverter}, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1},Path=IsSelected}"

Tickmark on all images but should only be active on the selected images.

最佳答案

当您将 ListBoxItemIsSelected 属性绑定(bind)到属性 IsVisible 时,它应该可以工作。但这取决于您如何实现 ViewModel 和属性。

<ListBox>
  <!-- the rest of the XAML-Definition of your ListBox -->
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="IsSelected" Value="{Binding IsVisible, Mode=TwoWay}"/>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

关于wpf - 在 WPF 列表框中选择项目时显示刻度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15526289/

相关文章:

c# - 调用线程必须是 STA,因为在 WPF 中很多 UI 组件都需要这个

c++ - C# 与非托管 C++ 互操作性的续篇

c# - 隐藏 ListView ,直到按下按钮

Android:可滚动的图片库布局

java - 如何在 java 中获取 jpg 的像素尺寸?

delphi - 如何快速删除列表框中的重复项?

wpf - ICommand CanExecuteChanged 未更新

c# - ASP.NET 图片源问题

c# - 从列表框中删除项目会导致灾难性故障?

c# - WPF 列表框选择已更改