
Controlling Type / Member Naming and Visibility
The x:ClassModifier / x:FieldModifer attributes allow you to control the visibility of a member in the related
code file. A XAML file is often paired with a C# or VB code file where you author event handlers, helper
methods, and so forth. An additional compile-time generated code file (*.g.cs / *.g.vb) will be used to contain
the XAML => code object mapping, control declarations, and more.
If you do not make use of the x:ClassModifer / x:FieldModifer attributes, the item will be defined using the
default visibility of the .NET language. In most cases, you will not need to change these defaults. As a result,
you will not frequently need to make use of the x:ClassModifier or x:FieldModifer tokens.
As an example, however, consider the following use of the x:ClassModifier and x:FieldModifier attributes.
This will be used by msbuild.exe to declare a code file containing an internal class with a public Button. The
VB code would be similar, using the Friend keyword rather than the C#-specific internal keyword.
<!-- This class will now be internal.
If using a code file, the partial class must
also be defined as internal. -->
<Window x:Class = "MyWPFApp.MainWindow" x:ClassModifier="internal"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml">
<!-- This button will be public in the generated code file. -->
<Button x:Name = "myButton" x:FieldModifier = "public">
OK
</Button>
</Window>
// C# code (VB code would be similar)
internal partial class MainWindow : System.Windows.Window,
System.Windows.Markup.IComponentConnector
{
public System.Windows.Controls.Button myButton;
…
}
Notice that the previous <Button> has been defined using the x:Name attribute. When you are declaring an
item in XAML, you should always assign a value to the x:Name attribute. This becomes the name of the
variable in the code file.
Because the need to define names for elements is so common, XAML provides a shortcut. You do not need
to use the x: tag explicitly when assigning the Name attribute. Therefore, the previous <Button> could also
be defined as follows:
<Button Name = "myButton" x:FieldModifier = "public">
OK
</Button>
Property-element Syntax
Within the scope of an opening element, you will be able to set values to the properties and events of the
type. For example, the following <Button> sets the Height and Width values and handles the Click event.
The name assigned to the Click event will typically map to a method in your code file. However, this could
also be a method in a <x:Code> scope of the defining XAML file.
<Button Name = "myButton" Height = "100" Width = "100"
Click = "myButton_Click">
OK
</Button>
Here, Height, Width, and Click were assigned values that could be captured as a simple string. However,
many properties of WPF classes do not operate on data that represents simple string values. For example,
some properties require full-blown objects (complex brushes, pens, and so on).
XAML property-element syntax allows you to assign complex object values to a property. This syntax
defines a subscope scope representing a property of the defining type. Within this scope, you can describe
the object to be used for the property assignment. The format follows the following template:
<DefiningType>
<DefiningType.PropertyOnDefiningType>
<!-- data used to set property -->
</DefiningType.PropertyOnDefiningType>
</DefiningType>
Consider the following syntax, which sets the Background property of a Button to a LinearGradientBrush
type using property-element syntax. The associated image shows you how this button would look when
rendered by a XAML parser.
<Button Name = "myButton" Height = "100"
Width = "100" Content = "Click Me!">
<Button.Background>
<LinearGradientBrush StartPoint = "0,0" EndPoint = "1,1">
<GradientStop Color = "Blue" Offset = "0" />
<GradientStop Color = "Yellow" Offset = "0.25" />
<GradientStop Color = "Green" Offset = "0.75" />
<GradientStop Color = "Pink" Offset = "0.50" />
</LinearGradientBrush>
</Button.Background>
</Button>
While property-element syntax is most commonly used to assign complex objects to properties, it is possible
to assign simple string data too. You do not gain much by doing so, however, as you could simply use an
attribute in the opening element. Thus, the following markup is functionally equivalent:
<!-- OK, but verbose -->
<Button x:Name = "myButton" Height = "100"
Width = "100" Content = "Click Me!">
<Button.Background>
Pink
</Button.Background>
</Button>
<Button x:Name = "myButton" Height = "100"
Width = "100" Content = "Click Me!" Background = "Pink">
</Button>
Attached-property Syntax
WPF also makes use of a concept termed ‘attached property’ syntax. One use of attached properties is to
make it possible for a sub-element to assign a property value on a parent element. In this case, the template
to use looks like the following:
<ParentType>
<ChildType ParentType.PropertyName = "Value">
</ChildType>
</ParentType>
The most common use of attached-property syntax is to position UI elements within one of the WPF panel
types (Grid, DockPanel, and so on). You will dive into these panels in some detail later. However, here is an
example of attached-property syntax.
<Window x:Class = "FunWithAttachedPropeties.Window1"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "Fun with Attached Properties" Height = "300" Width = "300">
<DockPanel LastChildFill = "True">
<!-- Dock items to the panel using attached properties. -->
<Label DockPanel.Dock = "Top" Name = "lblInstruction"
FontSize = "15" Content = "Label One"/>
<Label DockPanel.Dock = "Left" Name = "lblMake"
Content = "Label Two"/>
<Label DockPanel.Dock = "Right" Name = "lblColor"
Content = "Label Three"/>
<Label DockPanel.Dock = "Bottom" Name = "lblPetName"
Content = "Label Four"/>
<Button Name = "btnOK">OK</Button>
</DockPanel>
</Window>
Understand that attached properties cannot be used as a general-purpose syntax that can be used to set any
property on a parent element. The IntelliSense of Visual Studio will show you valid attached properties for a
given element.
In reality, attached properties are a specialized form of a WPF-specific concept termed dependency property.
Thus, attached-property syntax can only work if the property supports the correct dependency property
infrastructure. You will learn more about this infrastructure later in the class.
Controlling Type / Member Naming and Visibility
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services