<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Implementing RelativeSource binding in Silverlight</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/</link>
	<description>Colin Eberhardt&#039;s Adventures in .NET</description>
	<lastBuildDate>Thu, 09 Feb 2012 03:02:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Luc Bos</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/#comment-95607</link>
		<dc:creator>Luc Bos</dc:creator>
		<pubDate>Tue, 27 Dec 2011 12:42:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=139#comment-95607</guid>
		<description>I found a memory leak in your class.  You don&#039;t remove items out of the dictionary when they are unloaded.  A big PIA when switching pages.

Here is the fix:

Add a property to RelayBindingKey:

        private struct RelayBindingKey
        {
            public DependencyProperty dependencyObject;
            public FrameworkElement frameworkElement;
            public FrameworkElement targetFrameworkElement;
        }

Then set the targetFrameworkElement in CreateRelayBinding:

        private static void CreateRelayBinding(FrameworkElement targetElement, FrameworkElement sourceElement,
            BindingProperties bindingProperties)
        {
...

            // create a key that identifies this source binding
            RelayBindingKey key = new RelayBindingKey()
            {
                dependencyObject = sourceDependencyProperty,
                frameworkElement = sourceElement,
                targetFrameworkElement = targetElement
            };

...
}

Then set that on unload of the element you remove the eventhandler and the dictionary entry:

        private static void OnBinding(
            DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement targetElement = depObj as FrameworkElement;

            targetElement.Loaded += TargetElement_Loaded;
            targetElement.Unloaded += TargetElement_Unloaded;
        }

        private static void TargetElement_Unloaded(object sender, RoutedEventArgs e)
        {
            FrameworkElement targetElement = sender as FrameworkElement;
            if (targetElement != null)
            {
                targetElement.Loaded -= TargetElement_Loaded;
                targetElement.Unloaded -= TargetElement_Unloaded;
            }

            var elementToDelete = relayBindings.Keys.FirstOrDefault(x =&gt; x.targetFrameworkElement.Equals(targetElement));
            if (elementToDelete.dependencyObject != null)
            {
                relayBindings.Remove(elementToDelete);
                Debug.WriteLine(&quot;Element deleted&quot;);
            }
        }</description>
		<content:encoded><![CDATA[<p>I found a memory leak in your class.  You don&#8217;t remove items out of the dictionary when they are unloaded.  A big PIA when switching pages.</p>
<p>Here is the fix:</p>
<p>Add a property to RelayBindingKey:</p>
<p>        private struct RelayBindingKey<br />
        {<br />
            public DependencyProperty dependencyObject;<br />
            public FrameworkElement frameworkElement;<br />
            public FrameworkElement targetFrameworkElement;<br />
        }</p>
<p>Then set the targetFrameworkElement in CreateRelayBinding:</p>
<p>        private static void CreateRelayBinding(FrameworkElement targetElement, FrameworkElement sourceElement,<br />
            BindingProperties bindingProperties)<br />
        {<br />
&#8230;</p>
<p>            // create a key that identifies this source binding<br />
            RelayBindingKey key = new RelayBindingKey()<br />
            {<br />
                dependencyObject = sourceDependencyProperty,<br />
                frameworkElement = sourceElement,<br />
                targetFrameworkElement = targetElement<br />
            };</p>
<p>&#8230;<br />
}</p>
<p>Then set that on unload of the element you remove the eventhandler and the dictionary entry:</p>
<p>        private static void OnBinding(<br />
            DependencyObject depObj, DependencyPropertyChangedEventArgs e)<br />
        {<br />
            FrameworkElement targetElement = depObj as FrameworkElement;</p>
<p>            targetElement.Loaded += TargetElement_Loaded;<br />
            targetElement.Unloaded += TargetElement_Unloaded;<br />
        }</p>
<p>        private static void TargetElement_Unloaded(object sender, RoutedEventArgs e)<br />
        {<br />
            FrameworkElement targetElement = sender as FrameworkElement;<br />
            if (targetElement != null)<br />
            {<br />
                targetElement.Loaded -= TargetElement_Loaded;<br />
                targetElement.Unloaded -= TargetElement_Unloaded;<br />
            }</p>
<p>            var elementToDelete = relayBindings.Keys.FirstOrDefault(x =&gt; x.targetFrameworkElement.Equals(targetElement));<br />
            if (elementToDelete.dependencyObject != null)<br />
            {<br />
                relayBindings.Remove(elementToDelete);<br />
                Debug.WriteLine(&#8220;Element deleted&#8221;);<br />
            }<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mister Goodcat &#124; Silverlight 5 Tidbits–Data binding</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/#comment-95523</link>
		<dc:creator>Mister Goodcat &#124; Silverlight 5 Tidbits–Data binding</dc:creator>
		<pubDate>Sun, 11 Dec 2011 10:49:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=139#comment-95523</guid>
		<description>[...] way from the data context of the item back to the context of the items control, so people like Colin Eberhardt and Kiener have come up with clever ways to work around this [...]</description>
		<content:encoded><![CDATA[<p>[...] way from the data context of the item back to the context of the items control, so people like Colin Eberhardt and Kiener have come up with clever ways to work around this [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: José Ribeiro</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/#comment-68105</link>
		<dc:creator>José Ribeiro</dc:creator>
		<pubDate>Fri, 29 Jul 2011 08:19:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=139#comment-68105</guid>
		<description>Thank you very much!
It solves the problem for binding within a DataTemplate!
Not even ElementName could do that! Great!

Cheers!</description>
		<content:encoded><![CDATA[<p>Thank you very much!<br />
It solves the problem for binding within a DataTemplate!<br />
Not even ElementName could do that! Great!</p>
<p>Cheers!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: .Net Follower</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/#comment-58512</link>
		<dc:creator>.Net Follower</dc:creator>
		<pubDate>Thu, 09 Jun 2011 19:43:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=139#comment-58512</guid>
		<description>In my blog I described the BoundToSelfControl, which binds itself to itself and in the same time it saves what was passed through DataContext in special field. Take a look at &lt;a href=&quot;http://dotnetfollower.com/wordpress/2011/06/silverlight-for-windows-phone-7-how-to-bound-usercontrol-to-itself/&quot; rel=&quot;nofollow&quot;&gt;http://dotnetfollower.com/wordpress/2011/06/silverlight-for-windows-phone-7-how-to-bound-usercontrol-to-itself/&lt;/a&gt;


Thanks!

.NET Follower (&lt;a href=&quot;http://dotnetfollower.com&quot; rel=&quot;nofollow&quot;&gt;http://dotnetfollower.com&lt;/a&gt;)</description>
		<content:encoded><![CDATA[<p>In my blog I described the BoundToSelfControl, which binds itself to itself and in the same time it saves what was passed through DataContext in special field. Take a look at <a href="http://dotnetfollower.com/wordpress/2011/06/silverlight-for-windows-phone-7-how-to-bound-usercontrol-to-itself/" rel="nofollow">http://dotnetfollower.com/wordpress/2011/06/silverlight-for-windows-phone-7-how-to-bound-usercontrol-to-itself/</a></p>
<p>Thanks!</p>
<p>.NET Follower (<a href="http://dotnetfollower.com" rel="nofollow">http://dotnetfollower.com</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eivind Gussiås Løkseth</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/#comment-56874</link>
		<dc:creator>Eivind Gussiås Løkseth</dc:creator>
		<pubDate>Sun, 29 May 2011 11:32:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=139#comment-56874</guid>
		<description>Thanks a lot! This solved my frustrating problem!</description>
		<content:encoded><![CDATA[<p>Thanks a lot! This solved my frustrating problem!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin Eberhardt</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/#comment-49277</link>
		<dc:creator>Colin Eberhardt</dc:creator>
		<pubDate>Thu, 07 Apr 2011 14:31:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=139#comment-49277</guid>
		<description>Hi,

To have multiple relative source bindings you could update the code as per my multibinding solution:

http://www.scottlogic.co.uk/blog/colin/2010/08/silverlight-multibinding-updated-adding-support-for-elementname-and-twoway-binding/

To answer your second question, I like to provide working solutions to interesting problems, however I am not trying to create some all-encompassing framework of Silverlight extensions. Therefore my solutions often will not integrate together fully.

Regards, Colin E.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>To have multiple relative source bindings you could update the code as per my multibinding solution:</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/2010/08/silverlight-multibinding-updated-adding-support-for-elementname-and-twoway-binding/" rel="nofollow">http://www.scottlogic.co.uk/blog/colin/2010/08/silverlight-multibinding-updated-adding-support-for-elementname-and-twoway-binding/</a></p>
<p>To answer your second question, I like to provide working solutions to interesting problems, however I am not trying to create some all-encompassing framework of Silverlight extensions. Therefore my solutions often will not integrate together fully.</p>
<p>Regards, Colin E.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ozz</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/#comment-49269</link>
		<dc:creator>Ozz</dc:creator>
		<pubDate>Thu, 07 Apr 2011 13:32:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=139#comment-49269</guid>
		<description>Has this been addressed at all? It looked like a great solution but unusable if its not production ready.</description>
		<content:encoded><![CDATA[<p>Has this been addressed at all? It looked like a great solution but unusable if its not production ready.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Javier Cobo</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/#comment-49266</link>
		<dc:creator>Javier Cobo</dc:creator>
		<pubDate>Thu, 07 Apr 2011 12:53:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=139#comment-49266</guid>
		<description>I&#039;m a Silverlight newbie and I found your owesome tools googling for some solutions to my problems. At first sight your tools solve them, but then I&#039;ve got some new ones.

I&#039;m trying your code for relative binding and for multibinding, and I have the following questions:

1. How can I define relative binding for more than one property in a control ? ( for example, inside a listboxitem, I need to bind an Ischecked property of a control to the IsSelected property of the listboxitem, and at the same time, I need to bind the Width property of the same control to the Width of the listbox

2.- Why didn&#039;t you implemented relative binding in multibinding - or  did I miss it -?

thank uou</description>
		<content:encoded><![CDATA[<p>I&#8217;m a Silverlight newbie and I found your owesome tools googling for some solutions to my problems. At first sight your tools solve them, but then I&#8217;ve got some new ones.</p>
<p>I&#8217;m trying your code for relative binding and for multibinding, and I have the following questions:</p>
<p>1. How can I define relative binding for more than one property in a control ? ( for example, inside a listboxitem, I need to bind an Ischecked property of a control to the IsSelected property of the listboxitem, and at the same time, I need to bind the Width property of the same control to the Width of the listbox</p>
<p>2.- Why didn&#8217;t you implemented relative binding in multibinding &#8211; or  did I miss it -?</p>
<p>thank uou</p>
]]></content:encoded>
	</item>
</channel>
</rss>

