gpt4 book ai didi

wpf - 将样式应用于 ListBoxItem 而不影响内部的 ComboboxItem

转载 作者:行者123 更新时间:2023-12-05 00:05:21 24 4
gpt4 key购买 nike

我有一个使用 DataTemplateListBox。单个 ListBoxItems 显示为 TextBlockComboBox。我现在想为 ListBoxItems 使用 Style,而不是为内部 ComboBoxes 的项目使用它。不幸的是,ComboBoxItem 继承自 ListBoxItem,这似乎使这成为不可能。或者我在这里遗漏了什么?

<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 添加另一种样式,但在这种情况下,我不知道如何将颜色重置为默认值。

感谢您的任何建议!

亨德里克。

最佳答案

也许有更好的解决方案。但是正如您提到的,您可以恢复默认颜色:

<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 属性。

关于wpf - 将样式应用于 ListBoxItem 而不影响内部的 ComboboxItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12296654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com