<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SHAZAML! &#187; SetInteractionPropertyAction</title>
	<atom:link href="http://www.shazaml.com/archives/tag/setinteractionpropertyaction/feed" rel="self" type="application/rss+xml" />
	<link>http://www.shazaml.com</link>
	<description>The Blog for Design &#38; Development Superheroes</description>
	<lastBuildDate>Wed, 28 Jul 2010 21:02:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Video: Creating a Silverlight 3 Casual Game using Blend 3 and Triggers, Actions, and Behaviors</title>
		<link>http://www.shazaml.com/archives/video-silverlight-casual-game</link>
		<comments>http://www.shazaml.com/archives/video-silverlight-casual-game#comments</comments>
		<pubDate>Mon, 09 Nov 2009 15:19:11 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Hidden Object Game]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[ChangePropertyAction]]></category>
		<category><![CDATA[ContinuousPlayMediaBehavior]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[GlobalCounterMinReachedTrigger]]></category>
		<category><![CDATA[GoToStateAction]]></category>
		<category><![CDATA[IncrementGlobalCounterAction]]></category>
		<category><![CDATA[InvokeCommandAction]]></category>
		<category><![CDATA[MagnifierOverBehavior]]></category>
		<category><![CDATA[MouseCursorBehavior]]></category>
		<category><![CDATA[ParticlesBehavior]]></category>
		<category><![CDATA[PlaySoundAction]]></category>
		<category><![CDATA[RemoveElementAction]]></category>
		<category><![CDATA[SetGlobalCounterAction]]></category>
		<category><![CDATA[SetInteractionPropertyAction]]></category>
		<category><![CDATA[ShowGlobalCounterBehavior]]></category>
		<category><![CDATA[Trigger]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[Visual State Manager]]></category>
		<category><![CDATA[VSM]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=333</guid>
		<description><![CDATA[Video presentation from Desert Code Camp 2009 - Silverlight 3 Casual Game]]></description>
			<content:encoded><![CDATA[<p>On Saturday, November 7, 2009 at 9:00am I presented at the Desert Code Camp in Phoenix, AZ. The topic of my presentation was Silverlight casual game development. In less than an hour I demonstrated how to use triggers, actions, and behaviors in Blend 3 to create a hidden object game. Because all code was contained in the TABs (Triggers, Actions, and Behaviors) there was no code behind for the main UserControl.</p>
<p>Here is a link to the <a title="Video - Creating a Silverlight 3 Casual Game using Blend 3 and Triggers, Actions, and Behaviors" href="http://www.shazaml.com/downloads/DesertCodeCamp2009-Silverlight3CasualGame.wmv">presentation video</a> in WMV format.</p>
<p>If you like you can see <a href="http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3">all the episodes</a> and follow along with the tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/video-silverlight-casual-game/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://www.shazaml.com/downloads/DesertCodeCamp2009-Silverlight3CasualGame.wmv" length="63294285" type="video/x-ms-wmv" />
		</item>
		<item>
		<title>Hidden Object: Episode 7 – Use an Action to Toggle the Magnifier Behavior</title>
		<link>http://www.shazaml.com/archives/hidden-object-episode-7</link>
		<comments>http://www.shazaml.com/archives/hidden-object-episode-7#comments</comments>
		<pubDate>Sat, 26 Sep 2009 13:03:42 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Hidden Object Game]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[BaseBehavior]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[MagnifierOverBehavior]]></category>
		<category><![CDATA[SetInteractionPropertyAction]]></category>
		<category><![CDATA[Trigger]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=186</guid>
		<description><![CDATA[Using the magnifier-styled CheckBox from the last episode along with the Behavior base classes and SetInteractionPropertyAction we finish the magnification feature.]]></description>
			<content:encoded><![CDATA[<p>This is episode 7 of <a href="http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3">Creating a Hidden Object Game is Silverlight 3</a>.</p>
<p>In the last few posts we have been working on the magnifier feature of the game which we will finish in this episode.</p>
<p>Please review the following posts:</p>
<ul>
<li><a href="http://www.shazaml.com/archives/hidden-object-episode-5">Episode 5 – Add a Magnifier Behavior</a></li>
<li><a href="http://www.shazaml.com/archives/base-classes-for-custom-behaviors">Base Classes for Custom Behaviors</a></li>
<li><a href="http://www.shazaml.com/archives/setinteractionpropertyaction">Creating an Action to set Properties on Actions &amp; Behaviors</a></li>
<li><a href="http://www.shazaml.com/archives/hidden-object-episode-6">Episode 6 – Create a CheckBox from an Image</a></li>
</ul>
<p>First we will add in the following files into the ClutteredCube project:</p>
<ul>
<li>
<div>Interactivity folder (from <a href="http://www.shazaml.com/archives/base-classes-for-custom-behaviors">Base Classes for Custom Behaviors</a>)</div>
<ul>
<li>BaseBehavior.cs</li>
<li>BaseBehaviorT.cs</li>
</ul>
</li>
<li>
<div>Interactivity\SetInteractionPropertyAction folder (from <a href="http://www.shazaml.com/archives/setinteractionpropertyaction">Creating an Action to set Properties on Actions &amp; Behaviors</a>)</div>
<ul>
<li>ConverterHelper.cs</li>
<li>SetInteractionPropertyAction.cs</li>
</ul>
</li>
</ul>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec1.png" alt="" /></p>
<p>The first thing we want to do is to change MagnifierOverBehavior so that it inherits from our custom behavior base class thus inheriting the IsEnabled property:</p>
<pre class="brush: csharp; light: true;">
public class MagnifierOverBehavior : BaseBehavior&lt;FrameworkElement&gt;
</pre>
<p>The only other change we need to make this behavior is not set the Effect property on MouseEnter but instead in MouseMove based on the value of the IsEnabled property:</p>
<pre class="brush: csharp; light: true;">
private void AssociatedObject_MouseEnter( object sender, MouseEventArgs e )
{
  this.AssociatedObject.MouseMove += new MouseEventHandler( AssociatedObject_MouseMove );
  //this.AssociatedObject.Effect = this.magnifier;
}

private void AssociatedObject_MouseMove( object sender, MouseEventArgs e )
{
  if (IsEnabled)
  {
    if (this.AssociatedObject.Effect != this.magnifier)
    {
      this.AssociatedObject.Effect = this.magnifier;
    }
    ...
  }
}
</pre>
<p>In Blend, open the MainPage and select the MagnifierOverBehavior in the Object tree under <em>magnifierCanvas</em>. Name the behavior <em>magnifierBehavior</em> and set IsEnabled to false:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec2.png" alt="" /></p>
<p>From the Assets tab drag two instances of SetInteractionPropertyAction onto the CheckBox magnifier control:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec3.png" alt="" /></p>
<p>On the first one, set the properties as follows:</p>
<ul>
<li>EventName: Checked</li>
<li>TargetName: magnifierCanvas</li>
<li>ObjectName: magnifierBehavior</li>
<li>PropertyName: IsEnabled</li>
<li>Value: true</li>
</ul>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec4.png" alt="" /></p>
<p>Set the second the same as the first, except:</p>
<ul>
<li>EventName: Unchecked</li>
<li>Value: false</li>
</ul>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec5.png" alt="" /></p>
<p>Make sure that the CheckBox control IsChecked property is false to match the InEnabled value of false for <em>magniferBehavior</em>.</p>
<p>Run the game and verify that magnification is only turned on when the CheckBox is checked.</p>
<p>Let&#8217;s do one more thing to finish this episode. Notice that magnifier.png has a white background behind the glass part of the magnifying glass. The image should be on top of something white like a notepad. Import the notepad image and drop it at the bottom of LayoutRoot.</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/notepad.png" alt="notepad" /></p>
<p>Position it how you want it and move the magnifier CheckBox on top of it.</p>
<p><a href="http://www.shazaml.com/downloads/ClutteredCubeSource7.zip">Source Code</a></p>
<p><a href="http://www.shazaml.com/hidden-object-episode-7-demo">Demo</a></p>
<p>There are many features to add so I will keep the <a href="http://www.shazaml.com/archives/hidden-object-episode-8">next episode</a> contents a surprise.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/hidden-object-episode-7/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating an Action to set Properties on Actions &amp; Behaviors</title>
		<link>http://www.shazaml.com/archives/setinteractionpropertyaction</link>
		<comments>http://www.shazaml.com/archives/setinteractionpropertyaction#comments</comments>
		<pubDate>Tue, 22 Sep 2009 23:36:06 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[ChangePropertyAction]]></category>
		<category><![CDATA[SetInteractionPropertyAction]]></category>
		<category><![CDATA[ShowMessageBox]]></category>
		<category><![CDATA[Trigger]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=137</guid>
		<description><![CDATA[The SetInteractionPropertyAction allows you to set properties on Actions and Behaviors as a result of a Trigger.]]></description>
			<content:encoded><![CDATA[<p>Behaviors and actions have properties of their own and it would be useful to be able to set those properties as a result of a trigger.</p>
<p>For this post, we will use the ShowMessageBox action that is part of the <a href="http://expressionblend.codeplex.com/">Expression Blend Samples</a> CodePlex project.</p>
<p>To explore this, create a Silverlight project in Blend 3, drop a Rectangle on LayoutRoot, and drop a ShowMessageBox action onto the Rectangle. The ShowMessageBox action is under the Behaviors &gt; Experimental in the Assets tab:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_2335_CreatinganA1.png" alt="" /></p>
<p>Change the rectangle stroke and fill so that it is easily seen.</p>
<p>Set the Caption and Message properties of ShowMessageBox:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_2335_CreatinganA2.png" alt="" /></p>
<p>When we run the application and click on the rectangle, the message box shows:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_2335_CreatinganA3.png" alt="" /></p>
<p>What if we wanted to change the value of the Message property when a CheckBox control is checked or unchecked?</p>
<p>To accomplish this, we will create SetInteractionPropertyAction that derives from TargetedTriggerAction and adds properties for the name of the action or behavior, the property on that action/behavior, and the value to set on that property:</p>
<pre class="brush: csharp; gutter: false;">
public string ObjectName
{
  get { return (string)GetValue(ObjectNameProperty); }
  set { SetValue(ObjectNameProperty, value); }
}
public static readonly DependencyProperty ObjectNameProperty =
  DependencyProperty.Register(&quot;ObjectName&quot;,
  typeof(string), typeof(SetInteractionPropertyAction),
  new PropertyMetadata(null));&lt;/pre&gt;

public string PropertyName
{
  get { return (string)base.GetValue(PropertyNameProperty); }
  set { base.SetValue(PropertyNameProperty, value); }
}
public static readonly DependencyProperty PropertyNameProperty =
    DependencyProperty.Register(&quot;PropertyName&quot;,
    typeof(string), typeof(SetInteractionPropertyAction),
    new PropertyMetadata(null));

public string Value
{
  get { return (string)base.GetValue(ValueProperty); }
  set { base.SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
  DependencyProperty.Register(&quot;Value&quot;,
  typeof(string), typeof(SetInteractionPropertyAction),
  new PropertyMetadata(null));
</pre>
<p>Next we need to implement the Invoke method which finds the action or behavior under the Target control that matches the ObjectName and sets its property defined by PropertyName to the value specified in Value.</p>
<p>To find actions and behaviors associated with a given object, we use the GetTriggers and GetBehaviors static methods defined on the Interaction class in the System.Windows.Interactivity namespace:</p>
<pre class="brush: csharp; light: true;">
  public static TriggerCollection GetTriggers(DependencyObject obj);
  public static BehaviorCollection GetBehaviors(DependencyObject obj);
</pre>
<p>If we look at some sample XAML we can see that when an action is defined, it becomes part of the Triggers collection and appears under the trigger that causes the action to be invoked. If multiple actions are associated with the same object and use the same trigger, the actions appear together:</p>
<pre class="brush: xml; light: true;">
&lt;Path&gt;
  &lt;i:Interaction.Triggers&gt;
    &lt;i:EventTrigger EventName=&quot;MouseLeftButtonDown&quot;&gt;
      &lt;ic:ChangePropertyAction x:Name=&quot;changeProperty&quot; TargetName=&quot;staplerText&quot; PropertyName=&quot;Opacity&quot; Value=&quot;0.4&quot;/&gt;
      &lt;im:PlaySoundAction x:Name=&quot;playSoundAction&quot; Source=&quot;/Audio/magic_wand.mp3&quot;/&gt;
      &lt;ic:RemoveElementAction x:Name=&quot;removeElement&quot; /&gt;
    &lt;/i:EventTrigger&gt;
  &lt;/i:Interaction.Triggers&gt;
&lt;/Path&gt;
</pre>
<p>Calling Interaction.GetTriggers passing a reference to the hosting Path, will return a single trigger of type EventTrigger. For that trigger, we can use its Actions property to access the three actions.</p>
<p>If we look at some XAML for a behavior, we notice that there is a Behaviors collection containing one or more behaviors and for each behavior there is an optional Triggers collection:</p>
<pre class="brush: xml; light: true;">
&lt;Canvas&gt;
  &lt;i:Interaction.Behaviors&gt;
    &lt;local:ParticlesBehavior x:Name=&quot;particles&quot;&gt;
      &lt;i:Interaction.Triggers&gt;
        &lt;i:EventTrigger SourceName=&quot;staplerPath&quot; EventName=&quot;MouseLeftButtonDown&quot;&gt;
          &lt;i:InvokeCommandAction CommandName=&quot;ShowParticles&quot;/&gt;
        &lt;/i:EventTrigger&gt;
        &lt;i:EventTrigger SourceName=&quot;idolPath&quot; EventName=&quot;MouseLeftButtonDown&quot;&gt;
          &lt;i:InvokeCommandAction CommandName=&quot;ShowParticles&quot;/&gt;
        &lt;/i:EventTrigger&gt;
      &lt;/i:Interaction.Triggers&gt;
    &lt;/local:ParticlesBehavior&gt;

    &lt;Behaviors:MagnifierOverBehavior x:Name=&quot;magnifier&quot;/&gt;
  &lt;/i:Interaction.Behaviors&gt;
&lt;/Canvas&gt;
</pre>
<p>In the case, calling Interaction.GetBehaviors with a reference to the Canvas will return two behaviors: ParticlesBehavior and MagnifierOverBehavior</p>
<p>Just like any object you add to the Object tree, you can set the name of an action or behavior. This adds an x:Name property in the XAML and the name appears in the name textbox in the Properties panel. To access the x:Name property in code, get a reference to the behavior or action and call GetValue passing the FrameworkElement.NameProperty dependency property:</p>
<pre class="brush: csharp; light: true;">
  string actionName = (string)action.GetValue(FrameworkElement.NameProperty);
  string behaviorName = (string)behavior.GetValue(FrameworkElement.NameProperty);
</pre>
<p>For SetInteractionPropertyAction we define the following methods that will try to find an action or behavior that matches the same name as the ObjectName property:</p>
<pre class="brush: csharp; light: true;">
private object TryFindBehavior()
{
  var behaviors = Interaction.GetBehaviors(Target);

  foreach (Behavior behavior in behaviors)
  {
    string behaviorName = (string)behavior.GetValue(FrameworkElement.NameProperty);

    if (behaviorName == ObjectName)
    {
      return behavior;
    }
  }

return null;
}

private object TryFindAction()
{
  var triggers = Interaction.GetTriggers(Target);

  foreach (System.Windows.Interactivity.TriggerBase trigger in triggers)
  {
    foreach (System.Windows.Interactivity.TriggerAction action in trigger.Actions)
    {
      string actionName = (string)action.GetValue(FrameworkElement.NameProperty);

      if (actionName == ObjectName)
      {
        return action;
      }
    }
  }

return null;
}
</pre>
<p>Finally, the Invoke method uses the previous two methods to find a matching action or behavior and to set one of its properties. I admit that a large portion of the Invoke method in the full project source code was borrowed from the SetProperty action defined in the Expression.Samples.Interactivity assembly (thanks .NET Reflector) :</p>
<pre class="brush: csharp; light: true;">
Type itemType = null;
object item = null;

item = TryFindBehavior();

if (item == null)
{
  item = TryFindAction();
}

if (item == null)
{
  return;
}
else
{
  itemType = item.GetType();
}

PropertyInfo property = itemType.GetProperty(this.PropertyName);
property.SetValue(item, this.Value, null);
</pre>
<p>After compiling the code, the SetInteractionPropertyAction will appear in the Assets panel.</p>
<p>Let&#8217;s add a CheckBox control to LayoutRoot and drop two instances of SetInteractionPropertyAction onto the CheckBox. Next select the ShowMessageBox action and set its name property to <em>showMessageBox</em>.</p>
<p>Click on the first instance of SetInteractionPropertyAction and set it&#8217;s EventName, TargetName, ObjectName, PropertyName, and Value properties:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_2335_CreatinganA4.png" alt="" /></p>
<p>For the second instance of SetInteractionPropertyAction, set the same properties as follows:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_2335_CreatinganA5.png" alt="" /></p>
<p>Now run the application. Initially, when you click on the rectangle, you will see a message box that says &#8220;Hello World&#8221;.</p>
<p>When you click on the CheckBox and then click the rectangle, the message will change to &#8220;I am checked&#8221;. Unchecking the CheckBox and clicking on the rectangle will cause the message to change to &#8220;I am unchecked&#8221;.</p>
<p><div id="silverlightControlHost"><object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="200" height="100"><param name="source" value="http://www.shazaml.com/SL/SetInteractionProperty.xap"/><param name="background" value="white" /><param name="minRuntimeVersion" value="3.0.40723.0" /><param name="autoupgrade" value="true" /><param name="enableHtmlAccess" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156" style="text-decoration: none;"><img src="http://storage.timheuer.com/sl4wp-ph.png" alt="Install Microsoft Silverlight" style="border-style: none; width:400px; height:200px"/></a></object><iframe style="visibility:hidden;height:0;width:0;border:0px" id="_sl_historyFrame"></iframe></div><br /></p>
<p> </p>
<p>We have successfully set the property of one action based on a trigger invoking SetInteractionPropertyAction.</p>
<p>If you look at the Common Properties group for our custom action, you will notice that the editors for ObjectName, PropertyName, and Value are just TextBox controls. I would like the design-time experience to be better for this action. Maybe in a future post, this will be revisited. You might have noticed that the Value property is of type string even though the data type of the property that corresponds to PropertyName might be another data type. I originally had Value as type object, but there were times when the textbox would gray out and not allow any input. This would also be solved if custom property editors were defined.</p>
<p>The Microsoft.Expression.Interactions assembly defines an action called ChangePropertyAction that sets the property on an object and has a much better designer experience. It provides a drop-down containing property names and when a property is selected, the editor for the Value reflects the underlying data type of the property:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_2335_CreatinganA7.png" alt="" /></p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_2335_CreatinganA8.png" alt="" /></p>
<p>I would like to include these editors plus one that would show a list of named actions and behaviors for the given Target grouped in the list under the words: Actions or Behaviors.</p>
<p><a href="http://www.shazaml.com/downloads/SetInteractionProperty.zip">Source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/setinteractionpropertyaction/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
