Share via


Add Behaviors to Objects in XAML (Compact 2013)

3/26/2014

To apply a behavior to an object in XAML, you can copy and paste the XAML code provided in documentation for each specific behavior into your XAML for Windows Embedded application, and then modify the attributes to identify the source object, target object, the triggering event, and the action to take.

To include the behavior classes in your application

In order to use behaviors in your XAML for Windows Embedded application, you must include the namespace declarations in the UserControl element of your XAML file, as shown in the following example.

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 
xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions"

For example, after you paste the previous lines into the UserControl element of your XAML file, the beginning of your XAML might resemble the following example.

<UserControl
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
  xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 
  xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions"
  x:Class="SilverlightProject.MainPage"
  Width="640" Height="480">
...

These lines cause the Interactivity namespaces to be included in your project, and therefore make the behaviors available to your XAML. You don’t need to use the identifiers “i”, “ic”, and “il”, but these prefixes are used in the code examples in the following sections. If you choose to use different prefixes, make sure to change them in the code examples.

To apply a behavior to an object

After you include the behavior classes at the top of your XAML file, you can add XAML elements for behaviors. For example, the following XAML adds a ChangePropertyAction behavior to a button that will change the color of a rectangle, as shown in the following example.

<!-- This collection element might exist if you have already added other behaviors. -->
    <i:Interaction.Triggers>
    
      <!-- Specify the name of the object (button) whose event will cause the property change.
           Also specify which event will trigger the change (Click). -->
      <i:EventTrigger SourceName="button" EventName="Click">
      
        <!-- Specify the name of the object (rectangle) whose property will change.
             Also specify which property will change (Fill). -->
        <ic: ChangePropertyAction TargetName="rectangle" PropertyName="Fill">
        
          <!-- Specify the new value that will be applied to the property.
               In this case, the value is a color, and the property type is a solid color brush. -->
          <ic:ChangePropertyAction.Value>
            <SolidColorBrush Color="#FF2667D8"/>
          </ic:ChangePropertyAction.Value>
          
        </ic:ChangePropertyAction>

      </i:EventTrigger>

    </i:Interaction.Triggers>

    <!-- Here is the button that will trigger the property change when clicked. -->
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top"/>

    <!-- Here is the rectangle whose Fill property will be changed. -->
    <Rectangle x:Name="rectangle" Fill="#FFBC0F0F" HorizontalAlignment="Left"
        Width="200" Height="100" VerticalAlignment="Top" Margin="0,30,0,0"/>

Tip

The previous code is included in the ChangePropertyAction Behavior topic. The topics for the other supported behaviors include code snippets as well.

See Also

Concepts

Behaviors, Actions, and Triggers