简体   繁体   English

文本不在WPF ComboBox中显示

[英]Text not displaying in WPF ComboBox

I almost perfected my Combobox, its working 98%. 我几乎完善了我的Combobox,它的工作率为98%。 The only thing missing is the highlighted color on an item. 唯一缺少的是项目上突出显示的颜色。 The moment I set this I get the desired highlighted color, but it displays no text. 我设置它的那一刻,我得到了所需的突出显示颜色,但它没有显示任何文字。 When I comment the highlight part out it works 100%, only the highlightcolor is wrong. 当我评论高亮部分它100%工作时,只有高亮颜色是错误的。 Can someone see what is wrong. 有人可以看出出了什么问题。

This is my complete xaml: 这是我完整的xaml:

<SolidColorBrush x:Key="ComboBoxNormalBorderBrush" Color="#B9B9B9" />
<SolidColorBrush x:Key="ComboBoxNormalBackgroundBrush" Color="#E1E1E1" />
<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 ComboBoxNormalBackgroundBrush}" />
        <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 ComboBoxNormalBackgroundBrush}" />
        <Path Name="Arrow" Grid.Column="1" 
        Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
        HorizontalAlignment="Center" Fill="#444"
        VerticalAlignment="Center" />
    </Grid>
    <ControlTemplate.Triggers>
    <Trigger Property="UIElement.IsMouseOver" Value="True">
            <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="#E5F1FB"/>
            <Setter Property="Panel.Background" TargetName="Border" Value="#E5F1FB"/>
            <Setter Property="BorderBrush" TargetName="Border" Value="#0078D7"/>
        </Trigger>
        <Trigger Property="ToggleButton.IsChecked" Value="True">
            <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="#CCE4F7"/>
            <Setter Property="Panel.Background" TargetName="Border" Value="#CCE4F7"/>
            <Setter Property="BorderBrush" TargetName="Border" Value="#3176AF"/>
        </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="{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="Height" Value="22"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Margin" Value="3"/>
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <ToggleButton Name="ToggleButton" Grid.Column="2"
            ClickMode="Press" Focusable="False"
            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"                     
                 IsReadOnly="{TemplateBinding IsReadOnly}"
                 Visibility="Hidden" Background="Transparent"
                 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="White" Margin="0, 1, 0, 0"
                    CornerRadius="0" BorderThickness="1,1,1,1" 
                    BorderBrush="#0078D7"/>
                            <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>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ComboBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ComboBoxItem">
                            <Border Background="{TemplateBinding Background}" BorderThickness="0" Margin="2">
                                <TextBlock Text="{TemplateBinding Content}" Margin="2" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{x:Static SystemColors.HighlightBrush}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

Resulting this (text in list missing): 结果这个(列表中的文字丢失):
在此输入图像描述

Onece you comment out the last setter (HighlightColor), this piece: Onece你注释掉最后一个setter(HighlightColor),这件作品:

<Setter Property="ItemContainerStyle">
    <Setter.Value>
        <Style TargetType="ComboBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBoxItem">
                        <Border Background="{TemplateBinding Background}" BorderThickness="0" Margin="2">
                            <TextBlock Text="{TemplateBinding Content}" Margin="2" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{x:Static SystemColors.HighlightBrush}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Setter.Value>
</Setter>

Resulting this (Text there, but highligtcolor wrong): 结果这个(文本在那里,但highligtcolor错误):
在此输入图像描述
Why is my text missing once I set the desired Highlight color? 设置所需的高光颜色后,为什么我的文字丢失了?

Instead of using "#3399ff" try with "#663399ff". 而不是使用“#3399ff”尝试使用“#663399ff”。 Adding opacity will make colors transparent and they wont cover your text. 添加不透明度将使颜色透明,并且它们不会覆盖您的文本。 Also you may find this useful http://yolijn.com/convert-rgba-to-rgb to calculate resulting color from background color and transparent color over it. 您也可以找到这个有用的http://yolijn.com/convert-rgba-to-rgb来计算背景颜色和透明颜色的结果颜色。

The Opocity thing did not work for me. Opocity的事情对我不起作用。 This also was not the problem, because I should have then seen some text if even if it was not highlighted. 这也不是问题所在,因为即使没有突出显示,我也应该看到一些文本。 This was not the case. 此情况并非如此。 So I had to fiddle for quite a while, whilest not knowing what I'm doing ;-). 所以我不得不捣乱了一段时间,而不知道我在做什么;-)。 By replacing: 通过替换:

<TextBlock Text="{TemplateBinding Content}" Margin="2" />

with

<ContentPresenter></ContentPresenter>

solved the problem for me. 为我解决了这个问题。 Hope this small thing will help someone else. 希望这件小事可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM