2011-04-01 122 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及其使用的顏色。