views:

26

answers:

1
<Border Name="ItemBorder" Margin="5 5 0 5" BorderBrush="Black" BorderThickness="1" Height="75" Width="75">
  <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?

+3  A: 

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.

Justin Niessner