如何在Combobox中设置第一项的ItemStyle

时间:2018-01-18 12:03:01

标签: wpf xaml combobox

我为组合框设置了一个样式,选择器内的所有项目都是白色的,因为我指定了它。唯一仍为黑色的文字是第一项。我是否需要为第一项设置特定的内容?

enter image description here

这是它的xaml代码:

 <SolidColorBrush x:Key="ComboBoxNormalBorderBrush" Color="#e3e9ef" />
        <SolidColorBrush x:Key="ComboBoxNormalBackgroundBrush" Color="#fff" />
        <SolidColorBrush x:Key="ComboBoxDisabledForegroundBrush" Color="#888" />
        <SolidColorBrush x:Key="ComboBoxDisabledBackgroundBrush" Color="#eee" />
        <SolidColorBrush x:Key="ComboBoxDisabledBorderBrush" Color="#888" />

        <ControlTemplate TargetType="ToggleButton" x:Key="ComboBoxToggleButtonTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="20" />
                </Grid.ColumnDefinitions>
                <Border Grid.ColumnSpan="2" Name="Border"
          BorderBrush="{StaticResource ComboBoxNormalBorderBrush}" 
          CornerRadius="0" BorderThickness="1, 1, 1, 1" 
          Background="{StaticResource MainButtonBackground}" />
                <Border Grid.Column="1" Margin="1, 1, 1, 1" BorderBrush="#444" Name="ButtonBorder"
          CornerRadius="0, 0, 0, 0" BorderThickness="0, 0, 0, 0" 
          Background="{StaticResource MainButtonBackground}"
                                      />

                <Path Name="Arrow" Grid.Column="1" 
                      Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z" 
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center" Fill="WhiteSmoke">
                </Path>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="UIElement.IsMouseOver" Value="True">
                    <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="WhiteSmoke"/>
                    <Setter Property="Shape.Fill" TargetName="Arrow" Value="{StaticResource MainButtonBackground}"/>
                </Trigger>
                <Trigger Property="ToggleButton.IsChecked" Value="True">
                    <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="{StaticResource MainButtonBackground}"/>
                    <Setter Property="Shape.Fill" TargetName="Arrow" Value="WhiteSmoke"/>
                </Trigger>
                <Trigger Property="UIElement.IsEnabled" Value="False">
                    <Setter Property="Panel.Background" TargetName="Border" Value="{StaticResource ComboBoxDisabledBackgroundBrush}"/>
                    <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="{StaticResource ComboBoxDisabledBackgroundBrush}"/>
                    <Setter Property="Border.BorderBrush" TargetName="ButtonBorder" Value="{StaticResource ComboBoxDisabledBorderBrush}"/>
                    <Setter Property="TextElement.Foreground" Value="{StaticResource ComboBoxDisabledForegroundBrush}"/>
                    <Setter Property="Shape.Fill" TargetName="Arrow" Value="#999"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style TargetType="ComboBoxItem">
            <Setter Property="Foreground" Value="WhiteSmoke"/>
            <Setter Property="Background" Value="{StaticResource MainButtonBackground}"/>
        </Style>

        <Style x:Key="ComboBoxFlatStyle"  TargetType="{x:Type ComboBox}">
            <Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
            <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
            <Setter Property="TextElement.Foreground" Value="Black"/>
            <Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <ToggleButton Name="ToggleButton" Grid.Column="2"
            ClickMode="Press" Focusable="False" Background="{StaticResource MainButtonBackground}"
                                          Foreground="{StaticResource WindowForeColor}"
            IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
            Template="{StaticResource ComboBoxToggleButtonTemplate}"/>

                            <ContentPresenter Name="ContentSite" Margin="5, 3, 23, 3" IsHitTestVisible="False"
                          HorizontalAlignment="Left" VerticalAlignment="Center"                              
                          Content="{TemplateBinding ComboBox.SelectionBoxItem}" 
                          ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/>
                            <TextBox Name="PART_EditableTextBox" Margin="3, 3, 23, 3"    Background="{StaticResource MainButtonBackground}"
                                          Foreground="{StaticResource WindowForeColor}"                 
                 IsReadOnly="{TemplateBinding IsReadOnly}"
                 Visibility="Hidden"  
                 HorizontalAlignment="Left" VerticalAlignment="Center"
                 Focusable="True" >
                                <TextBox.Template>
                                    <ControlTemplate TargetType="TextBox" >
                                        <Border Name="PART_ContentHost" Focusable="False"  />
                                    </ControlTemplate>
                                </TextBox.Template>
                            </TextBox>
                            <!-- Popup showing items -->
                            <Popup Name="Popup" Placement="Bottom"
               Focusable="False" AllowsTransparency="True"
               IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
               PopupAnimation="Slide">
                                <Grid Name="DropDown" SnapsToDevicePixels="True"
                MinWidth="{TemplateBinding FrameworkElement.ActualWidth}" 
                MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}">
                                    <Border Name="DropDownBorder" Background="{StaticResource MainButtonBackground}" Margin="0, 1, 0, 0"
                    CornerRadius="0" BorderThickness="1,1,1,1" 
                    BorderBrush="{StaticResource ComboBoxNormalBorderBrush}"/>
                                    <ScrollViewer Margin="4" SnapsToDevicePixels="True">
                                        <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </ScrollViewer>
                                </Grid>
                            </Popup>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ItemsControl.HasItems" Value="False">
                                <Setter Property="FrameworkElement.MinHeight" TargetName="DropDownBorder" Value="95"/>
                            </Trigger>
                            <Trigger Property="UIElement.IsEnabled" Value="False">
                                <Setter Property="TextElement.Foreground" Value="{StaticResource ComboBoxDisabledForegroundBrush}"/>
                            </Trigger>
                            <Trigger Property="ItemsControl.IsGrouping" Value="True">
                                <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
                            </Trigger>
                            <Trigger Property="ComboBox.IsEditable" Value="True">
                                <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                                <Setter Property="UIElement.Visibility" TargetName="PART_EditableTextBox" Value="Visible"/>
                                <Setter Property="UIElement.Visibility" TargetName="ContentSite" Value="Hidden"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我不知道如何设置仍然用黑色书写的项目的前景。会爱一些帮助(=

解决方案编辑:

我不得不更改我的ComboBoxFlatStyle的TextElement.Foreground属性......

<Style x:Key="ComboBoxFlatStyle"  TargetType="{x:Type ComboBox}">
.
.
.
                <Setter Property="TextElement.Foreground" Value="WhiteSmoke"/>
.
.
.
</Style>

0 个答案:

没有答案