2011-04-01 130 views
3

我想更改选定的背景并使其显示具有圆角的渐变。我搜索了Google,发现有些人通过覆盖默认颜色来改变所选的颜色。有什么办法可以做到这一点?我在想,有什么办法可以在选择一个项目时显示一个圆形的边框作为背景?更改选定的颜色列表框

回答

8

这是ListBoxItem的默认样式(这是我们想要更改的)。如果通过右键单击Objects和Timelines控件中的listboxitem来使用Expression Blend 4,则可以“检索”此样式。

<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
    <Setter Property="Padding" Value="2,0,0,0"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}" 
       Background="{TemplateBinding Background}" 
       Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" 
       > 
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="true"> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
        </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelected" Value="true"/> 
          <Condition Property="Selector.IsSelectionActive" Value="false"/> 
         </MultiTrigger.Conditions> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
        </MultiTrigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

让我们把一些重要的部分,让你学会这个做自己。

<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}"> 

这是样式声明的开始。我们给它一个x:Key以便它可以从资源字典中检索,并且我们已经为ListBoxItem设置了TargetType。

现在,我们要查找我们想要改变的样式部分。在这种情况下,我们要下去并在新的ControlTemplate上查找MultiTrigger的一部分样式。

<MultiTrigger> 
<MultiTrigger.Conditions> 
     <Condition Property="IsSelected" Value="true"/> 
     <Condition Property="Selector.IsSelectionActive" Value="false"/> 
    </MultiTrigger.Conditions> 
    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
</MultiTrigger> 

此MultiTrigger需要2个属性才能匹配值才能被激活。这个触发器在激活时会将背景颜色更改为Value =“...”,将前景颜色更改为Value =“...”。为了获得渐变背景,我们需要将Background Value =“...”中的值更改为不同的画笔。让我们创建一个小巧的渐变画笔(非常丰富多彩的一个呢!)

<LinearGradientBrush x:Key="GradientBrush" StartPoint="0,0" EndPoint="1,1"> 
     <GradientStop Color="Yellow" Offset="0.0" /> 
     <GradientStop Color="Red" Offset="0.25" /> 
     <GradientStop Color="Blue" Offset="0.75" /> 
     <GradientStop Color="LimeGreen" Offset="1.0" /> 
</LinearGradientBrush> 

现在让我们这个适用于本触发的背景。

<Setter Property="Background" TargetName="Bd" Value="{StaticResource GradientBrush}"/> 

现在,当这种风格应用到ListBoxItem的,而ListBoxItem的IsSelected = TRUE(和Selector.IsSelectionActive = FALSE),你会看到在ListBoxItem的渐变背景。

现在,你也想要圆角。如果我们走到ControlTemplate的顶部,我们会看到一个边界声明。

<Border x:Name="Bd" 

在该声明中,我们想要添加一个CornerRadius属性以在ListBoxItem上四舍五入。

CornerRadius="5" 

而现在,你应该可以创建一个圆角半径,线性渐变背景listboxitem。我希望你能够从这里继续自己。

+0

哇,很好的答案! – CodeNaked 2011-04-02 00:33:25

+0

Wao这是伟大的迷你toutorial.You是男人。 首先它没有工作,但改变'GradientBrush'name的工作。我认为它有任何方式有一些冲突。 这是一个很好的答案 – 2011-04-02 01:07:45

1

我在我的博客here上有一个示例。它覆盖ControlTemplate及其使用的颜色。