选择项目时,更改ListBox中Border元素的颜色

时间:2015-03-31 19:21:57

标签: wpf datatemplate

我为ItemTemplate定义了以下ListBox

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid Margin="0 4 0 4">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="grp1" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" SharedSizeGroup="grp2" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Border BorderBrush="Black" BorderThickness="0 0 0 1" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3">
                <TextBlock Text="..." FontSize="16" />
            </Border>
            <TextBlock Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" Text="..." />
            <StackPanel Grid.Column="0" Grid.Row="2" Orientation="Horizontal">
                <TextBlock Grid.Column="0" Grid.Row="2" Text="..." />
            </StackPanel>
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

问题是,当选择该项目时,所有TextBlock的前景颜色都会发生变化,但Border的颜色仍为黑色:

未选定的:

enter image description here

选择的:

enter image description here

如何更改Border的颜色以匹配TextBlock的颜色?

1 个答案:

答案 0 :(得分:0)

项目选择颜色在ControlTemplate ListBoxItem中定义。这是注入ItemTemplate的容器,因此在您呈现DataTemplate时已经设置了颜色。要覆盖它,您需要将ListBox.ItemContainerStyle设置为新的Style,其中ControlTemplate可以执行您想要的操作。一个简单的例子如下。为了使其尽可能接近您现在所看到的内容,请使用Blend中的选项“编辑当前模板在当前主题下呈现的模板”。然后,您可以只编辑所需的部分,并保持模板的其余部分相同。

<Style x:Key="ListBoxItemBasicStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid SnapsToDevicePixels="true">
                    <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
                <ControlTemplate.Triggers>

                <!-- Change IsSelected SelectedBackgroundBrush to set the selection color for the items -->
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="{DynamicResource SelectedBackgroundBrush}" TargetName="Border"/>
                    </Trigger>

                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>