How to assign a style to all objects at once in WPF

Asked

Viewed 81 times

1

It is possible to create a style in WPF where will be using for all components of the same type, so it is not necessary to call the Style for the component?

My idea is to create a style as for example

 <Style TargetType="Button">

 </Style>

And all Buttons that have no style associated carry the style of that site, without it being necessary for me to specify the style key for the button.

In the same way that happens with css, I refer a style to "p" and all "p" receive that style.

  • Already tried to put the style in App.xaml?

  • Yes, but it is necessary to reference with a key is not?

1 answer

0

Global styles in WPF can be done as follows:

<Style TargetType="{x:Type TextBox}">
...
</Style>

That would apply to everyone who’s the type TextBox or inherited from TextBox.

To leave this overall style, put the style inside a ResourceDictionary in the Xaml App..

  • I just tested this by applying a style to all the Row of a datagrid ... <Style TargetType="DataGridRow">&#xA; <Style.Triggers>&#xA; <Trigger Property="IsMouseOver" Value="True">&#xA; <Setter Property="FontWeight" Value="Bold"/>&#xA; </Trigger> &#xA; </Style.Triggers>&#xA; </Style>

  • Put it in a Resourcedictionary?

  • I have several styles within the Resourcedictionary ... I must place it in a Separate Resourcedictionary?

  • No, it would be the same. You even tested your style separately, only on a screen to see if it gives you the expected result?

  • I tested on several screens and nothing

  • And when I put a key it works, I also use the Basedon="{Staticresource {x:Type Datagridrow} when I use with the key, but without the key it doesn’t work

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.