Share via


x:FieldModifier Attribute

Modifies XAML compilation behavior, such that fields for named object references are defined with TypeAttributes.Public access rather than the TypeAttributes.NotPublic default behavior.

XAML Attribute Usage

<object x:FieldModifier="Public".../>

XAML Values

Public

The exact string to pass to specify TypeAttributes.Public versus TypeAttributes.NotPublic varies, depending on the code-behind programming language being used. See Remarks.

Dependencies

x:Name must also be provided on the same element.

Remarks

x:FieldModifier is not relevant for declaring the general access level of a class or its members. It is relevant only for the XAML processing behavior when a particular XAML element that is part of a XAML UI definition is processed, and becomes an object that is potentially viewable in the object tree of instantiated controls that use that XAML element in their default UI. By default, the field reference for such an object is kept private, which prevents control consumers from modifying the object tree directly. Instead, control consumers are expected to modify the object tree using standard patterns such as obtaining the layout root, child element collections, dedicated public properties, etc.

The value for the x:FieldModifier attribute will vary by programming language. The string to use will depend on how each language implements its CodeDomProvider and the type converters it returns to define the meanings for TypeAttributes.Public and TypeAttributes.NotPublic, and whether that language is case sensitive or not.

  • For C#, the string to pass to designate TypeAttributes.Public is public.

  • For Microsoft Visual Basic .NET, the string to pass to designate TypeAttributes.Public is Public.

  • For C++/CLI, the C++/CLI targets do not currently support compiling XAML.

You can also specify TypeAttributes.NotPublic (internal in C#, Friend in Microsoft Visual Basic .NET) but that is uncommon, because NonPublic as the behavior is already the default.

TypeAttributes.NotPublic is the default because it is uncommon that code outside of the assembly that compiled the XAML would have any need to access a XAML created element. The WPF security architecture and XAML compilation behavior in combination will not make fields that store element instances public, unless you specifically set the x:FieldModifier.

x:FieldModifier is only relevant for elements with an x:Name Attribute, because that name is used to reference the field once it is public.

The partial class for the root element is public by default, but can be made nonpublic by using the x:ClassModifier Attribute. The x:ClassModifier Attribute also affects the access level of the instance of the root element class. You can place both x:Name and x:FieldModifier on the root element, but this only makes a public field copy of the root element, with the true root element class access level still controlled by x:ClassModifier Attribute.

See Also

Concepts

XAML and Custom Classes

Code-Behind and XAML

Building a WPF Application (WPF)

Reference

x:Name Attribute

x:ClassModifier Attribute