2015-03-31 117 views
0

我对ListBox定义了以下ItemTemplate当选择项目时更改列表框中的边框元素的颜色

<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的颜色?

回答

0

项目选择颜色在ControlTemplate中定义为ListBoxItem。这是注入你的ItemTemplate的容器,因此在你的DataTemplate被渲染时颜色已经被设置。要覆盖它,你需要设置ListBox.ItemContainerStyle到一个新的StyleControlTemplate这是你想要的。下面是一个简单的例子。为了尽可能地保持您现在看到的效果,请使用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> 
+0

我不确定我们在谈论同样的事情。我不想改变颜色本身,我只希望我的截图中的线条在选择时也是白色的。我不希望它像截屏一样保持黑色。 – 2015-04-01 05:08:37