<?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: Styling hard-to-reach elements in control templates with attached behaviours</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/</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: Ranch</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comment-95549</link>
		<dc:creator>Ranch</dc:creator>
		<pubDate>Fri, 16 Dec 2011 17:20:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118#comment-95549</guid>
		<description>Thank you! I have been struggling with this for last couple of days. They key part i was missing is the Loaded Event subscription then find the Template Element. Thanks again!</description>
		<content:encoded><![CDATA[<p>Thank you! I have been struggling with this for last couple of days. They key part i was missing is the Loaded Event subscription then find the Template Element. Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dominic</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comment-23678</link>
		<dc:creator>dominic</dc:creator>
		<pubDate>Thu, 28 Oct 2010 16:13:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118#comment-23678</guid>
		<description>This was the missing link in completing my grid look and I was suprised at how little info there is on the net. Elegantly done. So glad I stumbled on this big thanks!</description>
		<content:encoded><![CDATA[<p>This was the missing link in completing my grid look and I was suprised at how little info there is on the net. Elegantly done. So glad I stumbled on this big thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin Eberhardt</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comment-12020</link>
		<dc:creator>Colin Eberhardt</dc:creator>
		<pubDate>Wed, 09 Jun 2010 10:46:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118#comment-12020</guid>
		<description>Thanks for the update :-)</description>
		<content:encoded><![CDATA[<p>Thanks for the update <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thrash505</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comment-11875</link>
		<dc:creator>Thrash505</dc:creator>
		<pubDate>Fri, 04 Jun 2010 20:37:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118#comment-11875</guid>
		<description>I&#039;ve updated the code a little to account for some blind spots. It seems that if the items collection is empty the previous code will first get an error when it tries to call GetChild on an element with no children. I fixed that with a quick if statement, but another issue was that the Loaded event doesn&#039;t seem to fire when items finally are added. And apparently the button were trying to template doesn&#039;t exist if there are no items in the items collection. So I fixed the code to handle the CLR LayoutUpdated event instead which does seem to fire off when items are finally added, and when they are initially there. I also made sure the button we are trying to template has its command property set to the selectall command. I also removed the handler once we&#039;ve applied the template. Thanks Colin for the great post!

Here&#039;s the updated version:
private static void OnSelectAllButtonTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    DataGrid dataGrid = d as DataGrid;
    if (dataGrid == null)
    {
        return;
    }

    EventHandler handler = null;
    handler = delegate
    {
        DependencyObject dep = dataGrid;
        while (dep != null &amp;&amp; VisualTreeHelper.GetChildrenCount(dep) != 0
            &amp;&amp; !(dep is Button &amp;&amp; ((Button)dep).Command == DataGrid.SelectAllCommand))
        {
            dep = VisualTreeHelper.GetChild(dep, 0);
        }

        Button button = dep as Button;
        if (button != null)
        {
            ControlTemplate template = GetSelectAllButtonTemplate(dataGrid);
            button.Template = template;
            dataGrid.LayoutUpdated -= handler;
        }
    };

    dataGrid.LayoutUpdated += handler;
}</description>
		<content:encoded><![CDATA[<p>I&#8217;ve updated the code a little to account for some blind spots. It seems that if the items collection is empty the previous code will first get an error when it tries to call GetChild on an element with no children. I fixed that with a quick if statement, but another issue was that the Loaded event doesn&#8217;t seem to fire when items finally are added. And apparently the button were trying to template doesn&#8217;t exist if there are no items in the items collection. So I fixed the code to handle the CLR LayoutUpdated event instead which does seem to fire off when items are finally added, and when they are initially there. I also made sure the button we are trying to template has its command property set to the selectall command. I also removed the handler once we&#8217;ve applied the template. Thanks Colin for the great post!</p>
<p>Here&#8217;s the updated version:<br />
private static void OnSelectAllButtonTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)<br />
{<br />
    DataGrid dataGrid = d as DataGrid;<br />
    if (dataGrid == null)<br />
    {<br />
        return;<br />
    }</p>
<p>    EventHandler handler = null;<br />
    handler = delegate<br />
    {<br />
        DependencyObject dep = dataGrid;<br />
        while (dep != null &amp;&amp; VisualTreeHelper.GetChildrenCount(dep) != 0<br />
            &amp;&amp; !(dep is Button &amp;&amp; ((Button)dep).Command == DataGrid.SelectAllCommand))<br />
        {<br />
            dep = VisualTreeHelper.GetChild(dep, 0);<br />
        }</p>
<p>        Button button = dep as Button;<br />
        if (button != null)<br />
        {<br />
            ControlTemplate template = GetSelectAllButtonTemplate(dataGrid);<br />
            button.Template = template;<br />
            dataGrid.LayoutUpdated -= handler;<br />
        }<br />
    };</p>
<p>    dataGrid.LayoutUpdated += handler;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin Eberhardt</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comment-5480</link>
		<dc:creator>Colin Eberhardt</dc:creator>
		<pubDate>Fri, 13 Nov 2009 17:17:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118#comment-5480</guid>
		<description>Hi Jay,

Yes, Loaded gets fired quiet often - good idea about removing the event handler after the style has been applied.

Thanks, Colin E.</description>
		<content:encoded><![CDATA[<p>Hi Jay,</p>
<p>Yes, Loaded gets fired quiet often &#8211; good idea about removing the event handler after the style has been applied.</p>
<p>Thanks, Colin E.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jay</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comment-5366</link>
		<dc:creator>Jay</dc:creator>
		<pubDate>Tue, 10 Nov 2009 15:23:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118#comment-5366</guid>
		<description>Hi Colin,

I used this implementation to walk the visual tree of a Templated element in the loaded event, but i have one doubt. The loaded event gets fired several times during the lifetime of the application, not only in the initialization phase, so that code is repeated several times, right? Removing the handler to the loaded event would be a good solution to this (because after the init. we wouldn&#039;t need to template the button)?</description>
		<content:encoded><![CDATA[<p>Hi Colin,</p>
<p>I used this implementation to walk the visual tree of a Templated element in the loaded event, but i have one doubt. The loaded event gets fired several times during the lifetime of the application, not only in the initialization phase, so that code is repeated several times, right? Removing the handler to the loaded event would be a good solution to this (because after the init. we wouldn&#8217;t need to template the button)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin Eberhardt</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comment-4026</link>
		<dc:creator>Colin Eberhardt</dc:creator>
		<pubDate>Thu, 01 Oct 2009 09:26:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118#comment-4026</guid>
		<description>Will,

Nice solution ... thanks for posting it.

Regards, Colin E.</description>
		<content:encoded><![CDATA[<p>Will,</p>
<p>Nice solution &#8230; thanks for posting it.</p>
<p>Regards, Colin E.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: WillH</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comment-4025</link>
		<dc:creator>WillH</dc:creator>
		<pubDate>Thu, 01 Oct 2009 08:07:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118#comment-4025</guid>
		<description>Hi Colin,

I fixed my problem, needed to remove the command. I added an answer to a StackOverflow question I asked here:

http://stackoverflow.com/questions/1493491/wpf-datagrid-select-all-button-unselect-all-too

Might be of use to readers of your blog.
Cheers,
Will</description>
		<content:encoded><![CDATA[<p>Hi Colin,</p>
<p>I fixed my problem, needed to remove the command. I added an answer to a StackOverflow question I asked here:</p>
<p><a href="http://stackoverflow.com/questions/1493491/wpf-datagrid-select-all-button-unselect-all-too" rel="nofollow">http://stackoverflow.com/questions/1493491/wpf-datagrid-select-all-button-unselect-all-too</a></p>
<p>Might be of use to readers of your blog.<br />
Cheers,<br />
Will</p>
]]></content:encoded>
	</item>
</channel>
</rss>

