Difference between TargetType=“controlType” and TargetType=“{x:Type controlType}”

ぐ巨炮叔叔 提交于 2019-11-26 16:52:38

问题


In WPF you can set the TargetType to either the name of the type or you can set it to {x:Type nameOfType}.

Does anyone know what the difference is?


回答1:


Nothing. Since the property type is Type, the XAML parser knows to try and convert whatever you supply to a Type. In other scenarios, the property type might be less specific (eg. Object), and that's where you need the markup extension, otherwise the XAML parser will just interpret your value as a String.




回答2:


Sorry for poking such an old thread, but I feel it's worth it. I have recently encountered a situation which shows that x:Type is different from TypeName-as-String.

From my experience -

x:Type considers the strong name or the version of the assembly but not TypeName-as-String.

I have explained about my scenario and other details in my blog here -

Importance of specifying AncestorType with x:Type in RelativeSourceBinding

Apart from this, there is also difference in how WPF infers the type. For x:Type TypeExtension is used, whereas for TypeName-as-String FrameworkElementFactory is used.

As per MSDN - x:Type Markup Extension

Type Properties That Support Typename-as-String

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.TargetType. Support for this behavior is not provided through either type converters or markup extensions. Instead, this is a deferral behavior implemented through FrameworkElementFactory.




回答3:


Apart from the real difference described above, using {x:Type NameOfType} obviously will be highlighted differently in your IDE and makes it stand out and spot right away (as opposed to a regular string). So this is my personal preference.



来源:https://stackoverflow.com/questions/1085730/difference-between-targettype-controltype-and-targettype-xtype-controltype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!