x:FieldModifier Directive

Modifies XAML compilation behavior so that fields for named object references are defined with TypeAttributes.Public access instead of the TypeAttributes.NotPublic default behavior.

XAML Attribute Usage

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

XAML Values

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


If a XAML production uses x:FieldModifier anywhere, the root element of that XAML production must declare an x:Class Directive.


x:FieldModifier is not relevant for declaring the general access level of a class or its members. It is relevant only for XAML-processing behavior when a particular XAML object that is part of a XAML production is processed, and becomes an object that is potentially accessible in the object graph of an application. By default, the field reference for such an object is kept private, which prevents control consumers from modifying the object graph directly. Instead, control consumers are expected to modify the object graph by using standard patterns that are enabled by programming models, such as by obtaining the layout root, the child element collections, the dedicated public properties, and so on.

The value for the x:FieldModifier attribute varies by programming language, and its purpose can vary in specific frameworks. The string to use depends 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.

  • 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, no targets for XAML currently exist; therefore, the string to pass is undefined.

You can also specify TypeAttributes.NotPublic (internal in C#, Friend in Visual Basic) but specifying TypeAttributes.NotPublic is unusual because NotPublic as the behavior is already the default.

TypeAttributes.NotPublic is the default behavior because it is infrequent that code outside the assembly that compiled the XAML needs access to a XAML-created element. WPF security architecture together with XAML compilation behavior will not declare fields that store element instances as public, unless you specifically set the x:FieldModifier to allow public access.

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

By default, the partial class for the root element is public; however, you can make it nonpublic by using the x:ClassModifier Directive. The x:ClassModifier Directive also affects the access level of the instance of the root element class. You can put 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 Directive.

See also