<?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>design-time writer's block &#187; attached behavior</title>
	<atom:link href="http://www.dragonshed.org/blog/tag/attached-behavior/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dragonshed.org/blog</link>
	<description>deadly sharp lines, waves of sine, intersecting shapes with tweens and keysplines</description>
	<lastBuildDate>Wed, 10 Feb 2010 06:04:32 +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>radiobuttons in a datatemplate in silverlight</title>
		<link>http://www.dragonshed.org/blog/2009/03/08/radiobuttons-in-a-datatemplate-in-silverlight/</link>
		<comments>http://www.dragonshed.org/blog/2009/03/08/radiobuttons-in-a-datatemplate-in-silverlight/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 09:33:37 +0000</pubDate>
		<dc:creator>Karim</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[attached behavior]]></category>
		<category><![CDATA[IdentityMine]]></category>
		<category><![CDATA[Samples]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.dragonshed.org/blog/?p=211</guid>
		<description><![CDATA[I was recently asked by a customer, “Why doesn’t RadioButton.GroupName work when a single RadioButton is put in a DataTemplate within an ItemsControl?”  I had no answer.  When I tried it out myself, lo and behold, two checked RadioButtons were staring back at me.  A quick search told me that others have [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently asked by a customer, “Why doesn’t RadioButton.GroupName work when a single RadioButton is put in a DataTemplate within an ItemsControl?”  I had no answer.  When I tried it out myself, lo and behold, two checked RadioButtons were staring back at me.  A quick search told me that others have encountered this issue, and none had found a solution based in markup.</p>
<p>To illustrate the mechanics of Attached Behaviors, a topic that I had been reviewing with my customer, I decided to implement a working RadioButtonGroup as one.  Simply use the attached property in place of RadioButton.GroupName, and the behavior takes care of the rest.</p>
<pre style="font-size:1em;line-height:1.2em;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ItemsControl </span><span style="color:#ff0000;">ItemsSource</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">Binding </span><span style="color:#ff0000;">DataCollection</span><span style="color:#0000ff;">}"&gt;
</span>  <span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ItemsControl.ItemsPanel</span><span style="color:#0000ff;">&gt;
</span><span style="color:#0000ff;">    </span><span style="color:#0000ff;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ItemsPanelTemplate</span><span style="color:#0000ff;">&gt;
    </span></span><span style="color:#0000ff;">  &lt;</span><span style="color:#a31515;">StackPanel </span><span style="color:#ff0000;">Orientation</span>=<span style="color:#0000ff;">"Horizontal"</span><span style="color:#a31515;"> </span><span style="color:#0000ff;">/&gt;
</span><span style="color:#0000ff;"><span style="color:#0000ff;">    </span></span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ItemsPanelTemplate</span><span style="color:#0000ff;">&gt;
</span><span style="color:#0000ff;">  &lt;/</span><span style="color:#a31515;">ItemsControl.ItemsPanel</span><span style="color:#0000ff;">&gt;
  &lt;</span><span style="color:#a31515;">ItemsControl</span><span style="color:#a31515;">.</span><span style="color:#a31515;">ItemTemplate</span><span style="color:#0000ff;">&gt;
    &lt;</span><span style="color:#a31515;">DataTemplate</span><span style="color:#0000ff;">&gt;
      &lt;</span><span style="color:#a31515;">RadioButton</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">Content</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">Binding</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">}"
                   </span><span style="color:#ff0000;">b</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">RadioButtonGroup.Name</span><span style="color:#0000ff;">="1"/&gt;
    &lt;/</span><span style="color:#a31515;">DataTemplate</span><span style="color:#0000ff;">&gt;
  &lt;/</span><span style="color:#a31515;">ItemsControl</span><span style="color:#a31515;">.</span><span style="color:#a31515;">ItemTemplate</span><span style="color:#0000ff;">&gt;
</span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ItemsControl</span><span style="color:#0000ff;">&gt;</span></pre>
<p>One interesting detail about my implementation:  It can be used with ToggleButtons to achieve RadioButton-like behavior.</p>
<p><img class="aligncenter size-full wp-image-197" title="Screenshot" src="http://dragonshed.org/samples/radiobuttongroup/radiobuttons-attachedbehavior.png" alt="" width="298" height="64" /></p>
<p>The solution source is available <a href="http://dragonshed.org/samples/radiobuttongroup/RadioButtonGroup.zip">here</a>.  Feedback welcome.</p>
<p>Side note: Using reflector, I noticed that although RadioButton has all the necessary plumbing to group arbitrary radio buttons (and that code is remarkably similar to how a standard attachedbehavior is implemented), it relied on the &#8220;Logical Tree&#8221;, FrameworkElement.Parent, to scope group names to a common root node.  One side effect of using ItemsControl is that the ContentPresenters used to show an item&#8217;s DataTemplate are severed from their parents.  Parent is always null.  This attached behavior doesn’t use that, and in fact it does not scope the group Names at all, so be sure to choose unique names.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dragonshed.org/blog/2009/03/08/radiobuttons-in-a-datatemplate-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
