<?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 Design, LLC &#187; ChangePropertyAction</title>
	<atom:link href="http://www.shazaml.com/archives/tag/changepropertyaction/feed" rel="self" type="application/rss+xml" />
	<link>http://www.shazaml.com</link>
	<description>Design and Development for Windows Phone 7</description>
	<lastBuildDate>Thu, 27 Oct 2011 19:33:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<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>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; title: ; notranslate">
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; title: ; notranslate">
  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; title: ; notranslate">
&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; title: ; notranslate">
&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; title: ; notranslate">
  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; title: ; notranslate">
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; title: ; notranslate">
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>
		<item>
		<title>Hidden Object: Episode 3 &#8211; Marking Items off the List</title>
		<link>http://www.shazaml.com/archives/hidden-object-episode-3</link>
		<comments>http://www.shazaml.com/archives/hidden-object-episode-3#comments</comments>
		<pubDate>Fri, 18 Sep 2009 00:10:27 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Hidden Object Game]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[ChangePropertyAction]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[PlaySoundAction]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=57</guid>
		<description><![CDATA[A clever use of Paths and the ChangePropertyAction will quickly mark items of the list.]]></description>
			<content:encoded><![CDATA[<p>In this episode of <a href="http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3">Creating a Hidden Object Game in Silverlight 3</a> we overlay the items in the image with paths and associate the ChangePropertyAction to change the Opacity of the TextBlock to indicate that the item has been found.</p>
<p>In order to detect when an item is clicked in the office image, we need a hotspot. A Path works great for this and odd-shaped items can be outlined using the Pen tool. Press the &#8220;P&#8221; key and the Pen tool will be selected:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0010_HiddenObjec1.png" alt="" /></p>
<p>Center the mouse cursor over the stapler and use the mouse wheel to zoom in. Now click various points to outline the shape of the stapler:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0010_HiddenObjec2.png" alt="" /></p>
<p>I started with a point in the bottom left corner and clicked a total of 7 points to outline the shape. When I got to point 5 in the top left, the path&#8217;s fill started obstructing my view of the rest of the object. Simply move over to the Appearance group and set the Opacity below 50%. Continue to draw the remaining points. When you get back to the first point, the mouse cursor will change to the pen with a loop indicating that clicking will close the path. Give the path a name (staplerPath) and set the Opacity to 0%. It is time to add the ChangePropertyAction to the path. On the Assets tab, click Behaviors and find the ChangePropertyAction.</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0010_HiddenObjec3.png" alt="" /></p>
<p> </p>
<p>Drag the action from the Assets tab to the Object tree and drop it on staplerPath:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0010_HiddenObjec4.png" alt="" /></p>
<p> </p>
<p>This action is now associated with staplerPath object. Adding this action to our object tree caused two assemblies to be added to the References folder:</p>
<ul>
<li>Microsoft.Expression.Interactions.dll</li>
<li>System.Windows.Interactivity.dll</li>
</ul>
<p>Looking at the Properties panel you will see the properties for this action and notice that there is already an EventTrigger selected to fire when the staplerPath object gets a MouseLeftButtonDown event. That is exactly what we want. In the Common Properties group you will see TargetName and PropertyName. The TargetName is the name of the object that will have its property changed and PropertyName is the name of the property. Since we want the staplerText TextBlock to be dimmed when the staperPath object is clicked, then we set the TargetName to staplerText. An easy way to do that is to grab the bullseye (called the artboard element picker) and drag it to staplerText on either the canvas or the object tree. The Properties for this action should now look like this:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0010_HiddenObjec5.png" alt="" /></p>
<p>Press F5 to run the project and click on the stapler. The text for stapler is now dimmed.</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0010_HiddenObjec6.png" alt="" /></p>
<p>You can play with the Animation Properties by setting duration for the property change and even set an easing value if you don&#8217;t want a linear change in the Opacity from 100% to 40%. Let&#8217;s make this even better by adding a sound effect when an item is clicked.  I found a nice <a title="Magic Wand Sound" href="http://soundbible.com/474-Magic-Wand-Noise.html">magic wand sound on SoundBible.com</a> and added it to the project in an Audio folder. Now drag the PlaySoundAction from the Assets list onto staplerPath and set the Properties to use the audio file:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/091809_0010_HiddenObjec7.png" alt="" /></p>
<p> </p>
<p>Now repeat this same process for the other 12 items. We have the core of our very own hidden object game!</p>
<p> </p>
<p>In our <a href="http://www.shazaml.com/archives/hidden-object-episode-4">next episode</a>, we will write a custom behavior that will shoot a spray of particles when an item is clicked.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/hidden-object-episode-3/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

