行。我有一个蓝色背景和白色文本的ComboBox。此ComboBox中显示的项目有两种:具有子项的项目和不具有子项目的项目。
TextBlock
。Expander
,其标题设置为简单TextBlock
,其正文设置为显示所有孩子的ItemsControl。我想在下拉列表中以黑色显示这些项目,而在选中时,我想在ComboBox的TextBox区域中以白色显示它们(即不在下拉列表中)。
所以我用Google搜索并找到了this post。经过多次反复试验,我取得了以下成绩:
<Style x:Key="MyComboStyle" TargetType="ComboBox">
<Setter Property="Background" Value="Blue" />
<Setter Property="Foreground" Value="White" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<DataTemplate.Resources>
<DataTemplate DataType="{x:Type localVM:CostCenterVM}" x:Key="CCWithoutChildren" >
<TextBlock Text="{Binding CCNumberName}" />
</DataTemplate>
<DataTemplate DataType="{x:Type localVM:CostCenterVM}" x:Key="CCWithChildren">
<Expander>
<Expander.Header>
<TextBlock VerticalAlignment="Center" Text="{Binding CCNumberName}" />
</Expander.Header>
<Expander.Content>
<ItemsControl ItemsSource="{Binding Children}" DisplayMemberPath="CCNumberName"
Background="Transparent" Padding="10,0,0,0" />
</Expander.Content>
</Expander>
</DataTemplate>
</DataTemplate.Resources>
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding HasChildren}" Value="False">
<Setter Property="ContentTemplate" Value="{StaticResource CCWithoutChildren}" />
</DataTrigger>
<DataTrigger Binding="{Binding HasChildren}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource CCWithChildren}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border x:Name="Bd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<ContentPresenter.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
除了一个案例外,这大部分都有效。如果我从包含子项的下拉列表中选择一个项目,则所选项目在TextBox区域中以黑色显示,而不像在WHITE中正确显示的子项目。我该如何解决这个问题?