Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义组合框样式不显示SelectedItem_C#_Wpf_Combobox_Styles - Fatal编程技术网

C# 自定义组合框样式不显示SelectedItem

C# 自定义组合框样式不显示SelectedItem,c#,wpf,combobox,styles,C#,Wpf,Combobox,Styles,我正在设置一个组合框的样式,我只是复制粘贴默认样式,并且已经更改了一些内容我的问题是,SelectedItem没有显示 也许xaml大师可以帮我找出错误。 组合框将不可编辑,因此我猜问题出在内容站点中,但我不太确定如何使其工作 提前谢谢 编辑: 我已经检查过是否有一个SelectedItem,如果我使用一个普通的组合框,则会显示该项。 当前样式: <ControlTemplate x:Key="ToggleButton" TargetType="{x

我正在设置一个组合框的样式,我只是复制粘贴默认样式,并且已经更改了一些内容
我的问题是,
SelectedItem
没有显示

也许xaml大师可以帮我找出错误。
组合框
将不可编辑,因此我猜问题出在
内容站点
中,但我不太确定如何使其工作

提前谢谢

编辑: 我已经检查过是否有一个
SelectedItem
,如果我使用一个普通的组合框,则会显示该项。 当前样式:

<ControlTemplate x:Key="ToggleButton"
                    TargetType="{x:Type ToggleButton}">
        <Grid>
                <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="50" />
                </Grid.ColumnDefinitions>
                <Border x:Name="Border"
                        Grid.ColumnSpan="2"
                        Background="{StaticResource ColorMagnoscoGreen}"
                        BorderBrush="#FF97A0A5"
                        BorderThickness="1"
                        CornerRadius="0" />
                <Border Grid.Column="0"
                        Margin="1"
                        Background="White"
                        BorderThickness="1"
                        CornerRadius="0" />
                <Path x:Name="Arrow"
                        Grid.Column="1"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
                        Fill="White" />
        </Grid>
</ControlTemplate>
<ControlTemplate x:Key="ComboBoxTextBox"
                    TargetType="{x:Type TextBox}">
        <Border x:Name="PART_ContentHost"
                BorderThickness="1"
                Focusable="False">
        </Border>
</ControlTemplate>

<Style x:Key="{x:Type ComboBox}"
        TargetType="{x:Type ComboBox}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
        <Setter Property="MinWidth" Value="120" />
        <Setter Property="MinHeight" Value="40" />
        <Setter Property="Template">
                <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ComboBox}">
                                <Grid>
                                        <ContentPresenter x:Name="ContentSite"
                                                            IsHitTestVisible="False"
                                                            Content="{TemplateBinding SelectionBoxItem}"
                                                            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                            Margin="3,3,23,3"
                                                            VerticalAlignment="Stretch"
                                                            HorizontalAlignment="Left">
                                        </ContentPresenter>
                                        <TextBox x:Name="PART_EditableTextBox"
                                                    Style="{x:Null}"
                                                    Template="{StaticResource ComboBoxTextBox}"
                                                    HorizontalAlignment="Left"
                                                    VerticalAlignment="Bottom"
                                                    Margin="3,3,23,3"
                                                    Focusable="True"
                                                    Visibility="Hidden"
                                                    IsReadOnly="{TemplateBinding IsReadOnly}" />
                                        <Popup Name="Popup"
                                                AllowsTransparency="False"
                                                Focusable="False"
                                                IsOpen="{TemplateBinding IsDropDownOpen}"
                                                Placement="Bottom"
                                                PopupAnimation="Slide">
                                                <Grid Name="DropDown"
                                                        Background="White"
                                                        MinWidth="{TemplateBinding ActualWidth}"
                                                        MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                                        SnapsToDevicePixels="True">
                                                        <Border x:Name="DropDownBorder"
                                                                BorderBrush="#888888"
                                                                BorderThickness="1" />
                                                        <ScrollViewer Margin="4,6,4,6"
                                                                        SnapsToDevicePixels="True">
                                                                <StackPanel IsItemsHost="True"
                                                                            KeyboardNavigation.DirectionalNavigation="Contained" />
                                                        </ScrollViewer>
                                                </Grid>
                                        </Popup>
                                        <ToggleButton Name="ToggleButton"
                                                        Grid.Column="2"
                                                        ClickMode="Press"
                                                        Focusable="false"
                                                        IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                                        Template="{StaticResource ComboBoxToggleButton}" />
                                </Grid>
                                <ControlTemplate.Triggers>
                                        <Trigger Property="HasItems" Value="false">
                                                <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" />
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="false">
                                                <Setter Property="Foreground" Value="#888888" />
                                        </Trigger>
                                        <Trigger Property="IsGrouping" Value="true">
                                                <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                                        </Trigger>
                                        <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                                                <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="0" />
                                                <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0" />
                                        </Trigger>
                                        <Trigger Property="IsEditable" Value="true">
                                                <Setter Property="IsTabStop" Value="false" />
                                                <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible" />
                                                <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden" />
                                        </Trigger>
                                </ControlTemplate.Triggers>
                        </ControlTemplate>
                </Setter.Value>
        </Setter>
</Style>
<Style x:Key="{x:Type ComboBoxItem}"
        TargetType="{x:Type ComboBoxItem}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Template">
                <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                <Border Name="Border"
                                        Padding="2"
                                        SnapsToDevicePixels="true">
                                        <ContentPresenter />
                                </Border>
                                <ControlTemplate.Triggers>
                                        <Trigger Property="IsHighlighted" Value="true">
                                                <Setter TargetName="Border" Property="Background" Value="{StaticResource ColorMagnoscoGreen}" />
                                        </Trigger>
                                </ControlTemplate.Triggers>
                        </ControlTemplate>
                </Setter.Value>
        </Setter>
</Style>

控制模板中,
切换按钮
隐藏
内容演示器
。将
ContentPresenter
移动到
切换按钮下方

<Grid>
    <TextBox x:Name="PART_EditableTextBox"
            Style="{x:Null}"
            Template="{StaticResource ComboBoxTextBox}"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            Margin="3,3,23,3"
            Focusable="True"
            Visibility="Hidden"
            IsReadOnly="{TemplateBinding IsReadOnly}" />
    <Popup Name="Popup"
            AllowsTransparency="False"
            Focusable="False"
            IsOpen="{TemplateBinding IsDropDownOpen}"
            Placement="Bottom"
            PopupAnimation="Slide">
        <Grid Name="DropDown"
            Background="White"
            MinWidth="{TemplateBinding ActualWidth}"
            MaxHeight="{TemplateBinding MaxDropDownHeight}"
            SnapsToDevicePixels="True">
            <Border x:Name="DropDownBorder"
                    BorderBrush="#888888"
                    BorderThickness="1" />
            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
            </ScrollViewer>
        </Grid>
    </Popup>
    <ToggleButton Name="ToggleButton"
                Grid.Column="2"
                ClickMode="Press"
                Focusable="false"
                IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                Template="{StaticResource ComboBoxToggleButton}" />
    <ContentPresenter x:Name="ContentSite"
                    IsHitTestVisible="False"
                    Content="{TemplateBinding SelectionBoxItem}"
                    ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                    ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                    Margin="3,3,23,3"
                    VerticalAlignment="Stretch"
                    HorizontalAlignment="Left">
    </ContentPresenter>
</Grid>

我对WPF的了解永远都不够;像往常一样谢谢@mm8,你又救了我一天。