How to set WPF ComboBox style

I would like to change the default style for comboBox. Therefore, I am writing this code:

        <!-- 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>

This is my comboBox in a XAML file, but I don't know how to set my style:

<ComboBox x:Name="nomiGiocatori" /> 

How do I apply the new Stype of this ComboBox?

Reguards

+4
source share
2 answers

Your style is a little wrong, it should be

<Style x:Key="YourButtonStyle" TargetType="{x:Type Button}">

And in your xaml code

<ComboBox x:Name="nomiGiocatori" Style="{StaticResource YourButtonStyle}"/> 

You can apply it to all lists if you define your style as

<Style TargetType="{x:Type Button}" BasedOn="Button"/>

Then you do not need to do anything with your xaml.

This answer describes a similar problem.

Hope this helps,

Stian

+4
source

, , Button ComboBox. , . BasedOn = "Button" "ComboBox". TypeConverter "Style" .

ComboBox : ComboBox, ComboBoxItems ToggleButton. , , OverridesDefaultStyle true.

0

All Articles