更改ListBoxItem的选择颜色

时间:2012-12-17 22:48:34

标签: c# wpf listbox border mouseevent

如何将IsSelected触发器添加到关注ListBox,这将更改名为Background的{​​{1}}的{​​{1}}。我不能通过在Border旁边添加PlaceHolder触发器来实现此目的。我不想选择整个IsSelected,只需选择IsMouseOver。感谢任何帮助!

ListBoxItem

1 个答案:

答案 0 :(得分: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>