2013-04-25 119 views
1

我是WPF新手;我有一個包含擴展(擴展器有他們周圍的邊框)列表框:更改ListBoxItem內容的背景顏色

<ListBox Background="Transparent" BorderBrush="Transparent"> 
    <ListBox.Style> 
     <Style> 
      <Style.Resources> 
       <!-- Background of selected item when focussed --> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
       <!-- Background of selected item when not focussed --> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
      </Style.Resources> 
     </Style> 
    </ListBox.Style> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border Margin="5" BorderThickness="2" BorderBrush="LightGray" CornerRadius="5"> 
       <Expander IsExpanded="True" Background="#f7f7f7"> 
        <!-- Content --> 
       </Expander> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ListBox> 

我想的行爲所選擇的項目有其展開的背景顏色變深(例如#e0e0e0)。我找到了讓我隱藏ListBoxItem的背景顏色(這在代碼中顯示)的示例,但是沒有好的方法來更改ListBoxItem中內容的背景顏色。我知道我需要設置一個觸發器來執行此操作,但我不知道如何設置它,無論它是ListBox還是Expander的觸發器。

如何設置適當的觸發器?

+0

你可能想嘗試Expression Blend中,這是非常好的工具,做這樣的事情 – 2013-04-25 15:14:50

+0

觸發器需要評估的條件,你要什麼條件。是基於數據值的觸發器,還是僅僅在選擇或擴展該行/項目時? – 2013-04-25 15:15:20

回答

1
<DataTemplate> 
    <Border Margin="5" BorderThickness="2" BorderBrush="LightGray" CornerRadius="5"> 
     <Expander IsExpanded="True" Background="#f7f7f7" Name="expander"> 

     </Expander> 
    </Border> 
    <DataTemplate.Triggers> 
    <DataTrigger Binding="{Binding IsSelected,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="True"> 
     <Setter Property="Background" Value="#e0e0e0" TargetName="expander"/> 
    </DataTrigger> 
    </DataTemplate.Triggers> 
</DataTemplate> 
+0

這很好用;謝謝。 – 2013-04-25 15:44:45