开发者

What is the meaning of x:Name in xaml (WPF) and its use with Storyboard

<Border Name="ItemBorder" Margin="5 5 0 5" BorderBrush="Black" BorderThickness="1" Height="75" Width="75"开发者_如何学Go>
  <Border.Background>
    <SolidColorBrush x:Name="ItemBorderBrush" Color="LightBlue"/>
  </Border.Background>
  <ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
  <EventTrigger RoutedEvent="someEvent">
    <BeginStoryboard>
      <Storyboard TargetName="ItemBorderBrush" TargetProperty="Color" Duration="0:0:1" >
      <!--Storyboard TargetName="ItemBorder" TargetProperty="Background.Color" Duration="0:0:1"> -->
         <ColorAnimation To="White"/>
      </Storyboard>
    </BeginStoryboard>
  </EventTrigger>
</ControlTemplate.Triggers>

I'll try to explain my question clearly. The Storyboard Target name, when it is "ItemBorder" (the commented out line) works intermittently. Sometimes I get an error that the name "ItemBorder" cannot be found in the scope.

I decided to follow a style from an MSDN example of this, and change the color property directly on the brush, instead of having the target of the storyboard be the border, and changing the color of the border's brush by property (the commented out line). This seems to work.

However, Name="ItemBorderBrush" does not compile because Name is not a property of SolidColorBrush so I use x:Name="ItemBorderBrush" Both Name and x:Name are accepted for the Border. Why is this?

What does the x: mean (how is x:Name different from Name), and why would having the Name property of border only work with the storyboard sometimes?


The x: prefix is simply setting an attribute from a seperate namespace:

Within the namespace declarations in the root tag of many XAML files, you will see that there are typically two XML namespace declarations. The first declaration maps the overall WPF client / framework XAML namespace as the default:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

The second declaration maps a separate XAML namespace, mapping it (typically) to the x: prefix.

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

The relationship between these declarations is that the x: prefix mapping supports the intrinsics that are part of the XAML language definition, and WPF is one implementation that uses XAML as a language and defines a vocabulary of its objects for XAML. Because the WPF vocabulary's usages will be far more common than the XAML intrinsics usages, the WPF vocabulary is mapped as the default

So, the reason that Name and x:Name both work on Border is because Border has a property called Name. It also supports the XAML Intrinsic usage of x:Name (which is what WPF uses to create the named instance of the class).

However, SolidColorBrush doesn't have the property called Name so it only supports the XAML Intrinsic usage of x:Name.


There really is only one name in XAML, the x:Name. A framework, such as WPF, can optionally map one of its properties to XAML's x:Name by using the RuntimeNamePropertyAttribute on the class that designates one of the classes properties as mapping to the x:Name attribute of XAML.

The reason this was done was to allow for frameworks that already have a concept of "Name" at runtime, such as WPF. In WPF, for example, FrameworkElement introduces a Name property.

In general, a class does not need to store the name for x:Name to be useable. All x:Name means to XAML is generate a field to store the value in the code behind class. What the runtime does with that mapping is framework dependent.

So, why are there two ways to do the same thing? The simple answer because there are two concepts mapped onto one property. WPF wants the name of an element preserved at runtime (which is usable through Bind, among other things) and XAML needs to know what elements you want to be accessible by fields in the code behind class. WPF ties these two together by marking the Name property as an alias of x:Name.

In the future, XAML will have more uses for x:Name, such as allowing you to set properties by referring to other objects by name, but in 3.5 and prior, it is only used to create fields.

Whether you should use one or the other is really a style question, not a technical one. I will leave that to others for a recommendation.

See also AutomationProperties.Name VS x:Name, AutomationProperties.Name is used by accessibility tools and some testing tools.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜