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

前端 未结 15 1538
闹比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:48

    They are not the same thing.

    x:Name is a xaml concept, used mainly to reference elements. When you give an element the x:Name xaml attribute, "the specified x:Name becomes the name of a field that is created in the underlying code when xaml is processed, and that field holds a reference to the object." (MSDN) So, it's a designer-generated field, which has internal access by default.

    Name is the existing string property of a FrameworkElement, listed as any other wpf element property in the form of a xaml attribute.

    As a consequence, this also means x:Name can be used on a wider range of objects. This is a technique to enable anything in xaml to be referenced by a given name.

提交回复
热议问题