<?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>Designer Silverlight &#187; Triggers</title>
	<atom:link href="http://www.designersilverlight.com/category/triggers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designersilverlight.com</link>
	<description>Matthias Shapiro's Silverlight &#38; WPF Blog - Because Developers Get All The Good Blogs</description>
	<lastBuildDate>Sun, 05 Sep 2010 21:43:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using the &quot;Tag&quot; Field And Triggers To Avoid Writing a Value Converter in WPF</title>
		<link>http://www.designersilverlight.com/2009/03/08/using-the-tag-field-and-triggers-to-avoid-writing-a-value-converter-in-wpf/</link>
		<comments>http://www.designersilverlight.com/2009/03/08/using-the-tag-field-and-triggers-to-avoid-writing-a-value-converter-in-wpf/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 04:50:04 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Binding]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Triggers]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[styles]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2009/03/08/using-the-tag-field-and-triggers-to-avoid-writing-a-value-converter-in-wpf/</guid>
		<description><![CDATA[I was working on a project recently and I wanted one of my layout controls to have a different margin based on a certain piece of data. (It&#8217;s a long story&#8230; let&#8217;s just say that this is a good post if you want to change properties of a control based on a piece of data [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a project recently and I wanted one of my layout controls to have a different margin based on a certain piece of data.</p>
<p>(It&#8217;s a long story&#8230; let&#8217;s just say that this is a good post if you want to change properties of a control based on a piece of data of a different type.)</p>
<p>So&#8230; for the sake of the argument, let&#8217;s say that I want my control to have a margin of &#8220;4,4,4,4&#8243; if my data returns &#8220;dog&#8221; and I want it to have a margin of &#8220;2,2,2,2&#8243; if my data returns &#8220;cat&#8221; and a margin of &#8220;0,0,0,0&#8243; if the data is anything else.</p>
<p>Normally, I would use a value converter for this. My problem was that I was sick of using value converters for things so specific and using them only a couple times in my application. So I decided I wanted to do this one with styles and triggers.</p>
<p>First thing I did was bind my data to the “Tag” field.</p>
<p><span style="color: blue">&lt;</span><span style="color: #a31515">Border </span><span style="color: red">Style</span><span style="color: blue">=&#8221;{</span><span style="color: #a31515">DynamicResource </span><span style="color: red">MyBorderWithTriggers</span><span style="color: blue">}&#8221; </span><span style="color: red">Tag</span><span style="color: blue">=&#8221;{</span><span style="color: #a31515">Binding </span><span style="color: red">MySpecialData</span><span style="color: blue">}&#8221; &gt;</span></p>
<p>Then, I created a style for my Border layout control. If you’re in Blend, go to Object –&gt; Edit Style –&gt; Create Empty…</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/03/clip-image001.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/03/clip-image001-thumb.png" border="0" alt="clip_image001" width="368" height="320" /></a></p>
<p>Create a new property trigger by clicking on the “+ Property” button and change the property to “Tag”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/03/clip-image0015.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[5]" src="http://www.designersilverlight.com/wp-content/uploads/2009/03/clip-image0015-thumb.png" border="0" alt="clip_image001[5]" width="314" height="185" /></a></p>
<p>I couldn’t find a way to type “dog” into the field value, so I did it in the XAML (full XAML sample below, for those of you who want to cut to the chase… you know who you are).</p>
<p>With the property trigger highlighted, you’ll see a “Trigger recording is on” sign in the corner of your canvas.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/03/clip-image0017.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[7]" src="http://www.designersilverlight.com/wp-content/uploads/2009/03/clip-image0017-thumb.png" border="0" alt="clip_image001[7]" width="170" height="39" /></a></p>
<p>Just change all the properties you want. Of course, in this case, I’m just going to change the Margin property. If we do the same thing for the “Cat” contingency, we get the following style.</p>
<p><span style="color: blue">&lt;</span><span style="color: #a31515">Style </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&#8221;MyBorderWithTriggers&#8221; </span><span style="color: red">TargetType</span><span style="color: blue">=&#8221;{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type </span><span style="color: red">Border</span><span style="color: blue">}&#8221;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&#8221;Margin&#8221; </span><span style="color: red">Value</span><span style="color: blue">=&#8221;0,0,0,0&#8243;/&gt;</span>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">Style.Triggers</span><span style="color: blue">&gt;<br />
</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue"> &lt;</span><span style="color: #a31515">Trigger </span><span style="color: red">Property</span><span style="color: blue">=&#8221;Tag&#8221; </span><span style="color: red">Value</span><span style="color: blue">=&#8221;Dog&#8221;&gt;<br />
</span><span style="color: blue"> </span><span style="color: blue"> </span><span style="color: blue"> </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&#8221;Margin&#8221; </span><span style="color: red">Value</span><span style="color: blue">=&#8221;4,4,4,4&#8243;/&gt;</span><span style="color: blue"> </span><span style="color: blue"> </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue"> &lt;/</span><span style="color: #a31515">Trigger</span><span style="color: blue">&gt;<br />
</span><span style="color: blue"> </span><span style="color: blue"> </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">Trigger </span><span style="color: red">Property</span><span style="color: blue">=&#8221;Tag&#8221; </span><span style="color: red">Value</span><span style="color: blue">=&#8221;Cat&#8221;&gt;<br />
</span><span style="color: blue"> </span><span style="color: blue"> </span><span style="color: blue"> </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">Setter </span><span style="color: red">Property</span><span style="color: blue">=&#8221;Margin&#8221; </span><span style="color: red">Value</span><span style="color: blue">=&#8221;2,2,2,2&#8243;/&gt;<br />
</span><span style="color: blue"> </span><span style="color: blue"> </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">Trigger</span><span style="color: blue">&gt;<br />
</span><span style="color: blue"> </span>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue"> &lt;/</span><span style="color: #a31515">Style.Triggers</span><span style="color: blue">&gt;<br />
&lt;/</span><span style="color: #a31515">Style</span><span style="color: blue">&gt;</span></p>
<p>And we end up with a layout that changes its properties based on a bound value. And we don’t have to write endless value converters.  Pretty handy… or at least I thought so.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/03/08/using-the-tag-field-and-triggers-to-avoid-writing-a-value-converter-in-wpf/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
