What is the difference for
TargetType="{x:Type Button}"
and
TargetType="Button"
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With