2

我有这个简单的 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}"

勾选所有图像,但应仅在选定图像上处于活动状态。

4

2 回答 2

2

IsSelected当您将 的属性绑定ListBoxItem到您的属性时,它应该可以工作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>
于 2013-03-20T14:46:53.817 回答
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}"
于 2013-03-20T14:54:19.270 回答