繁体   English   中英

WPF组合框文本不可见

[英]WPF Combobox Text not visible

我已经尝试了一段时间,似乎找不到问题。 我的样式是完美的,但组合框未在主栏中显示所选的项目(如您在嵌入的图像中所看到的)。 下拉列表文本是可见的,但未显示所选项目。

我对XAML的了解有限,所以我希望有人能够发现我所犯的错误。

<Window.Resources>
    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#3a3a3a" />

    <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="20" />
            </Grid.ColumnDefinitions>
            <Border
                x:Name="Border"
                Grid.ColumnSpan="2"
                Background="{StaticResource WindowBackgroundBrush}"
                BorderBrush="#4a4a4a"
                BorderThickness="1" />
            <Border
                Grid.Column="0"
                Margin="1"
                Background="{StaticResource WindowBackgroundBrush}" />
            <Path
                x:Name="Arrow"
                Grid.Column="1"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Data="M 0 0 L 4 4 L 8 0 Z"
                Fill="White" />
        </Grid>
    </ControlTemplate>

    <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
        <Border x:Name="PART_ContentHost" Focusable="False" />
    </ControlTemplate>

    <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton
                            Name="ToggleButton"
                            Grid.Column="2"
                            ClickMode="Press"
                            Focusable="false"
                            IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                            Template="{StaticResource ComboBoxToggleButton}" />
                        <ContentPresenter
                            Name="ContentSite"
                            Margin="3,3,23,3"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Center"
                            IsHitTestVisible="False" />
                        <TextBox
                            x:Name="PART_EditableTextBox"
                            Margin="3,3,23,3"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Center"
                            Focusable="True"
                            IsReadOnly="{TemplateBinding IsReadOnly}"
                            Style="{x:Null}"
                            Template="{StaticResource ComboBoxTextBox}"
                            Visibility="Hidden" />
                        <Popup
                            Name="Popup"
                            AllowsTransparency="True"
                            Focusable="False"
                            IsOpen="{TemplateBinding IsDropDownOpen}"
                            Placement="Bottom"
                            PopupAnimation="Slide">
                            <Grid
                                Name="DropDown"
                                MinWidth="{TemplateBinding ActualWidth}"
                                MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                SnapsToDevicePixels="True">
                                <Border
                                    x:Name="DropDownBorder"
                                    Background="{StaticResource WindowBackgroundBrush}"
                                    BorderThickness="1" />
                                <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers />
    </Style>
</Window.Resources>

        <ComboBox
            Name="cbxEmployees"
            Width="Auto"
            Margin="10,0,0,0"
            VerticalContentAlignment="Center"
            Foreground="White"
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding Employees}"
            SelectedItem="{Binding SelectedEmployee}"
            SelectionChanged="cbxEmployees_SelectionChanged" />

选择项目后,这是空栏。

尝试将ContentTemplate绑定到模板的SelectionBoxItem,如下所示:

<ContentPresenter
  Name="ContentSite"
  Margin="3,3,23,3"
  HorizontalAlignment="Left"
  VerticalAlignment="Center"
  IsHitTestVisible="False"                                 
  Content="{TemplateBinding SelectionBoxItem}" />

SelectionBoxItem包含要在组合框上显示的对象。 如果您覆盖控件的默认模板而未将该prop绑定到某处,则它将丢失。

暂无
暂无

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

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