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

时间:2013-03-20 14:25:51

标签: wpf image listbox

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

我怎样才能做到这一点?

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.

2 个答案:

答案 0 :(得分:2)

IsSelected的{​​{1}}属性绑定到您的媒体资源ListBoxItem时,它应该有效。但这取决于您如何实现ViewModel和属性。

IsVisible

答案 1 :(得分:1)

当我需要将项目的可见性绑定到布尔值时,我一直在使用转换器类:

public class BoolToVisibleOrHidden : IValueConverter {
        public BoolToVisibleOrHidden() { }
        public bool Collapse { get; set; }
        public bool Reverse { get; set; }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            bool bValue = (bool)value;
            if (bValue != Reverse) {
                return Visibility.Visible;
            } else {
                if (Collapse)
                    return Visibility.Collapsed;
                else
                    return Visibility.Hidden;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            Visibility visibility = (Visibility)value;
            if (visibility == Visibility.Visible)
                return !Reverse;
            else
                return Reverse;
        }
    }

在XAML中抓取它的实例后如下:

<local:BoolToVisibleOrHidden x:Key="BoolToVisConverter" Collapse="True" />

我可以绑定像这样的项目的可见性:

Visibility="{Binding Converter={StaticResource BoolToVisConverter}, Path=DataContext.PATHTOBOOLEAN}"