如何设置 WPF ComboBox 的样式

bir*_*tri 5 wpf combobox styles

我想修改组合框的默认样式。所以我写了这段代码:

        <!-- ComboBox style -->
        <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <ToggleButton Name="ToggleButton" 
                    Template="{StaticResource ComboBoxToggleButton}" 
                    IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                    ClickMode="Press">
                            </ToggleButton>
                            <ContentPresenter Name="ContentSite" IsHitTestVisible="False" 
                        Content="{TemplateBinding SelectionBoxItem}"
                        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                        Margin="3,3,23,3"
                            <TextBox x:Name="PART_EditableTextBox"
                        Style="{x:Null}"  Template="{StaticResource ComboBoxTextBox}" 
                        Text="{TemplateBinding Text}"
                        Foreground="DarkBlue"
                        IsReadOnly="{TemplateBinding IsReadOnly}"/>
                            <Popup  Name="Popup" Placement="Bottom"
                        IsOpen="{TemplateBinding IsDropDownOpen}"
                        AllowsTransparency="True"  Focusable="False"
                        PopupAnimation="Slide">
                                <Grid  Name="DropDown"
                          SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}"
                          MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                    <Border  x:Name="DropDownBorder"/>
                                    <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.Triggers>
        </Style>
Run Code Online (Sandbox Code Playgroud)

这是我在 XAML 文件中的组合框,但我不知道如何设置我的样式:

<ComboBox x:Name="nomiGiocatori" /> 
Run Code Online (Sandbox Code Playgroud)

我如何应用这个 ComboBox 的新 Stype?

警告

Stí*_*ndr 5

你的风格有点不对,应该是

<Style x:Key="YourButtonStyle" TargetType="{x:Type Button}">
Run Code Online (Sandbox Code Playgroud)

在你的 xaml 代码中

<ComboBox x:Name="nomiGiocatori" Style="{StaticResource YourButtonStyle}"/> 
Run Code Online (Sandbox Code Playgroud)

如果您将样式定义为,则可以将其应用于所有组合框

<Style TargetType="{x:Type Button}" BasedOn="Button"/>
Run Code Online (Sandbox Code Playgroud)

然后你不必对你的 xaml 做任何事情。

这个答案描述了一个类似的问题。

希望能帮助到你,

斯蒂安