Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference for TargetType="{x:Type Button}" and TargetType="Button"?

What is the difference for

TargetType="{x:Type Button}" 

and

TargetType="Button" 
like image 579
DEN Avatar asked Nov 26 '12 03:11

DEN


People also ask

What is X type WPF?

WPF supports techniques that enable specifying the value of some properties of type Type without requiring an x:Type markup extension usage. Instead, you can specify the value as a string that names the type. Examples of this are ControlTemplate. TargetType and Style.

How to inherit style in wpf?

When you inherit a style from an existing style, the settings from a parent style are available in the inherited style. To inherit a style from another style, we set the BasedOn property to StaticResource Markup Extension as the style it is being inherited from.


1 Answers

The XAML designer applies inbuilt type converters that convert the string value "Button" to System.Type which is Button , which makes it seem like there is no practical difference.

However one should practise to use the explicit Type specification using x:Type.

Explicit Type specification is required is when we inherit Styles using BasedOn, there implicit string Type wont work.

e.g.

This would work

 BasedOn="{StaticResource {x:Type Button}}" 

But not this...

 BasedOn="{StaticResource Button}" 

as here it would try to search a resource with Key "Button". But in the x:Type specification, as we already specified explicit Button Type the search of the static resource would be happen for the Style which is targetted for a Button.

like image 166
WPF-it Avatar answered Sep 23 '22 02:09

WPF-it