2013-04-25 65 views
0

關於前一個問題,我有以下的代碼,禁用藍色背景: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> 

什麼我注意到的是,現在一切事物都像組合框即在內容中也具有相同風格的透明選擇。我需要做什麼才能讓選擇對ListBoxItem唯一透明,而不是其內容?

回答

1

您可以設置這些刷子的原始值在DataTemplate中,像這樣:

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

        </Expander> 
       </Border> 
      </DataTemplate> 
+0

謝謝,雖然我必須分別硬編碼#3399ff和#f0f0f0的'標準'顏色,能夠從系統中獲得原始顏色將會很好。 – 2013-04-26 13:50:06

0

你將不得不針對ListBoxItem並添加到ListBoxResources

<ListBox> 
    <ListBox.Resources> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <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.Resources> 
</ListBox> 
+0

更改ListBox.Style到列表框控件.Resources確實消除了錯誤,但行爲與原始相同,ComboBox或DataGrid繼承了透明樣式。 – 2013-04-25 21:51:46