In WPF, what are the differences between the x:Name and Name attributes?

前端 未结 15 1246
闹比i
闹比i 2020-11-22 05:19

The title says it all. Sometimes it seems that the Name and x:Name attributes are interchangeable.

So, what are the definitive differences

15条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 05:42

    Name:

    1. can be used only for descendants of FrameworkElement and FrameworkContentElement;
    2. can be set from code-behind via SetValue() and property-like.

    x:Name:

    1. can be used for almost all XAML elements;
    2. can NOT be set from code-behind via SetValue(); it can only be set using attribute syntax on objects because it is a directive.

    Using both directives in XAML for one FrameworkElement or FrameworkContentElement will cause an exception: if the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.

提交回复
热议问题