3
我有一個ListBox
使用DataTemplate
。單個ListBoxItems
顯示爲TextBlock
和ComboBox
。我現在想爲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
,但在這種情況下,我不知道如何重置顏色爲默認值。
感謝您的任何建議!
亨德里克。
如果你只想顯示一個列表,但並不需要選擇的能力,你可以使用一個ItemsControl,而不是你的列表框。 – LPL
這真是一個很好的暗示,因爲我實際上試圖通過設置風格來隱藏選擇能力! :) 我把ItemsControl包裝在一個ScrollViewer裏面,它似乎很好地工作。不過,我仍然對使用ListBox/ComboBox/Styles的解決方案感興趣。 – Hendrik