<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mike Talbot&#039;s Blog</title>
	<atom:link href="http://whydoidoit.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://whydoidoit.com</link>
	<description>Musing on software development in .NET/Silverlight and C#</description>
	<lastBuildDate>Thu, 16 Feb 2012 09:47:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='whydoidoit.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/d9da59a6b656ce63457986cc8f7babe0?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Mike Talbot&#039;s Blog</title>
		<link>http://whydoidoit.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://whydoidoit.com/osd.xml" title="Mike Talbot&#039;s Blog" />
	<atom:link rel='hub' href='http://whydoidoit.com/?pushpress=hub'/>
		<item>
		<title>Updated WordCloud Component</title>
		<link>http://whydoidoit.com/2011/08/17/updated-wordcloud-component/</link>
		<comments>http://whydoidoit.com/2011/08/17/updated-wordcloud-component/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 12:50:59 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=412</guid>
		<description><![CDATA[I&#8217;ve updated the WordCloud component and added some documentation that you can find here: http://whydoidoit.com/wordcloud-for-silverlight The new component supports individual word colouring and word angle.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=412&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve updated the WordCloud component and added some documentation that you can find here: <a href="http://whydoidoit.com/wordcloud-for-silverlight/">http://whydoidoit.com/wordcloud-for-silverlight</a></p>
<p>The new component supports individual word colouring and word angle.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/412/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/412/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/412/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=412&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2011/08/17/updated-wordcloud-component/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>
	</item>
		<item>
		<title>SilverlightSerializer Version 2</title>
		<link>http://whydoidoit.com/2011/08/05/silverlightserializer-version-2/</link>
		<comments>http://whydoidoit.com/2011/08/05/silverlightserializer-version-2/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 20:22:11 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Binary Serialization]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MakeGenericType]]></category>
		<category><![CDATA[Missing]]></category>
		<category><![CDATA[MissingMethod]]></category>
		<category><![CDATA[Reflection]]></category>
		<category><![CDATA[Serialization]]></category>
		<category><![CDATA[Serializer]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[SilverlightSerializer]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=352</guid>
		<description><![CDATA[SilverlightSerializer version 2 Well, I know it&#8217;s been a long wait and I&#8217;ve been promising the new version of SilverlightSerializer for months, but it&#8217;s finally here, extracted from my core project and working standalone. This new version of the serializer is a major rewrite &#8211; with 2 goals in mind: 1. Performance Serialization performance is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=352&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>SilverlightSerializer version 2</h3>
<p>Well, I know it&#8217;s been a long wait and I&#8217;ve been promising the new version of SilverlightSerializer for months, but it&#8217;s finally here, extracted from my core project and working standalone.</p>
<p>This new version of the serializer is a major rewrite &#8211; with 2 goals in mind:</p>
<p style="padding-left:30px;">1. <strong>Performance</strong></p>
<p style="padding-left:60px;">Serialization performance is <strong>61%</strong> faster (my test harness serializes 100,000 objects in 1.03 seconds compared to 1.72)<br />
Deserialization performance is <strong>27%</strong> faster (my test hardness deserializes 100,000 objects in 1.61 seconds compared to 2.04)</p>
<p style="padding-left:60px;">These performance gains are on large object graphs containing multiple instances of the same types &#8211; it would nominally be slightly slower on small graphs or graphs that contain only one copy of each object &#8211; but of course that&#8217;s very unlikely in the real world.</p>
<p style="padding-left:30px;">2. <strong>Extensibility</strong></p>
<p style="padding-left:60px;">The new version of SilverlightSerializer abstracts the storage methods behind a new interface, IStorage.  There&#8217;s a complete implementation of a BinarySerializer that is compatible with the old version of SilverlightSerializer, but you could add your own.  Alterian has built versions that store data in XML and SQL Server tables.  This means one standard semantic serialization layer can be used with pluggable storage types to put the information where you need it for the application at hand.</p>
<p style="padding-left:60px;">That said, writing an IStorage is fairly involved process and I&#8217;ll write a post or a page on it if people want me to.</p>
<p>In addition the serializer now handles arrays and enums far better than before and hopefully should avoid the dreaded <strong>MissingMethod</strong> exception by providing a more developer friendly exception and message while offering the opportunity to construct classes without a parameterless constructor using a new CreateType event that will help some avoid the need to write custom serializers.</p>
<p>You can find the version 2 source code <a title="Silverlight Serializer" href="http://whydoidoit.com/silverlight-serializer/">here</a>.  In response to a couple of requests, the project is also accessible on GitHub, see the link at the top of the SilverlightSerializer page.</p>
<h3>Why is it faster?</h3>
<p>So read on if you&#8217;d like to know why it&#8217;s faster!</p>
<p>SilverlightSerializer is a reflection based serializer.  It examines an object to identify the available properties and fields. As anyone who is familiar with .NET will know, reflection isn&#8217;t exactly the fastest thing in the world.  SS v1 did its level best to cache everything possible, but it still relied on reflection to write and read properties and fields from the underlying objects.  This is why SS didn&#8217;t perform as well as the inbuilt serializers in .NET.</p>
<p>Now reflection in this case is a hard thing to get around, I don&#8217;t know the types in advance and I have no interest in getting the developer to have to write a custom serialization class for each of their types.</p>
<p>The answer to this performance challenge came from using generic classes and using .NET to construct a new generic class on the fly for each property of each class being serialized.  The generic class accesses the native property access functions directly, rather than have to use reflection.  Fields have to be handled slightly differently, with a reflection function, but at least it&#8217;s no slower than the previous method.</p>
<p>First let&#8217;s start with the base class that is used for all of the generic classes, it&#8217;s called GetSet.</p>
<p><pre class="brush: csharp;">

public abstract class GetSet
{
public PropertyInfo Info;
public string Name;
public FieldInfo FieldInfo;
public object Vanilla;
public bool CollectionType;
public abstract object Get(object item);
public abstract void Set(object item, object value);

}

</pre></p>
<p>From this base class I create a definition of a generic class, called GetSetGeneric.  This class creates Get and Set delegates using the class parameters, these delegates then map to the functions that will be exposed by the property getters and setters on the target class (neat huh).  This happens only once per type/property combination and the resulting code is the same speed as writing a function to get the value.</p>
<p><pre class="brush: csharp;">
public class GetSetGeneric&lt;T, TR&gt; : GetSet
{
public delegate TR GetValue(T obj);
public delegate void SetValue(T obj, TR value);
private readonly GetValue _get;
private readonly SetValue _set;

public GetSetGeneric(PropertyInfo info)
{
MethodInfo getMethod;
MethodInfo setMethod = null;
Name = info.Name;
Info = info;
CollectionType = Info.PropertyType.GetInterface(&quot;IEnumerable&quot;, true) != null;
getMethod = info.GetGetMethod();
setMethod = info.GetSetMethod();
_get = (GetValue)Delegate.CreateDelegate(typeof(GetValue), getMethod);
if (setMethod != null) _set = (SetValue)Delegate.CreateDelegate(typeof(SetValue), setMethod);
}

public GetSetGeneric(FieldInfo info)
{
MethodInfo getMethod;
MethodInfo setMethod = null;
Name = info.Name;
FieldInfo = info;
_get = new GetValue(GetFieldValue);
_set = new SetValue(SetFieldValue);
CollectionType = FieldInfo.FieldType.GetInterface(&quot;IEnumerable&quot;, true) != null;
return;
}

public GetSetGeneric(string name)
{
Name = name;
MethodInfo getMethod;
MethodInfo setMethod= null;
var t = typeof(T);
var p = t.GetProperty(name);
if (p == null)
{
FieldInfo = typeof(T).GetField(Name);
_get = new GetValue(GetFieldValue);
_set = new SetValue(SetFieldValue);
CollectionType = FieldInfo.FieldType.GetInterface(&quot;IEnumerable&quot;, true) != null;
return;
}
Info = p;
CollectionType = Info.PropertyType.GetInterface(&quot;IEnumerable&quot;, true) != null;
getMethod = p.GetGetMethod();
setMethod = p.GetSetMethod();
_get = (GetValue)Delegate.CreateDelegate(typeof(GetValue), getMethod);
if(setMethod != null) _set = (SetValue)Delegate.CreateDelegate(typeof(SetValue), setMethod);
}

private TR GetFieldValue(T obj)
{
return (TR)FieldInfo.GetValue(obj);
}

private void SetFieldValue(T obj, TR value)
{
FieldInfo.SetValue(obj, value);
}

public override object Get(object item)
{
return _get((T)item);
}

public override void Set(object item, object value)
{
_set((T)item, (TR)value);
}
}

</pre></p>
<p>Once that&#8217;s done it&#8217;s just a matter of creating the generic classes. Deserialization does this one property at a time form the input stream:</p>
<p><pre class="brush: csharp;">
entryConfiguration = new EntryConfiguration();

var pi = entry.OwningType.GetProperty(entry.Name);
if (pi != null)
{
entryConfiguration.Type = pi.PropertyType;
var gs = typeof(GetSetGeneric&lt;,&gt;);
var tp = gs.MakeGenericType(new Type[] { entry.OwningType, pi.PropertyType });
entryConfiguration.Setter = (GetSet)Activator.CreateInstance(tp, new object[] { pi });
}
else
{
var fi = entry.OwningType.GetField(entry.Name);
if (fi != null)
{
entryConfiguration.Type = fi.FieldType;
var gs = typeof(GetSetGeneric&lt;,&gt;);
var tp = gs.MakeGenericType(new Type[] { entry.OwningType, fi.FieldType });
entryConfiguration.Setter = (GetSet)Activator.CreateInstance(tp, new object[] { fi });
}
}
</pre></p>
<p>Here you can see I get either the property or the field information, use the type of the containing class and the property in a call to MakeGenericType to construct the correct class, then create an instance of that class that can be used to get and set properties on the object.<br />
Serialization works by creating a list of getters and setters for every sensible property on the object, in SilverlightSerializer that creates a few different flavours depending on whether you are using Checksums etc. If you want to see how I do that, have a look at the GetWriteableAttributes class and the GetAccessors() function.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/352/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=352&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2011/08/05/silverlightserializer-version-2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>
	</item>
		<item>
		<title>Wordle Style Word Cloud or Tag Cloud Component For Silverlight</title>
		<link>http://whydoidoit.com/2011/07/28/wordle-style-word-cloud-component-for-silverlight/</link>
		<comments>http://whydoidoit.com/2011/07/28/wordle-style-word-cloud-component-for-silverlight/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 17:45:03 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Jonathan Feinberg]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Stack Overflow]]></category>
		<category><![CDATA[Tag cloud]]></category>
		<category><![CDATA[Word Cloud]]></category>
		<category><![CDATA[Wordle]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=330</guid>
		<description><![CDATA[LATEST VERSION AND DOCUMENTATION AVAILABLE HERE I&#8217;ve been looking for a component that could show Wordle style word clouds.  The only Silverlight component I could find was Infragistics xamTagCloud, but it really didn&#8217;t produce the output I was looking for.  What I wanted was to be able to put lower importance words within the spaces [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=330&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="mceTemp">
<div id="attachment_333" class="wp-caption alignleft" style="width: 470px"><a href="http://whydoidoit.files.wordpress.com/2011/07/wordcloudpicture.png"><img class="size-full wp-image-333" title="wordcloudpicture" src="http://whydoidoit.files.wordpress.com/2011/07/wordcloudpicture.png?w=460&#038;h=377" alt="Word Cloud" width="460" height="377" /></a><p class="wp-caption-text">Example Word Cloud</p></div>
</div>
<h1><a href="http://www.miketalbot.plus.com/TestCloud.zip" target="_blank"><img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=460" alt="Download"   /></a>LATEST VERSION AND DOCUMENTATION AVAILABLE <a href="http://whydoidoit.com/wordcloud-for-silverlight/">HERE</a></h1>
<p>I&#8217;ve been looking for a component that could show <a href="http://www.wordle.net/" target="_blank">Wordle</a> style word clouds.  The only Silverlight component I could find was Infragistics xamTagCloud, but it really didn&#8217;t produce the output I was looking for.  What I wanted was to be able to put lower importance words within the spaces left around larger entries.</p>
<div id="attachment_332" class="wp-caption alignright" style="width: 310px"><a href="http://whydoidoit.files.wordpress.com/2011/07/word-cloud-2.png"><img class="size-medium wp-image-332" title="Word Cloud Example" src="http://whydoidoit.files.wordpress.com/2011/07/word-cloud-2.png?w=300&#038;h=199" alt="Words with coloring" width="300" height="199" /></a><p class="wp-caption-text">Example Subtle Word Coloring</p></div>
<p>Having looked around the web I couldn&#8217;t find a component to buy or borrow so I decided to write my own.</p>
<p>I came across <a title="StackOverflow post" href="http://stackoverflow.com/questions/342687/algorithm-to-implement-something-like-wordle" target="_blank">this</a> post on Stack Overflow that had a response from the author of Wordle (Jonathan Feinberg), giving some tips on how Wordle works.  I used that and the <a title="Wordle Algorithm" href="http://static.mrfeinberg.com/bv_ch03.pdf" target="_blank">extra reference</a> Jonathan provided to build my component.</p>
<p>The key to word layout is to use a spiral (or another progressive positioning equation) to try to position words, every new word needs to be checked against the previously placed items to ensure that there is no overlap.</p>
<p>As collision is the expensvie part of the algorithm Jonathan suggests using a variety of techniques around collision rectangles and space partitioning to do the item testing; I felt this wasn&#8217;t necessary for my needs so I actually test the overlapping pixels to see if there is a hit.  This seems to give good enough performance, and if I wanted to improve it I would advocate making a second collision map at a lower resolution (1/8 scale) that would significantly cut down on the processing &#8211; but again, wasn&#8217;t necessary for me.</p>
<p>If you want to use something to layout instead of my spiral, you need to replace the GetSpiralPoint function with something that will return a progressive space based on an increaseing angle measured in radians.</p>
<p>The component also supports word hit testing and selection as well as coloring words based on a value as well as sizing them.</p>
<p>You can download the full source code for the component and the test project which will allow you to play with the parameters from the link at the top of this post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/330/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=330&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2011/07/28/wordle-style-word-cloud-component-for-silverlight/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2011/07/wordcloudpicture.png" medium="image">
			<media:title type="html">wordcloudpicture</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2011/07/word-cloud-2.png?w=300" medium="image">
			<media:title type="html">Word Cloud Example</media:title>
		</media:content>
	</item>
		<item>
		<title>Automatic Grid Layout for Silverlight</title>
		<link>http://whydoidoit.com/2010/10/06/automatic-grid-layout-for-silverlight/</link>
		<comments>http://whydoidoit.com/2010/10/06/automatic-grid-layout-for-silverlight/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 14:57:55 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[Automatic Grid]]></category>
		<category><![CDATA[Grid]]></category>
		<category><![CDATA[Grid layout]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=273</guid>
		<description><![CDATA[An automatic Grid layout for Silverlight<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=273&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.miketalbot.plus.com/AutoGrid.zip" target="_blank"><img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=460" alt="Download"   /></a></p>
<p>I always find myself cursing Grids in Silverlight when I have to go back and insert something else into the layout after the fact: I always forget to move somethings Row and end up with a lot of obscured components. To compensate for this I&#8217;ve written a Grid subclass that can automatically layout its children and fill out the <strong>ColumnDefinition</strong> and <strong>RowDefinition</strong> for missing values when the children refer to rows and columns beyond the specified dimensions. In addition it will auto layout for multiple columns using sequential children, this is good if all you want is a 2 by n grid of label/editor cells.</p>
<p>My AutoGrid will allow you to define Rows and Columns like an ordinary grid, but if you don&#8217;t catch all of the cases then it fills out the rest for you. For instance you could just define the columns and it will fill out the rows based on the<strong> Grid.Row</strong> settings of the children. If you want to go a step further, then don&#8217;t specify any<strong> Grid.Row</strong> or <strong>Grid.Column</strong> attributes and instead add a <strong>Columns=&#8221;x&#8221;</strong> to the AutoGrid and it will sequentially lay out the children in column then row order. </p>
<p>Normally the rows and columns added are &#8220;Auto&#8221; sized, you can however set the last row and column to be &#8220;*&#8221; sized using the properties <strong>AutoSizeLastColumn</strong> and <strong>AutoSizeLastRow</strong>.  If you need more complex arrangements than this then you should define the items you need sizing explicity using the <strong>ColumnDefinitions</strong> and/or <strong>RowDefinitions</strong> attributes.</p>
<p>It really saves me a lot of time when I&#8217;m editing this stuff!</p>
<p>Here is an example in full automatic layout mode:</p>
<p><pre class="brush: xml;">

		&lt;AutoGrid:AutoGrid x:Name=&quot;LayoutRoot&quot; Columns=&quot;2&quot;&gt;
			&lt;TextBlock Text=&quot;Name&quot;
					   Margin=&quot;2&quot;/&gt;
			&lt;TextBox Components:SelectOnFocus.SelectAll=&quot;True&quot; Text=&quot;{Binding Name, Mode=TwoWay}&quot;
					 Margin=&quot;2&quot;
					 Width=&quot;150&quot;
					 /&gt;
			&lt;TextBlock Text=&quot;Type&quot;
					   Margin=&quot;2&quot;
					   /&gt;
			&lt;ComboBox
					ItemsSource=&quot;{Binding Type, Converter={StaticResource EnumSource}}&quot;
					SelectedItem=&quot;{Binding Type, Converter={StaticResource EnumValue}}&quot;
					Margin=&quot;2&quot;
					Width=&quot;150&quot;
					/&gt;
			&lt;TextBlock Text=&quot;Width&quot; Margin=&quot;2&quot; /&gt;
			&lt;TextBox Text=&quot;{Binding Width, Mode=TwoWay}&quot;
				 Components:SelectOnFocus.SelectAll=&quot;True&quot;
					 Margin=&quot;2&quot;
				 Width=&quot;150&quot;/&gt;
			&lt;TextBlock Text=&quot;Choice List &quot; VerticalAlignment=&quot;Top&quot; Margin=&quot;2&quot;/&gt;
			&lt;TextBox Width=&quot;150&quot;
					 Height=&quot;100&quot;
					 Margin=&quot;2&quot;
					 AcceptsReturn=&quot;True&quot;
					 Text=&quot;{Binding ChoiceList, Converter={StaticResource ListToString}, Mode=TwoWay}&quot;/&gt;
		&lt;/AutoGrid:AutoGrid&gt;

</pre></p>
<p>Which produces this output:</p>
<p style="text-align:center;"><a href="http://whydoidoit.files.wordpress.com/2010/10/edit-variable.jpg"><img class="size-full wp-image-275  aligncenter" title="edit variable" src="http://whydoidoit.files.wordpress.com/2010/10/edit-variable.jpg?w=460" alt=""   /></a></p>
<p> </p>
<p>You can download the project from <a href="http://www.miketalbot.plus.com/AutoGrid.zip" target="_blank">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/273/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/273/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/273/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/273/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/273/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/273/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/273/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/273/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/273/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/273/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/273/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/273/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/273/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/273/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=273&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2010/10/06/automatic-grid-layout-for-silverlight/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/10/edit-variable.jpg" medium="image">
			<media:title type="html">edit variable</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight VisualState changes using DataBinding not code behind</title>
		<link>http://whydoidoit.com/2010/09/09/silverlight-visualstate-changes-using-databinding-not-code-behind/</link>
		<comments>http://whydoidoit.com/2010/09/09/silverlight-visualstate-changes-using-databinding-not-code-behind/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 09:56:36 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[DataBinding]]></category>
		<category><![CDATA[Extensible Application Markup Language]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[VisualState]]></category>
		<category><![CDATA[VisualState Trigger]]></category>
		<category><![CDATA[VisualStateManager]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=238</guid>
		<description><![CDATA[I hate the fact that I have to keep using code behind to trigger VisualState changes in my view.  Frequently I just want to change a visual state of a control based on the properties of the object to which the control is bound &#8211; such as making additional options visible or invisible, changing the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=238&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.miketalbot.plus.com/VisualStateCtrls.zip" target="_blank"><img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=460" alt="Download"   /></a><br />
I hate the fact that I have to keep using code behind to trigger VisualState changes in my view.  Frequently I just want to change a visual state of a control based on the properties of the object to which the control is bound &#8211; such as making additional options visible or invisible, changing the mode of the display based on information stored in the model etc.</p>
<p>So I&#8217;ve come up with a control that helps!  The VisualStateController can be placed in the <a class="zem_slink" title="Extensible Application Markup Language" rel="wikipedia" href="http://en.wikipedia.org/wiki/Extensible_Application_Markup_Language">XAML</a> for a control and be bound to a property of the databound object using its <em>StateMode </em>dependency property. </p>
<ul>
<li>If the property is a <strong>bool </strong>then two further properties <em>OnState</em> and <em>OffState </em>are used to determine the visual state of the control based on the value of the property.</li>
<li>If the property is an <strong>enum</strong> then the visual state will be set the the text string name of the enum value.</li>
<li>If the property is a <strong>string</strong> it will be used to directly set the visual state</li>
</ul>
<p>Now changes causing normal binding updates will automatically trigger the visual state changes.</p>
<p><pre class="brush: xml;">&lt;VisualStateCtrls:VisualStateController StateMode=&quot;{Binding IsSelected}&quot;
             OnState=&quot;Selected&quot;
             OffState=&quot;Unselected&quot;/&gt;

&lt;VisualStateCtrls:VisualStateController StateMode=&quot;{Binding IsResizable}&quot;
             OnState=&quot;Sizers&quot;
             OffState=&quot;NoSizers&quot;/&gt;

</pre></p>
<p>The VisualStateController binds automatically to the UserControl (or Control) that it is a child of.  You can put it inside any panel within the body of your UserControl&#8217;s XAML.</p>
<p>You can find the project and source code <a href="http://www.miketalbot.plus.com/VisualStateCtrls.zip">here</a>.</p>
<p>If you want to achieve the same thing using triggers and a slightly more wordy approach, you can use blend and triggers &#8211; see <a href="http://betaforums.silverlight.net/forums/p/170935/387395.aspx">here </a>for details.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/238/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=238&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2010/09/09/silverlight-visualstate-changes-using-databinding-not-code-behind/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>
	</item>
		<item>
		<title>Serializing classes between Silverlight and .NET 4</title>
		<link>http://whydoidoit.com/2010/09/03/serializing-classes-between-silverlight-and-net-4/</link>
		<comments>http://whydoidoit.com/2010/09/03/serializing-classes-between-silverlight-and-net-4/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 08:19:28 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Share silverlight class .NET]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight Binary Serialization]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=221</guid>
		<description><![CDATA[I&#8217;ve realized that I didn&#8217;t detail how to ensure you can serialize your own classes between Silverlight and .NET 4 using my SilverlightSerializer class.  The vital thing to do in these circumstances is to define the classes you want to share in a Silverlight assembly that only references System, System.Core and mscorlib.  These are the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=221&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve realized that I didn&#8217;t detail how to ensure you can serialize your own classes between Silverlight and .NET 4 using my <a href="http://whydoidoit.com/2010/04/08/silverlight-serialization/">SilverlightSerializer </a>class. </p>
<p>The vital thing to do in these circumstances is to define the classes you want to share in a Silverlight assembly that only references System, System.Core and mscorlib.  These are the requirements for assemblies that can be used in both types of project.  If you define your classes in this way then they can be deserialized on the back end without a problem, reference anything else and it won&#8217;t work.  Fortunately most of what you will need is included in those system assemblies!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/221/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=221&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2010/09/03/serializing-classes-between-silverlight-and-net-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight AllChildren: find all of the visual children of a FrameworkElement</title>
		<link>http://whydoidoit.com/2010/09/02/silverlight-allchildren-find-all-of-the-visual-children-of-a-frameworkelement/</link>
		<comments>http://whydoidoit.com/2010/09/02/silverlight-allchildren-find-all-of-the-visual-children-of-a-frameworkelement/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 11:25:18 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AllChildren]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight all children]]></category>
		<category><![CDATA[VisualTree]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=211</guid>
		<description><![CDATA[Our application has some occasions where we need to iterate the entire child tree of a visual component. Not a common thing in many applications, I use it to pass visual states to the sub elements of a tree, but if you need a routine for this purpose then I thought I would post mine here:   The function [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=211&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Our application has some occasions where we need to iterate the entire child tree of a visual component. Not a common thing in many applications, I use it to pass visual states to the sub elements of a tree, but if you need a routine for this purpose then I thought I would post mine here:  </p>
<p><pre class="brush: csharp;">

        public static List&lt;T&gt; AllChildren&lt;T&gt;(this FrameworkElement ele, Func&lt;DependencyObject, bool&gt; whereFunc = null) where T : class
        {
            if (ele == null)
                return null;
            var output = new List&lt;T&gt;();
            var c = VisualTreeHelper.GetChildrenCount(ele);
            for (var i = 0; i &lt; c; i++)
            {
                var ch = VisualTreeHelper.GetChild(ele, i);
                if (whereFunc != null)
                {
                    if (!whereFunc(ch))
                    {
                        continue;
                    }
                }
                if ((ch is T))
                    output.Add(ch as T);
                if (!(ch is FrameworkElement))
                    continue;

                output.AddRange((ch as FrameworkElement).AllChildren&lt;T&gt;(whereFunc));
            }
            return output;
        }

</pre></p>
<p>The function is an extension method that uses the generic Type to decide on the types of children to return.  It takes an optional &#8220;Where function&#8221; that can be used to stop iterating down branches of the visual tree &#8211; please note that the Where function doesn&#8217;t only get passed the Type components, it gets everything so you can stop the recursive operation when you want to.  If you don&#8217;t pass the Where parameter then all visual children are returned.  </p>
<pre style="padding-left:30px;"><pre class="brush: csharp;"> 

                foreach (var c in panel.AllChildren&lt;Control&gt;((child) =&gt; !(child is FdTreeViewItem)))
                {
                    VisualStateManager.GoToState(c, &quot;FlowSelected&quot;, true);
                } 

</pre>

   
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/211/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=211&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2010/09/02/silverlight-allchildren-find-all-of-the-visual-children-of-a-frameworkelement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging Silverlight &#8220;element is already the child of another element&#8221;!</title>
		<link>http://whydoidoit.com/2010/08/30/debug-xaml-element-is-already-the-child-of-another-element/</link>
		<comments>http://whydoidoit.com/2010/08/30/debug-xaml-element-is-already-the-child-of-another-element/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 17:35:00 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[Element is already the child of another element]]></category>
		<category><![CDATA[LoadComponent]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[XAML debugging]]></category>
		<category><![CDATA[XAML Error]]></category>
		<category><![CDATA[XamlReader]]></category>
		<category><![CDATA[[Line: 0 Position: 0]]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=153</guid>
		<description><![CDATA[A debugging tool to help programmers track down the catch all error - Element is already the child of another element<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=153&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3><a href="http://www.miketalbot.plus.com/XamlValidator.zip"><img class="alignright size-full wp-image-181" title="Download" src="http://whydoidoit.files.wordpress.com/2010/08/download.png?w=460" alt="Download"   /></a>The problem</h3>
<p> I know how many people are having a problem with the catch-all XAML error &#8220;Element is already the child of another element&#8221; &#8211; it&#8217;s been a popular post on this blog and I come across it all of the time.  The real problem is that the exception gives no indication of the area causing the failure, you are forced into playing with the source file and banging your head against the wall.  Well, help is at hand!  </p>
<p>I have written a program that helps identify which part of the XAML is going wrong and tells you about it!  Now one caveat &#8211; it can only tightly identify a single error that is causing this problem &#8211; if more than one thing throws the exception then the best you can hope for is for it to point out the XAML container that contains the code that is failing &#8211; such as VisualStateManager.   In most cases it will be more specific, because only one element will have changed and cause the exception to be thrown. This ability is helping me a lot, hopefully it can help you too&#8230;  </p>
<h3>The solution</h3>
<p> So here&#8217;s what I have done.  I have written a XAML validator that reads in the file and then attempts to process it.  When the file has a problem it starts to prune out nodes in the XAML one XML node at a time (I mean it puts back nodes that didn&#8217;t solve the problem and tries the next in sequence).  It does this starting with the nodes that have no children, then the ones with 1, 2 and so on, until it has to remove the entire body &#8211; hopefully the error is found long before this.  Using this technique the algorithm tries to give you the most tightly isolated area of the XAML to focus on.  </p>
<p>When it identifies a node, it checks whether removing any attribute helps and returns this in addition to the offending XAML if it resolves the issue.  </p>
<p>The result is a string describing the XAML and any offending attribute.  </p>
<p>This is a debugging tool only &#8211; it really slows down the application using it, so don&#8217;t call it when you don&#8217;t have a problem - but it massively improves your debugging time.  I normally instantiate in the constructor of the failing class and Debug.WriteLine() its result.  It is VITAL that you call it from the assembly that contains the failing XAML &#8211; it won&#8217;t work if you don&#8217;t. (See the discussion below if you want to know why!)  </p>
<p>You can download the project from <a href="http://www.miketalbot.plus.com/XamlValidator.zip">here</a>.  Feel free to use it as you see fit.  </p>
<p>Here&#8217;s an example of using it: </p>
<p><pre class="brush: csharp;">  

public HtmlTextBox()
        {
            var s = ValidateXaml.GetXamlErrors(new System.Uri(&quot;/Alchemy.extension;component/Components/HtmlTextBox.xaml&quot;, System.UriKind.Relative));
            Debug.WriteLine(s);
            InitializeComponent();
       }   

</pre></p>
<h3>Caveats</h3>
<p>This isn&#8217;t going to work too well if removing a node that fails just causes the problem to propagate to a part of the XAML that was previously valid - the only way I can think about solving that requires far more recursion and taking out multiple nodes at each level.  I&#8217;d be interested in anyone elses view on this and any other strategies&#8230;</p>
<p>You may have a problem with project references, if you do then see the discussion below, hopefully I&#8217;ve accounted for most circumstances.</p>
<h3>Discussion</h3>
<p>Having had the initial idea about this approach I ran into a couple of stumbling blocks which are worth exploring for those who are interested.  </p>
<p>Firstly, when you call InitializeComponent in a UserControl etc &#8211; it uses Application.LoadComponent to parse the XAML.  I can&#8217;t use that approach because it takes a URI and I need to pass in a string which is the XAML with an element removed.  For this reason my code uses XamlReader to parse the file &#8211; the problem there is that it always throws an exception for XAML that contains an x:Class attribute.  My first job then is to strip that out.  </p>
<p>The next attribute problem was much harder to solve.  UserControl xmlns attributes are used to provide reference to components stored in other assemblies. The attribute provides the namespace and the assembly.  The assembly is omitted for other controls that are contained in the same assembly as the problem control.  LoadComponent has no problems with resolving this reference, but XamlReader throws our favorite exception if it isn&#8217;t there &#8211; this took me a LONG time to figure out.  </p>
<p>So I have to add an &#8220;assembly=&#8221; extension to the xmlns references that don&#8217;t contain one (except the xlmns:x) to ensure that XamlReader will parse a correct file.  I do this by enquiring about the calling assembly and adding its name to the attribute.  </p>
<p>Sounds sensible right?  Well it was a bit more complicated than that &#8211; if I just modify the attribute while it is connected to the document, some behind the scenes magic changes all of the things that are referred to by that namespace! Guess what the error is? Yep, &#8220;element is already the child of another element&#8221; :)  </p>
<p>So now I remove the attribute from the document, modify it and put it back &#8211; presto!  No unwanted changes and the algorithm finally gets to have a go at parsing the file.</p>
<p>If this attribute modification causes you a problem, you could remove the code and explicitly qualify your project references, this does work, I&#8217;ve tried it and would account for a situation in which I accidentally update an xmlns that doesn&#8217;t need touching &#8211; but this does mean that you won&#8217;t be using the VS2010 automatically generated references.  I hope that this won&#8217;t normally be a problem.</p>
<p>UPDATE: I&#8217;ve modified the source to allow you to pass a parameter that turns off the xmlns modifications &#8211; see above comment about explicit assembly references if you set this.  It defaults to &#8220;true&#8221; which enables the automatic update of references.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/153/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=153&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2010/08/30/debug-xaml-element-is-already-the-child-of-another-element/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:content url="http://whydoidoit.files.wordpress.com/2010/08/download.png" medium="image">
			<media:title type="html">Download</media:title>
		</media:content>
	</item>
		<item>
		<title>ProportionalPanel for Silverlight</title>
		<link>http://whydoidoit.com/2010/07/19/proportionalpanel-for-silverlight/</link>
		<comments>http://whydoidoit.com/2010/07/19/proportionalpanel-for-silverlight/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 17:57:14 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[ArrangeOverride]]></category>
		<category><![CDATA[custom panel]]></category>
		<category><![CDATA[custom silverlight panel]]></category>
		<category><![CDATA[dashboard]]></category>
		<category><![CDATA[dashboard layout]]></category>
		<category><![CDATA[MeasureOverride]]></category>
		<category><![CDATA[proportional panel]]></category>
		<category><![CDATA[ProportionalPanel]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=126</guid>
		<description><![CDATA[A Silverlight panel that distributes its children according to width or an arbitrary proportion supplied by the object attached to DataContext<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=126&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I wanted a panel that distributes its children according to the proportion of their width or by a proportion supplied by the object attached to the DataContext.  This lead to me writing PropotionalPanel &#8211; the first custom panel I have written.  It&#8217;s useful in building dashboards and other layouts that require collections of children, because Grids aren&#8217;t easy to use inside ItemsControls.  I wrote this control to be used as the ItemsPanel of a list box and it works well in that context.When writing a panel the two important routines are MeasureOverride and ArrangeOverride.  There&#8217;s lots of documentation on these in the help, but it&#8217;s interesting to see the parameters with which these methods are called at different times.  </p>
<p>For instance, even if you have a fixed size container, MeasureOverride is called with infinite dimensions on the first occasion, thereafter the call specifies the fixed size.  </p>
<p>Here&#8217;s my version of the MeasureOverride routine. I maintain a variable called totalProportion which is either the sum of the proportions of the attached objects, or the sum of the proportions of the stretched dimension based on orientation.  </p>
<p><pre class="brush: csharp;">  

        protected override Size MeasureOverride(Size availableSize)
        {
            Size finalSize = new Size(availableSize.Width, availableSize.Height);
            double totalProportion = 0;
            foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
            {  

                if (c.DataContext is IProportional)
                {
                    totalProportion += (c.DataContext as IProportional).GetProportion();
                }
                else
                {
                    totalProportion += Orientation == Orientation.Vertical
                                           ? c.DesiredSize.Height
                                           : c.DesiredSize.Width;
                }
            }  

            double sizeAvailable, maxAlternate = 0;
            switch (Orientation)
            {
                case Orientation.Horizontal:
                    sizeAvailable = availableSize.Width;
                    if (double.IsNaN(sizeAvailable) || double.IsPositiveInfinity(sizeAvailable))
                    {
                        sizeAvailable = 0;
                        foreach (var c in Children)
                        {
                            c.Measure(availableSize);
                            sizeAvailable += c.DesiredSize.Width;
                            maxAlternate = Math.Max(maxAlternate, c.DesiredSize.Height);
                        }
                        finalSize.Width = sizeAvailable;
                        finalSize.Height = maxAlternate;  

                    }
                    else
                    {
                        foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
                        {
                            double p;
                            if (c.DataContext is IProportional)
                            {
                                p = (c.DataContext as IProportional).GetProportion();
                            }
                            else
                            {
                                p = Orientation == Orientation.Vertical
                                                       ? c.DesiredSize.Height
                                                       : c.DesiredSize.Width;
                            }
                            c.Measure(new Size(Math.Max(0,Math.Floor((sizeAvailable * p)/totalProportion)), finalSize.Height));
                        }
                    }
                    break;
                case Orientation.Vertical:
                    sizeAvailable = availableSize.Height;
                    if (double.IsNaN(sizeAvailable) || double.IsPositiveInfinity(sizeAvailable))
                    {
                        sizeAvailable = 0;
                        foreach (var c in Children)
                        {
                            c.Measure(availableSize);
                            sizeAvailable += c.DesiredSize.Height;
                            maxAlternate = Math.Max(maxAlternate, c.DesiredSize.Width);
                        }
                        finalSize.Height = sizeAvailable;
                        finalSize.Width = maxAlternate;
                    }
                    else
                    {
                        foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
                        {
                            double p;
                            if (c.DataContext is IProportional)
                            {
                                p = (c.DataContext as IProportional).GetProportion();
                            }
                            else
                            {
                                p = Orientation == Orientation.Vertical
                                                       ? c.DesiredSize.Height
                                                       : c.DesiredSize.Width;
                            }
                            c.Measure(new Size(finalSize.Width, Math.Max(0, Math.Floor((sizeAvailable * p) / totalProportion))));
                        }
                    }  

                    break;
            }  
            return finalSize;
        }
    }  

</pre></p>
<p> </p>
<p>First the total proportion is calculated by walking through the children and summing either the proportions from the attached object, or the required size.  </p>
<p><pre class="brush: csharp;">  

            double totalProportion = 0;
            foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
            {  

                if (c.DataContext is IProportional)
                {
                    totalProportion += (c.DataContext as IProportional).GetProportion();
                }
                else
                {
                    totalProportion += Orientation == Orientation.Vertical
                                           ? c.DesiredSize.Height
                                           : c.DesiredSize.Width;
                }
            }  

</pre></p>
<p> </p>
<p>If the parameters of the MeasureOverride call have infinite dimensions I need to measure all of the children for their desired size, if there is a specific size then I need to measure each child providing guidance on how much space they will be allocated.  </p>
<p><pre class="brush: csharp;">  

           case Orientation.Horizontal:
                    sizeAvailable = availableSize.Width;
                    if (double.IsNaN(sizeAvailable) || double.IsPositiveInfinity(sizeAvailable))
                    {
                        sizeAvailable = 0;
                        foreach (var c in Children)
                        {
                            c.Measure(availableSize);
                            sizeAvailable += c.DesiredSize.Width;
                            maxAlternate = Math.Max(maxAlternate, c.DesiredSize.Height);
                        }
                        finalSize.Width = sizeAvailable;
                        finalSize.Height = maxAlternate;  

                    }
                    else
                    {
                        foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
                        {
                            double p;
                            if (c.DataContext is IProportional)
                            {
                                p = (c.DataContext as IProportional).GetProportion();
                            }
                            else
                            {
                                p = Orientation == Orientation.Vertical
                                                       ? c.DesiredSize.Height
                                                       : c.DesiredSize.Width;
                            }
                            c.Measure(new Size(Math.Max(0,Math.Floor((sizeAvailable * p)/totalProportion)), finalSize.Height));
                        }
                    }
                    break;  

</pre></p>
<p> </p>
<p>In the fixed size measurement I use the proportion to calculate the amount of stretched space available to the control.  It is <strong>vital </strong>that you both measure and arrange children in the correct size &#8211; if you don&#8217;t measure then it initially appears to work, but strange sizing artefacts occur.  </p>
<p>The arrangement pass is very similar to the fixed size measurement, except you pass a rectangle to the child giving it the layout slot into which it will fit:  </p>
<p><pre class="brush: csharp;">  

            if (Orientation == Orientation.Vertical)
            {
                foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
                {
                    double p = 0;
                    if (c.DataContext is IProportional)
                    {
                        p = (c.DataContext as IProportional).GetProportion();
                    }
                    else
                    {
                        p = c.DesiredSize.Height;
                    }
                    double d = Math.Max(0,Math.Floor((finalSize.Height * p) / totalProportion));
                    c.Arrange(new Rect(0, offset, finalSize.Width, d));
                    offset += d;
                }
            }  

</pre></p>
<p> </p>
<p>The IProportional interface is implemented on objects that are used as DataContexts for the items in my panel:  </p>
<p><pre class="brush: csharp;">  

    public interface IProportional
    {
        double GetProportion();
    } 

</pre></p>
<p> </p>
<p>When you have a proportional panel like this it&#8217;s a good idea to provide some way for the user to resize things!  I use a thing called FrameControl in my ListBox&#8217;s DataTemplate &#8211; this provides sizing thumbs that can change the horizontal and vertical proportions of the underlying DataContext objects. </p>
<p><pre class="brush: csharp;"> 

        private void WidthThumb_OnDragDelta(object sender, DragDeltaEventArgs e)
        {
            var pp = this.FirstVisualAncestorOfType&lt;ProportionalPanel&gt;();
            var thisIndex = pp.Children.IndexOf(this.FirstVisualAncestorOfType&lt;ListBoxItem&gt;());
            var thisItem = DataContext as DashboardItem;
            var nextItem = pp.Children[thisIndex + 1].Cast&lt;FrameworkElement&gt;().DataContext as DashboardItem; 

            Debug.Assert(thisItem != null);
            Debug.Assert(nextItem != null); 

            var move = (e.HorizontalChange * GetProportions()) / pp.ActualWidth;
            if (thisItem.Proportion + move &lt; 0)
                move = - thisItem.Proportion;
            if (nextItem.Proportion - move &lt; 0)
                move = nextItem.Proportion;
            thisItem.Proportion += move;
            nextItem.Proportion -= move;
            pp.InvalidateMeasure();
        } 

</pre></p>
<p> </p>
<p>This example uses my FirstVisualAncestorOfType extension method found elsewhere on this blog.  For the FrameControl it finds the ProportionalPanel that owns it then works out what the pixel change in the Thumb means in terms of the change to the arbitrary proportions returning by the IProportional implementation.  If you just used heights then this would be a lot simpler, but harder to store.  Obviously you need a thumb for the sizable dimension, but not both, so I show just the relevant thumb based on the panel&#8217;s orientation. </p>
<p>To build a dashboard I layer panel within panel as you can see in this video &#8211; which also shows the thumb action and the ability to react to drag and drop (not allowing containers of the same type to be embedded within containers). </p>
<p><div id="v-MtPSp6En-1" class="video-player" style="width:460px;height:344px">
<embed id="v-MtPSp6En-1-video" src="http://s0.videopress.com/player.swf?v=1.03&amp;guid=MtPSp6En&amp;isDynamicSeeking=true" type="application/x-shockwave-flash" width="460" height="344" title="ProportionalPanel in action" wmode="direct" seamlesstabbing="true" allowfullscreen="true" allowscriptaccess="always" overstretch="true"></embed></div> </p>
<p>Here&#8217;s the complete code for the panel.  I have the IProportional interface defined in a library that is used by the items I attach to the DataContext, so that they don&#8217;t have to reference the Panel - but you might just want to insert it in this namespace. </p>
<p><pre class="brush: csharp;"> 

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls; 

namespace DashboardRenderers
{
    public class ProportionalPanel : Panel
    {
        public Orientation Orientation
        {
            get
            {
                return (Orientation)GetValue(OrientationProperty);
            }
            set
            {
                SetValue(OrientationProperty, value);
            }
        } 

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register(&quot;Orientation&quot;, typeof(Orientation), typeof(ProportionalPanel), new PropertyMetadata(System.Windows.Controls.Orientation.Vertical)); 

        protected override Size ArrangeOverride(Size finalSize)
        {
            double offset = 0;
            double totalProportion = 0;
            foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
            { 

                if (c.DataContext is IProportional)
                {
                    totalProportion += (c.DataContext as IProportional).GetProportion();
                }
                else
                {
                    totalProportion += Orientation == Orientation.Vertical
                                           ? c.DesiredSize.Height
                                           : c.DesiredSize.Width;
                }
            }
            if (Orientation == Orientation.Vertical)
            {
                foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
                {
                    double p = 0;
                    if (c.DataContext is IProportional)
                    {
                        p = (c.DataContext as IProportional).GetProportion();
                    }
                    else
                    {
                        p = c.DesiredSize.Height;
                    }
                    double d = Math.Max(0,Math.Floor((finalSize.Height * p) / totalProportion));
                    c.Arrange(new Rect(0, offset, finalSize.Width, d));
                    offset += d;
                }
            }
            else
            {
                foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
                {
                    double p = 0;
                    if (c.DataContext is IProportional)
                    {
                        p = (c.DataContext as IProportional).GetProportion();
                    }
                    else
                    {
                        p = c.DesiredSize.Width;
                    }
                    double d = Math.Max(0, Math.Floor((finalSize.Width * p) / totalProportion));
                    c.Arrange(new Rect(offset, 0, d, finalSize.Height));
                    offset += d;
                } 

            }
            return finalSize;
        } 
        protected override Size MeasureOverride(Size availableSize)
        {
            Size finalSize = new Size(availableSize.Width, availableSize.Height);
            double totalProportion = 0;
            foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
            { 

                if (c.DataContext is IProportional)
                {
                    totalProportion += (c.DataContext as IProportional).GetProportion();
                }
                else
                {
                    totalProportion += Orientation == Orientation.Vertical
                                           ? c.DesiredSize.Height
                                           : c.DesiredSize.Width;
                }
            } 

            double sizeAvailable, maxAlternate = 0;
            switch (Orientation)
            {
                case Orientation.Horizontal:
                    sizeAvailable = availableSize.Width;
                    if (double.IsNaN(sizeAvailable) || double.IsPositiveInfinity(sizeAvailable))
                    {
                        sizeAvailable = 0;
                        foreach (var c in Children)
                        {
                            c.Measure(availableSize);
                            sizeAvailable += c.DesiredSize.Width;
                            maxAlternate = Math.Max(maxAlternate, c.DesiredSize.Height);
                        }
                        finalSize.Width = sizeAvailable;
                        finalSize.Height = maxAlternate; 

                    }
                    else
                    {
                        foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
                        {
                            double p;
                            if (c.DataContext is IProportional)
                            {
                                p = (c.DataContext as IProportional).GetProportion();
                            }
                            else
                            {
                                p = Orientation == Orientation.Vertical
                                                       ? c.DesiredSize.Height
                                                       : c.DesiredSize.Width;
                            }
                            c.Measure(new Size(Math.Max(0,Math.Floor((sizeAvailable * p)/totalProportion)), finalSize.Height));
                        }
                    }
                    break;
                case Orientation.Vertical:
                    sizeAvailable = availableSize.Height;
                    if (double.IsNaN(sizeAvailable) || double.IsPositiveInfinity(sizeAvailable))
                    {
                        sizeAvailable = 0;
                        foreach (var c in Children)
                        {
                            c.Measure(availableSize);
                            sizeAvailable += c.DesiredSize.Height;
                            maxAlternate = Math.Max(maxAlternate, c.DesiredSize.Width);
                        }
                        finalSize.Height = sizeAvailable;
                        finalSize.Width = maxAlternate;
                    }
                    else
                    {
                        foreach (var c in Children.OfType&lt;FrameworkElement&gt;())
                        {
                            double p;
                            if (c.DataContext is IProportional)
                            {
                                p = (c.DataContext as IProportional).GetProportion();
                            }
                            else
                            {
                                p = Orientation == Orientation.Vertical
                                                       ? c.DesiredSize.Height
                                                       : c.DesiredSize.Width;
                            }
                            c.Measure(new Size(finalSize.Width, Math.Max(0, Math.Floor((sizeAvailable * p) / totalProportion))));
                        }
                    } 

                    break;
            } 
            return finalSize;
        }
    }
} 

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=126&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" /><div><a href="http://whydoidoit.com/2010/07/19/proportionalpanel-for-silverlight/"><img alt="ProportionalPanel in action" src="http://videos.videopress.com/MtPSp6En/proportionalpanel_scruberthumbnail_0.jpg" width="160" height="120" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2010/07/19/proportionalpanel-for-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://videos.videopress.com/MtPSp6En/proportionalpanel_std.mp4" length="2241536" type="video/mp4" />

		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>

		<media:group>
			<media:content url="http://videos.videopress.com/MtPSp6En/proportionalpanel_std.mp4" fileSize="2241536" type="video/mp4" medium="video" bitrate="796" isDefault="true" duration="22" width="400" height="300" />

			<media:content url="http://videos.videopress.com/MtPSp6En/proportionalpanel_fmt1.ogv" fileSize="2241536" type="video/ogg" medium="video" bitrate="796" isDefault="false" duration="22" width="400" height="300" />

			<media:rating scheme="urn:mpaa">g</media:rating>
			<media:title type="plain">ProportionalPanel in action</media:title>
			<media:thumbnail url="http://videos.videopress.com/MtPSp6En/proportionalpanel_scruberthumbnail_0.jpg" width="256" height="192" />
			<media:player url="http://s0.videopress.com/player.swf?v=1.03&#38;guid=MtPSp6En&#38;isDynamicSeeking=true" width="400" height="300" />
		</media:group>
	</item>
		<item>
		<title>Finding a typed visual parent in Silverlight</title>
		<link>http://whydoidoit.com/2010/07/17/finding-a-typed-visual-parent-in-silverlight/</link>
		<comments>http://whydoidoit.com/2010/07/17/finding-a-typed-visual-parent-in-silverlight/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 10:54:54 +0000</pubDate>
		<dc:creator>whydoidoit</dc:creator>
				<category><![CDATA[Project With Code]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[visual parent]]></category>
		<category><![CDATA[Visual Tree]]></category>

		<guid isPermaLink="false">http://whydoidoit.com/?p=95</guid>
		<description><![CDATA[My application frequently needs to find a parent of a Silverlight element, and due to the nature of popup panels I also sometimes want to know if the element is &#8220;logically&#8221; connected to another element.  To achieve this I wrote a couple of helper functions that walk the visual tree and return a typed parent.  You [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=95&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>My application frequently needs to find a parent of a Silverlight element, and due to the nature of popup panels I also sometimes want to know if the element is &#8220;logically&#8221; connected to another element.  To achieve this I wrote a couple of helper functions that walk the visual tree and return a typed parent.  You can implement a special interface to indicate that there is a logical connection between items that aren&#8217;t physically connected to each other in the Visual Tree too if you need to (very helpful with focus issues).</p>
<p><pre class="brush: csharp;">

        public static T FirstVisualAncestorOfType&lt;T&gt;(this DependencyObject element) where T : DependencyObject
        {
            if (element == null) return null;
           
            var parent = VisualTreeHelper.GetParent(element) as DependencyObject;
            while (parent != null)
            {
                if (parent is T)
                    return (T)parent;
                if (parent is IBreakVisualParenting)
                {
                    parent = ((IBreakVisualParenting)parent).Parent;
                }
                else
                    parent = VisualTreeHelper.GetParent(parent) as DependencyObject;
            }
            return null;
        }

        public interface IBreakVisualParenting
        {
            DependencyObject Parent { get; }
        }

        public static T LastVisualAncestorOfType&lt;T&gt;(this DependencyObject element) where T : DependencyObject
        {
            T item = null;
            var parent = VisualTreeHelper.GetParent(element) as DependencyObject;
            while (parent != null)
            {
                if (parent is T)
                    item = (T) parent;
                if(parent is IBreakVisualParenting)
                {
                    parent = ((IBreakVisualParenting) parent).Parent;
                }
                else
                    parent = VisualTreeHelper.GetParent(parent) as DependencyObject;
            }
            return item;
        }

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/whydoidoit.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/whydoidoit.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/whydoidoit.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/whydoidoit.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/whydoidoit.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/whydoidoit.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/whydoidoit.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/whydoidoit.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/whydoidoit.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/whydoidoit.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/whydoidoit.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/whydoidoit.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/whydoidoit.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/whydoidoit.wordpress.com/95/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=whydoidoit.com&amp;blog=11474660&amp;post=95&amp;subd=whydoidoit&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://whydoidoit.com/2010/07/17/finding-a-typed-visual-parent-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/43adcf99cd9f3ccb588117bbdb855f5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">whydoidoit</media:title>
		</media:content>
	</item>
	</channel>
</rss>
