Reputation: 2169
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
Upvotes: 5
Views: 7349
Reputation: 57
Thanks for your suggestion but I don't think you can apply a style with a target type Button to ComboBox object. I have reviewed your suggestion and it doesn't work in anywhere in WFP implementation. Even if you use BasedOn="Button" or "ComboBox". The compiler generates the error message TypeConverter for "Style" does not support converting from a string.
ComboBox basically has three sections: ComboBox, ComboBoxItems and ToggleButton elements. To override the default settings, you have to define these three styles first with property type as template and also setting the OverridesDefaultStyle to true.
Upvotes: 0
Reputation: 2902
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
Upvotes: 5