1

如何将IsSelected触发器添加到以下ListBox,这将更Background改为Border调用PlaceHolder。我不能通过在 .IsSelected旁边添加触发器来做到这一点IsMouseOver。我不想选择整体ListBoxItem,只是Border。感谢任何帮助!

<ListBox>
    <ListBox.Template>
        <ControlTemplate>
            <ItemsPresenter />
        </ControlTemplate>
    </ListBox.Template>

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="2" Columns="3"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ListBox.ItemContainerStyle>
        <Style  TargetType="{x:Type ListBoxItem}">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                            <Border CornerRadius="5" x:Name="PlaceHolder" BorderBrush="PapayaWhip" BorderThickness="1">
                                <StackPanel Orientation="Horizontal" Width="148" Height="60">
                                    <Image Source="{Binding Image}"></Image>
                                    <Label VerticalAlignment="Center" x:Name="title" FontSize="12" FontWeight="SemiBold" Foreground="Gray" Content="{Binding Title}"></Label>
                                </StackPanel>
                            </Border>
                        <DataTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border"  Property="Background" Value="Bisque"></Setter>
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
4

1 回答 1

2

您将必须绑定到该ListBoxItem.IsSelected属性。

这应该工作

<ListBox.ItemContainerStyle>
    <Style  TargetType="{x:Type ListBoxItem}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        </Style.Resources>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border CornerRadius="5" x:Name="PlaceHolder" BorderBrush="PapayaWhip" BorderThickness="1">
                        <StackPanel Orientation="Horizontal" Width="148" Height="60">
                            <Image Source="{Binding Image}"></Image>
                            <Label VerticalAlignment="Center" x:Name="title" FontSize="12" FontWeight="SemiBold" Foreground="Gray" Content="{Binding Title}"></Label>
                        </StackPanel>
                    </Border>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                            <Setter TargetName="PlaceHolder" Property="Border.Background" Value="Red"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListBox.ItemContainerStyle>
于 2012-12-17T23:13:20.747 回答