2012-09-06 39 views
3

我有一個ListBox使用DataTemplate。單個ListBoxItems顯示爲TextBlockComboBox。我現在想爲ListBoxItems使用樣式,而不用於內部ComboBoxes的項目。不幸的是,ComboBoxItem繼承ListBoxItem這似乎使這不可能。或者我在這裏錯過了什麼?應用樣式到ListBoxItem的,而不會影響ComboboxItem內

<ListBox Grid.Row="1" Grid.Column="1" Name="comboBoxI" Margin="2" 
       ItemsSource="{Binding SomeCollection}" IsSynchronizedWithCurrentItem="True"> 
    <ListBox.Resources> 
     <Style TargetType="ListBoxItem"> 
      <Style.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/> 
      </Style.Resources> 
     </Style> 
    </ListBox.Resources> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Name}" Width="320" Padding="1,1,1,1" 
         <TextBlock.ToolTip> 
          <ToolTip Content="{Binding Path=Description}"/> 
         </TextBlock.ToolTip> 
       </TextBlock> 
       <ComboBox ItemsSource="{Binding SomeOtherCollection}" IsSynchronizedWithCurrentItem="True" 
        <ComboBox.ItemTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding CreationInfo}" Width="Auto" Padding="1,1,1,1"> 
           <TextBlock.ToolTip> 
            <ToolTip Content="{Binding Path=Description}"/> 
           </TextBlock.ToolTip> 
          </TextBlock> 
         </DataTemplate> 
        </ComboBox.ItemTemplate> 
       </ComboBox> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

我也嘗試添加另一種風格的ComboBoxItem,但在這種情況下,我不知道如何重置顏色爲默認值。

感謝您的任何建議!

亨德里克。

+1

如果你只想顯示一個列表,但並不需要選擇的能力,你可以使用一個ItemsControl,而不是你的列表框。 – LPL

+0

這真是一個很好的暗示,因爲我實際上試圖通過設置風格來隱藏選擇能力! :) 我把ItemsControl包裝在一個ScrollViewer裏面,它似乎很好地工作。不過,我仍然對使用ListBox/ComboBox/Styles的解決方案感興趣。 – Hendrik

回答

1

也許有更好的解決方案。但正如你所說,你可以恢復默認顏色:

<Style TargetType="ComboBoxItem"> 
    <Style.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{x:Static SystemColors.HighlightTextColor}"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.ControlColor}"/> 
    </Style.Resources> 
</Style> 

備註

只在ListBox.ItemContainerStyle不幸的是應用ListBoxItem風格,這ListBox因你而改變系統刷適用於所有的內部控制將不起作用,不是ListBoxItem屬性。

相關問題