<?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 WPF</description>
	<lastBuildDate>Fri, 23 Jul 2010 03:32:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<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>
</channel>
</rss>
