Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set Style of WPF ComboBox

I would like to modify default style of comboBox. So I write 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 XAML file, but I don't know to set my style:

<ComboBox x:Name="nomiGiocatori" /> 

How I apply new Stype of this ComboBox?

Reguards

like image 760
bircastri Avatar asked Sep 17 '14 09:09

bircastri


1 Answers

Your style is a bit 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 comboboxes if you define your style as

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

Then you don't have to do anything with your xaml.

This answer describes a similar issue.

Hope it helps,

Stian

like image 56
Stígandr Avatar answered Oct 17 '22 20:10

Stígandr