<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Colin Eberhardt&#039;s Adventures in .NET &#187; styling</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/tag/styling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottlogic.co.uk/blog/colin</link>
	<description>Colin Eberhardt&#039;s Adventures in .NET</description>
	<lastBuildDate>Thu, 09 Feb 2012 10:21:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Circular ProgressBar Style using an Attached ViewModel</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/02/a-circular-progressbar-style-using-an-attached-viewmodel/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/02/a-circular-progressbar-style-using-an-attached-viewmodel/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 08:28:35 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[attached view model]]></category>
		<category><![CDATA[styling]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1189</guid>
		<description><![CDATA[This blog post describes how to re-template the Silverlight ProgressBar control to render a circular progress indicator. This approach uses an attached view model to circumnavigate some of the limitations of the ProgressBar design. This blog post describes the creation of the following funky styles for the ProgressBar (and a bit of a rant about [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F02%252Fa-circular-progressbar-style-using-an-attached-viewmodel%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22A%20Circular%20ProgressBar%20Style%20using%20an%20Attached%20ViewModel%20%23%22%20%7D);"></div>
<p><em>This blog post describes how to re-template the Silverlight ProgressBar control to render a circular progress indicator. This approach uses an attached view model to circumnavigate some of the limitations of the ProgressBar design.</em></p>
<p>This blog post describes the creation of the following funky styles for the <code>ProgressBar</code> (and a bit of a <strong>rant</strong> about just what is wrong with the way that &#8216;lookless&#8217; controls work!)</p>
<div style="text-align: center;"><object width="500" height="250" data="data:application/x-silverlight," type="application/x-silverlight-2" ><param name="source" value="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/CircularProgressBar.xap"/><a href="http://go.microsoft.com/fwlink/?LinkID=124807"  style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a></object></div>
<p>If you get sick of the spinning, click the pause button!</p>
<h2>Introduction</h2>
<p>A few days ago I answered a question on stack overflow which asked <a href="http://stackoverflow.com/questions/4871263/how-create-a-circular-style-progressbar/4871370#4871370">How create a Circular Style progressbar?</a> (sic)</p>
<p><a href="http://stackoverflow.com/questions/4871263/how-create-a-circular-style-progressbar/4871370#4871370"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/CircleProgressSO.png" alt="" title="CircleProgressSO" width="500" height="267" class="aligncenter size-full wp-image-1190" /></a></p>
<p>The answer which I gave, and most people seemed to agree with, was that to achieve this you would have to create your own control from &#8216;scratch&#8217;. I was happy that my answer was accepted, but at the same time a little unhappy that this should be the right answer. After all, Silverlight / WPF give you the power to create &#8216;lookless&#8217; controls, and what is a circular progress bar if it isn&#8217;t just another skin or &#8216;look&#8217; for the regular progress bar?</p>
<h2>What is wrong with the ProgressBar?</h2>
<p>If you look at the <a href="http://msdn.microsoft.com/en-us/library/dd334413%28VS.95%29.aspx">documentation for styling / templating the ProgressBar</a> you will find that this control expects the template to contain two elements, <code>ProgressBarTrack</code> and <code>ProgressBarIndicator</code>:</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/ProgressBarParts.png"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/ProgressBarParts.png" alt="" title="ProgressBarParts" width="534" height="135" class="aligncenter size-full wp-image-1197" /></a></p>
<p>What the ProgressBar does is, when the template is applied, in OnApplyTemplate, it locates the elements with the given names in order to update the visual state of the UI. You can use Reflector (quick, <a href="http://www.red-gate.com/products/dotnet-development/reflector/announcement">while it is still free</a>!)  to see how the state of these elements is updated in the <code>ProgressBar.SetProgressBarIndicatorLength</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetProgressBarIndicatorLength<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> minimum <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Minimum</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> maximum <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Maximum</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> num3 <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ElementTrack</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ElementIndicator</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        FrameworkElement parent <span style="color: #008000;">=</span> VisualTreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">GetParent</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ElementIndicator</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> FrameworkElement<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>parent <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> num4 <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ElementIndicator</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Margin</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span> <span style="color: #008000;">+</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ElementIndicator</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Margin</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Right</span><span style="color: #008000;">;</span>
            Border border <span style="color: #008000;">=</span> parent <span style="color: #0600FF; font-weight: bold;">as</span> Border<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>border <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                num4 <span style="color: #008000;">+=</span> border<span style="color: #008000;">.</span><span style="color: #0000FF;">Padding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span> <span style="color: #008000;">+</span> border<span style="color: #008000;">.</span><span style="color: #0000FF;">Padding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Right</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span>
            <span style="color: #008000;">&#123;</span>
                Control control <span style="color: #008000;">=</span> parent <span style="color: #0600FF; font-weight: bold;">as</span> Control<span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>control <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    num4 <span style="color: #008000;">+=</span> control<span style="color: #008000;">.</span><span style="color: #0000FF;">Padding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span> <span style="color: #008000;">+</span> control<span style="color: #008000;">.</span><span style="color: #0000FF;">Padding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Right</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> num5 <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsIndeterminate</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span>maximum <span style="color: #008000;">==</span> minimum<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #FF0000;">1.0</span> <span style="color: #008000;">:</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>num3 <span style="color: #008000;">-</span> minimum<span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #008000;">&#40;</span>maximum <span style="color: #008000;">-</span> minimum<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> num6 <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Max</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span> <span style="color: #FF0000;">0.0</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span>parent<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">-</span> num4<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ElementIndicator</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">=</span> num5 <span style="color: #008000;">*</span> num6<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>You can see in the above code that the various properties of the <code>ElementTrack</code> and <code>ElementIndicator</code> elements (the two named elements in the template) are being updated programmatically. This basically restricts the re-templating capabilities of the <code>ProgressBar</code> to ones where the &#8216;indicator&#8217; element has a width which is some proportion of its parent element. That is not very lookless!</p>
<p>So what is so bad about creating your own circular progress indicator from scratch? Firstly, there is the issue of object-oriented design principles and re-use. Secondly, and in my opinion much more importantly, is how this affects skinning. Templating allows you to radically change your UI simply by applying a new set of styles, see for example the <a href="http://silverlight.codeplex.com/wikipage?title=Silverlight%20Toolkit%20Overview%20Part%203&#038;referringTitle=Home">Silverlight Toolkit Themes</a>. Styles can change the value of any property of an element (including its template) but they cannot change the class itself! So, if you create a circular progress bar as a new control, you cannot interchange it with the standard <code>ProgressBar</code> simply by applying a theme.</p>
<h2>An Attached View Model</h2>
<p>OK, rant over. Time to fix the problem!</p>
<p>A few months ago I blogged about <a href="http://www.scottlogic.co.uk/blog/colin/2010/10/developing-a-lookless-silverlight-gauge-control-part-2/">how to create completely lookless controls using an attached view model</a>. The basic concept behind this approach is that the control itself should not include any logic which is tightly-coupled to a particular template, or &#8216;look&#8217;. This logic is still required, but is instead introduced into the template by means of an attached view model.</p>
<p>Typically the elements within a control&#8217;s template inherit the same DataContext as the control itself, i.e. whatever business object or view model you have bound to your UI. With the attached view model approach, a view model is attached to the root element in the template. On attachment, this view model acquires a reference to the ProgressBar, in order to adapt its properties, making it easier to render a circular indicator, and sets itself as the DataContext of the child elements:</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/AttachedViewModel.png"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/AttachedViewModel.png" alt="" title="AttachedViewModel" width="650" height="211" class="aligncenter size-full wp-image-1203" /></a></p>
<p>The view model is attached in XAMl as follows, as a result the DataContext of any element within the template is now the view model:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;ProgressBar&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- attach the view model --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:CircularProgressBarViewModel.Attach<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:CircularProgressBarViewModel</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:CircularProgressBarViewModel.Attach<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- the rest of the template now has CircularProgressBarViewModel as the DataContext --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>Becoming Attached</h2>
<p>The changed handler for the <code>Attach</code> property is given below. In summary, on attachment, the view model sets itself as the <code>DataContext</code> for the element it has been attached to. It then handlers the <code>Loaded</code> event which fires when the UI is fully constructed in order to locate the <code>ProgressBar</code> using <a href="http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/">Linq to VisualTree</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Change handler for the Attach property</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnAttachChanged<span style="color: #008000;">&#40;</span>DependencyObject d, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// set the view model as the DataContext for the rest of the template</span>
  FrameworkElement targetElement <span style="color: #008000;">=</span> d <span style="color: #0600FF; font-weight: bold;">as</span> FrameworkElement<span style="color: #008000;">;</span>
  CircularProgressBarViewModel viewModel <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span> <span style="color: #0600FF; font-weight: bold;">as</span> CircularProgressBarViewModel<span style="color: #008000;">;</span>
  targetElement<span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> viewModel<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// handle the loaded event</span>
  targetElement<span style="color: #008000;">.</span><span style="color: #0000FF;">Loaded</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>Element_Loaded<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Handle the Loaded event of the element to which this view model is attached</span>
<span style="color: #008080; font-style: italic;">/// in order to enable the attached</span>
<span style="color: #008080; font-style: italic;">/// view model to bind to properties of the parent element</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Element_Loaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  FrameworkElement targetElement <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> FrameworkElement<span style="color: #008000;">;</span>
  CircularProgressBarViewModel attachedModel <span style="color: #008000;">=</span> GetAttach<span style="color: #008000;">&#40;</span>targetElement<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// find the ProgressBar and associated it with the view model</span>
  var progressBar <span style="color: #008000;">=</span> targetElement<span style="color: #008000;">.</span><span style="color: #0000FF;">Ancestors</span><span style="color: #008000;">&lt;</span>ProgressBar<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Single</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> ProgressBar<span style="color: #008000;">;</span>
  attachedModel<span style="color: #008000;">.</span><span style="color: #0000FF;">SetProgressBar</span><span style="color: #008000;">&#40;</span>progressBar<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Once the view model is associated with the progress bar, it is able to compute properties which assist in the creation of a circular template, e.g. the angle used to represent a particular progress value.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Add handlers for the updates on various properties of the ProgressBar</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetProgressBar<span style="color: #008000;">&#40;</span>ProgressBar progressBar<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  _progressBar <span style="color: #008000;">=</span> progressBar<span style="color: #008000;">;</span>
  _progressBar<span style="color: #008000;">.</span><span style="color: #0000FF;">SizeChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> ComputeViewModelProperties<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  RegisterForNotification<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Value&quot;</span>, progressBar, <span style="color: #008000;">&#40;</span>d,e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> ComputeViewModelProperties<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  RegisterForNotification<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Maximum&quot;</span>, progressBar, <span style="color: #008000;">&#40;</span>d, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> ComputeViewModelProperties<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  RegisterForNotification<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Minimum&quot;</span>, progressBar, <span style="color: #008000;">&#40;</span>d, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> ComputeViewModelProperties<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  ComputeViewModelProperties<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008080; font-style: italic;">/// Add a handler for a DP change</span>
<span style="color: #008080; font-style: italic;">/// see: http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> RegisterForNotification<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName, FrameworkElement element, PropertyChangedCallback callback<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">//Bind to a dependency property  </span>
  Binding b <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Binding<span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> Source <span style="color: #008000;">=</span> element <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
  var prop <span style="color: #008000;">=</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DependencyProperty</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAttached</span><span style="color: #008000;">&#40;</span>
      <span style="color: #666666;">&quot;ListenAttached&quot;</span> <span style="color: #008000;">+</span> propertyName,
      <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span>,
      <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>UserControl<span style="color: #008000;">&#41;</span>,
      <span style="color: #008000;">new</span> PropertyMetadata<span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  element<span style="color: #008000;">.</span><span style="color: #0000FF;">SetBinding</span><span style="color: #008000;">&#40;</span>prop, b<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Thanks to Anoop for publishing a nice and simple method for <a href="http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html">registering for change notification of dependency properties</a> (what a pain that DPs do not also implement the INotifyPropertyChanged pattern!).</p>
<p>Each time one of the properties on the progress bar changes, the following method updates a few of the CLR properties exposed by the attached view model:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Re-computes the various properties that the elements in the template bind to.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> ComputeViewModelProperties<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_progressBar <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
  Angle <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>_progressBar<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">-</span> _progressBar<span style="color: #008000;">.</span><span style="color: #0000FF;">Minimum</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">360</span> <span style="color: #008000;">/</span> <span style="color: #008000;">&#40;</span>_progressBar<span style="color: #008000;">.</span><span style="color: #0000FF;">Maximum</span> <span style="color: #008000;">-</span> _progressBar<span style="color: #008000;">.</span><span style="color: #0000FF;">Minimum</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  CentreX <span style="color: #008000;">=</span> _progressBar<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
  CentreY <span style="color: #008000;">=</span> _progressBar<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualHeight</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
  Radius <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Min</span><span style="color: #008000;">&#40;</span>CentreX, CentreY<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  Diameter <span style="color: #008000;">=</span> Radius <span style="color: #008000;">*</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
  InnerRadius <span style="color: #008000;">=</span> Radius <span style="color: #008000;">*</span> HoleSizeFactor<span style="color: #008000;">;</span>
  Percent <span style="color: #008000;">=</span> Angle <span style="color: #008000;">/</span> <span style="color: #FF0000;">360</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The complete XAML for one of the styled progress bars seen at the top of this blog post is given below. Here you can see how the various UI elements within the template are bound to the attached view model:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;ProgressBar&quot;</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;PieProgressBarStyle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;ProgressBar&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #808080; font-style: italic;">&lt;!-- attach the view model --&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:CircularProgressBarViewModel.Attach<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:CircularProgressBarViewModel</span> <span style="color: #000066;">HoleSizeFactor</span>=<span style="color: #ff0000;">&quot;0.75&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:CircularProgressBarViewModel.Attach<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
          <span style="color: #808080; font-style: italic;">&lt;!-- a circular outline --&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Ellipse</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;{Binding Diameter}&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;{Binding Diameter}&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">Stroke</span>=<span style="color: #ff0000;">&quot;LightGray&quot;</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;Transparent&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">StrokeThickness</span>=<span style="color: #ff0000;">&quot;0.3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Ellipse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
          <span style="color: #808080; font-style: italic;">&lt;!-- a pie-piece that indicates the progress --&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:PiePiece</span> <span style="color: #000066;">CentreX</span>=<span style="color: #ff0000;">&quot;{Binding CentreX}&quot;</span> <span style="color: #000066;">CentreY</span>=<span style="color: #ff0000;">&quot;{Binding CentreY}&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">RotationAngle</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">WedgeAngle</span>=<span style="color: #ff0000;">&quot;{Binding Angle}&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">Radius</span>=<span style="color: #ff0000;">&quot;{Binding Radius}&quot;</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;LightBlue&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
          <span style="color: #808080; font-style: italic;">&lt;!-- progress as a percent --&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">util:GridUtils.RowDefinitions</span>=<span style="color: #ff0000;">&quot;*,3.5*,*&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">util:GridUtils.ColumnDefinitions</span>=<span style="color: #ff0000;">&quot;*,3.5*,*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Percent, StringFormat=0%}&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;DarkBlue&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">FontWeight</span>=<span style="color: #ff0000;">&quot;Bold&quot;</span> <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;20&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>(The template uses a <code>PiePiece</code> is a control I borrowed from a <a href="http://www.codeproject.com/KB/WPF/PieChartDataBinding.aspx">PieChart control</a> I created a few years back, and the <a href="http://www.scottlogic.co.uk/blog/colin/2010/12/a-simplified-grid-markup-for-silverlight-and-wpf/">simplified Grid syntax</a>)</p>
<p>We now have a circular ProgressBar! &#8230;</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/CircleProgress.png" alt="" title="CircleProgress" width="142" height="144" class="aligncenter size-full wp-image-1207" /></p>
<h2>Segmented Progress Bar</h2>
<p>For a bit of fun I extended the attached view model to allow for the easy construction of circular progress bar sthat are rendered as discrete segments. The <code>SegmentedProgressBarViewModel</code>, which is attached to the template exposes a collection of objects which allow the creation of a segmented indicator via an <code>ItemsControl</code>. For full details,download the blog sourcecode.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;ProgressBar&quot;</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;SegmentedProgressBarStyle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;ProgressBar&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #808080; font-style: italic;">&lt;!-- attach the view model --&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:CircularProgressBarViewModel.Attach<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:SegmentedProgressBarViewModel</span> <span style="color: #000066;">HoleSizeFactor</span>=<span style="color: #ff0000;">&quot;0.7&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:CircularProgressBarViewModel.Attach<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
          <span style="color: #808080; font-style: italic;">&lt;!-- render the segments --&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Segments}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                  <span style="color: #808080; font-style: italic;">&lt;!-- a grey segment --&gt;</span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:PiePiece</span> <span style="color: #000066;">CentreX</span>=<span style="color: #ff0000;">&quot;{Binding Parent.CentreX}&quot;</span> <span style="color: #000066;">CentreY</span>=<span style="color: #ff0000;">&quot;{Binding Parent.CentreY}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">RotationAngle</span>=<span style="color: #ff0000;">&quot;{Binding StartAngle}&quot;</span> <span style="color: #000066;">WedgeAngle</span>=<span style="color: #ff0000;">&quot;{Binding WedgeAngle}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Radius</span>=<span style="color: #ff0000;">&quot;{Binding Parent.Radius}&quot;</span> <span style="color: #000066;">InnerRadius</span>=<span style="color: #ff0000;">&quot;{Binding Parent.InnerRadius}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;LightGray&quot;</span> <span style="color: #000066;">Stroke</span>=<span style="color: #ff0000;">&quot;White&quot;</span> <span style="color: #000066;">Opacity</span>=<span style="color: #ff0000;">&quot;0.5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                  <span style="color: #808080; font-style: italic;">&lt;!-- a blue segment, with an Opacity bound to the view model --&gt;</span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:PiePiece</span> <span style="color: #000066;">CentreX</span>=<span style="color: #ff0000;">&quot;{Binding Parent.CentreX}&quot;</span> <span style="color: #000066;">CentreY</span>=<span style="color: #ff0000;">&quot;{Binding Parent.CentreY}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">RotationAngle</span>=<span style="color: #ff0000;">&quot;{Binding StartAngle}&quot;</span> <span style="color: #000066;">WedgeAngle</span>=<span style="color: #ff0000;">&quot;{Binding WedgeAngle}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Radius</span>=<span style="color: #ff0000;">&quot;{Binding Parent.Radius}&quot;</span> <span style="color: #000066;">InnerRadius</span>=<span style="color: #ff0000;">&quot;{Binding Parent.InnerRadius}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;DarkBlue&quot;</span> <span style="color: #000066;">Stroke</span>=<span style="color: #ff0000;">&quot;White&quot;</span> <span style="color: #000066;">Opacity</span>=<span style="color: #ff0000;">&quot;{Binding Opacity}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>                  
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The above markup results in the following style:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/SegmentedProgress.png" alt="" title="SegmentedProgress" width="135" height="135" class="aligncenter size-full wp-image-1208" /></p>
<p>The sourcecode for this blog includes a few other styles, including a &#8216;glass&#8217; effect which was borrowed from <a href="http://10rem.net/blog/2009/01/09/dazzling-silverlight-toolkit-pie-charts-with-overlays">Pete Brown&#8217;s blog post on Pie chart styling</a>.</p>
<h2>Sourcecode</h2>
<p>You can download the full sourcecode for this blog post: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/02/CircularProgressBar.zip'>CircularProgressBar.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/02/a-circular-progressbar-style-using-an-attached-viewmodel/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Using CSS Selectors for Styling in WPF</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/03/using-css-selectors-for-styling-in-wpf/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2009/03/using-css-selectors-for-styling-in-wpf/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 17:14:31 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[attached behaviour]]></category>
		<category><![CDATA[styling]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=193</guid>
		<description><![CDATA[When I first encountered WPF I was really impressed by its styling and templating features which are more powerful than anything else I had previously seen for desktop software development. The property-value pairing within styles instantly reminded me of CSS, however the WPF styles lack the most powerful feature of CSS &#8211; the selector. This [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2009%252F03%252Fusing-css-selectors-for-styling-in-wpf%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Using%20CSS%20Selectors%20for%20Styling%20in%20WPF%20%23%22%20%7D);"></div>
<p>When I first encountered WPF I was really impressed by its styling and templating features which are more powerful than anything else I had previously seen for desktop software development. The property-value pairing within styles instantly reminded me of CSS, however the WPF styles lack the most powerful feature of CSS &#8211; the selector. This blog post describes an attached behaviour for styling WPF application using CSS selectors.</p>
<p>Let&#8217;s first look at the anatomy of CSS. A CSS stylesheet is composed of a number of <em>rules</em>, each rule is composed of a <em>selector</em> which provides a pattern which the CSS selector engine matches against the target HTML document to locate nodes which to apply the style to. The styling information itself is defined within a <em>declaration block</em>, which consists of a number of comma-separated <em>declarations</em> enclosed within braces. The declaration block looks quite similar to a style within WPF &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">div <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;TextBlock&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;FontSize&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So let&#8217;s ignore the declaration block for the moment and concentrate on the interesting part, the CSS selector.</p>
<p>Selectors define a pattern which is matched, the information which the selector contains can include the element (tag) type, class or ID. More complex selectors might also include element attribute values or use pseudo-classes. We can easily find parallel concepts to these within WPF &#8230;</p>
<ul>
<li>HTML element type (e.g. div, p, a) => dependency object type (e.g. TextBlock, Button)</li>
<li>HTML ID (#menu) => dependency object Name (e.g. x:Name=&#8221;menu&#8221;)</li>
<li>HTML class (.title) => attached class property (e.g. css:Css.Class=&#8221;menu&#8221;)</li>
</ul>
<p>The only concept for which there really is no correspondent in WPF is CSS class. This can easily be introduced via an attached property.</p>
<p>So now that we have the concept mapping, all we need now is a CSS selector engine that can apply our selector in order to extract matching objects from our visual / logical tree. Luckily I found a .NET CSS selector engine implementation in codeplex called Fizzler, you can read about it on the <a href="http://colinramsay.co.uk/diary/2008/10/19/fizzler-a-css-selector-engine-for-c-sharp/">authors blog</a>, or go straight to codeplex to <a href="http://code.google.com/p/fizzler/">download it</a>. The author of this engine, Colin Ramsay, had done a great job of the implementation and had also written an exhaustive set of unit tests. The engine itself was tightly coupled to HtmlDocumentNodes, however I was able to slide a simple interface between the selector engine and the HTML document types as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IDocumentNode
<span style="color: #008000;">&#123;</span>
    IAttributeCollection Attributes <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    List<span style="color: #008000;">&lt;</span>IDocumentNode<span style="color: #008000;">&gt;</span> ChildNodes <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    IDocumentNode ParentNode <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    IDocumentNode PreviousSibling <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> <span style="color: #6666cc; font-weight: bold;">Class</span> <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #6666cc; font-weight: bold;">bool</span> IsElement <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IAttributeCollection
<span style="color: #008000;">&#123;</span>
    IAttribute <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#91;</span><span style="color: #6666cc; font-weight: bold;">string</span> name<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IAttribute
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> Value <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Again, thanks to the extensive unit tests, this refactor only took ~ 40 minutes.</p>
<p>The next job was to create an implementation of the above interfaces that walks the visual / logical trees, both were pretty easy to implement using the LogicalTreeHelper and VisualTreeHelper classes. With these classes implemented, I was able to query the visual tree using selectors as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">SelectorEngine engine <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SelectorEngine<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
IList<span style="color: #008000;">&lt;</span>IDocumentNode<span style="color: #008000;">&gt;</span> matchingNodes <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.form TextBlock.warning&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// apply the style to all matching nodes</span>
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>DependencyObjectNode matchingNode <span style="color: #0600FF; font-weight: bold;">in</span> matchingNodes<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In the above example, the selector engine returns any TextBlock with the class &#8216;warning&#8217;, which is a descendant of any element with the class &#8216;form&#8217;. This is certainly a novel way of querying the visual tree! </p>
<p>The next step is to define the declaration block and apply the style which they define to any matching elements.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> StyleDeclarationBlock <span style="color: #008000;">:</span> List<span style="color: #008000;">&lt;</span>StyleDeclaration<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> StyleDeclaration
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Property <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Value <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>We can construct style declarations within XAML as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;BorderThickness&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;BorderBrush&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Black&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;CornerRadius&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Margin&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;10,10,10,10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The process for applying a style is pretty straightforward, the only subtle complication is that styles, once applied are frozen (i.e. immutable). Therefore, each time an element is selected via our selector engine, we clone the existing style then merge it with the new one. See the attached source-code for details.</p>
<p>Putting it all together involves the creation of an <a href="http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx">Attached Behaviour</a> which associates a stylesheet with an element within our XAML. When the element is loaded, the rules within the stylesheet are applied. Let&#8217;s look at a simple demo &#8230;</p>
<p>Here is a simple UI defined in XAML. Note the absence of any styling information:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">css:Css.Class</span>=<span style="color: #ff0000;">&quot;form&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>                
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">Orientation</span>=<span style="color: #ff0000;">&quot;Vertical&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">css:Css.Class</span>=<span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;User Details&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>    
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
&nbsp;
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>        
&nbsp;
&nbsp;
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">css:Css.Class</span>=<span style="color: #ff0000;">&quot;mandatory&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Name&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span>  <span style="color: #000066;">css:Css.Class</span>=<span style="color: #ff0000;">&quot;mandatory&quot;</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Anthony Gatto&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Age&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span>  <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;36&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Profession&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span>  <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Performer&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>Submit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The resulting UI looks like this:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/03/no-style.png" alt="no-style" title="no-style" width="299" height="199" class="alignnone size-full wp-image-197" /></p>
<p>Now we apply our stylesheet to the root element in our XAML:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleSheet</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;cssStyles&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleSheet.Rules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleRule</span> <span style="color: #000066;">Selector</span>=<span style="color: #ff0000;">&quot;.form Grid *&quot;</span> <span style="color: #000066;">SelectorType</span>=<span style="color: #ff0000;">&quot;LogicalTree&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleRule.DeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Margin&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;4,4,4,4&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleRule.DeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleRule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleRule</span> <span style="color: #000066;">Selector</span>=<span style="color: #ff0000;">&quot;.form TextBlock.mandatory&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleRule.DeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Foreground&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Red&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleRule.DeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleRule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleRule</span> <span style="color: #000066;">Selector</span>=<span style="color: #ff0000;">&quot;Border.form&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleRule.DeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;BorderThickness&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;BorderBrush&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Black&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;CornerRadius&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Margin&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;10,10,10,10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleRule.DeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleRule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleRule</span> <span style="color: #000066;">Selector</span>=<span style="color: #ff0000;">&quot;.form .title&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleRule.DeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;HorizontalAlignment&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;HorizontalContentAlignment&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Background&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;DarkBlue&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Foreground&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;White&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;FontSize&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;13&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Padding&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;3,3,3,3&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;css:StyleDeclaration</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;FontWeight&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Bold&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleDeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleRule.DeclarationBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleRule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleSheet.Rules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/css:StyleSheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
...
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">css:Css.StyleSheet</span>=<span style="color: #ff0000;">&quot;{StaticResource cssStyles}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>Which styles our WPF application as follows:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/03/styled.png" alt="styled" title="styled" width="299" height="299" class="alignnone size-full wp-image-199" /></p>
<p>Let&#8217;s look at those selector in a little more detail. The first one, <code>&lt;css:StyleRule Selector=".form Grid *" SelectorType="LogicalTree"&gt;</code> is quite interesting. Here we are selecting everything within the Grid within our form and applying a margin. Note that here we are using a selector on the logical tree, otherwise we will also match elements within the control template of our TextBoxes and Buttons and will hence apply the margin internally within these elements also. However, in other contexts the ability to be able to apply styles within a controls template is a powerful concept.</p>
<p>The next selector  <code>&lt;css:StyleRule Selector=".form TextBlock.mandatory"&gt;</code> styles our mandatory fields, the others styling the form title and borders.</p>
<p>The above is a pretty simple demonstration. The Fizzler CSS engine implements most of the CSS2.1 selectors and also some CSS3, so much more complex selector logic is possible. You can also include multiple comma-separated selectors on a rule.</p>
<p>Currently this code is not what I would call production-ready. It is just an idea that has been bugging me for a while that I wanted to get out of my head and share. I think this approach has a number of merits and it overcomes some of the restrictions in WPF styling. It provides a much more flexible mechanism for styling your UI, WPF gives you explicit styles and implicit styles, neither of which match the power of the CSS selector. This approach also adds the ability to merge styles, so if an element is matched by multiple rules these styles are merged. Furthermore, this approach does not impose the restriction that styles must have a TargetType, if the matching element does not have a corresponding dependency property, it simply does not pick up that piece of style information.</p>
<p>However, this implementation is not quite complete. I have not considered how to apply styles to dynamically generated content, ItemsControls for example. </p>
<p>I would be very interested to hear peoples opinions on this idea. Do you thin kit has potential? Perhaps if it was ported to Silverlight &#8211; it could even include mapping rules form &#8216;real&#8217; CSS documents so that your Silverlight application could share style information with the rest of the web page which hosts it. But that is a job for another day &#8230;</p>
<p>You can download the code for this blog post here: <a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/03/wpfcssstyling.zip">wpfcssstyling.zip</a></p>
<p><span style="color:red">UPDATE</span> &#8211; the concept of using CSS to style WPF and Silvelight applications has been discussed on the <a href="http://groups.google.com/group/wpf-disciples/t/251c90f31f603a8">WPF Discisples Group</a>.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2009/03/using-css-selectors-for-styling-in-wpf/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Styling hard-to-reach elements in control templates with attached behaviours</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 15:29:20 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[styling]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=118</guid>
		<description><![CDATA[OK, the title of this blog post is not very snappy, but it is not an easy problem to describe in a few short words. Here&#8217;s the rub, the WPF DataGrid has a select-all button located in the top-left corner, just like Excel and many other grid controls / applications. However, with the default style, [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2009%252F02%252Fstyling-hard-to-reach-elements-in-control-templates-with-attached-behaviours%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Styling%20hard-to-reach%20elements%20in%20control%20templates%20with%20attached%20behaviours%20%23%22%20%7D);"></div>
<p>OK, the title of this blog post is not very snappy, but it is not an easy problem to describe in a few short words. Here&#8217;s the rub, the WPF DataGrid has a select-all button located in the top-left corner, just like Excel and many other grid controls / applications. However, with the default style, this button is barely visible and I would not be surprised if a user of the grid failed to see it.</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/02/select-all.png"><img class="alignnone size-full wp-image-119" title="select-all" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/02/select-all.png" alt="select-all" width="418" height="220" /></a></p>
<p>Unfortunately restyling this button is not all that straightforward. The more complex WPF controls like the DataGrid and ListView typically expose the template used to construct, and the style applied to their various visual elements.  However, the DataGrid does not expose a style or template for the select-all button.</p>
<p>Fortunately, all is not lost. With WPF, if a control does not expose a style for one of its component parts, you can replicate and replace its entire template. Sometimes this approach feels a little heavy-handed &#8230; &#8220;I can&#8217;t seem to find a way to fit those flashy alloy wheels to my car, so I&#8217;ll just by a new car that already has the wheels I like&#8221;.</p>
<p>I want to explore a more lightweight approach.</p>
<p>Whilst it is usually preferable to modify a controls template from XAML, in cases like this, I prefer the more concise approach of modifying them in code-behind. In order to do this, you need to handle the <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.loaded.aspx">FrameworkElement.Loaded</a> event on the control whos template you wish to modify. This event is fired after the control&#8217;s visual tree has been constructed, to verify this add a breakpoint in the event handler and inspect the visual tree with <a href="http://www.codeproject.com/KB/WPF/MoleForWPF.aspx">Mole</a>. The image below shows the visual tree of my DataGrid which I have expanded to locate the select-all button.</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/02/datagrid-mole.png"><img class="alignnone size-full wp-image-120" title="datagrid-mole" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/02/datagrid-mole.png" alt="datagrid-mole" width="343" height="297" /></a></p>
<p>You can see that the button is the first element, four steps down the visual tree, therefore it should be easy to locate by walking the visual tree. Once it has been reached we can simply replace its template to the one which we desire:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Grid_Loaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    DependencyObject dep <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> DependencyObject<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Navigate down the visual tree to the button</span>
    <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #008000;">&#40;</span>dep <span style="color: #008000;">is</span> Button<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        dep <span style="color: #008000;">=</span> VisualTreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">GetChild</span><span style="color: #008000;">&#40;</span>dep, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    Button button <span style="color: #008000;">=</span> dep <span style="color: #0600FF; font-weight: bold;">as</span> Button<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// apply our new template</span>
    <span style="color: #6666cc; font-weight: bold;">object</span> res <span style="color: #008000;">=</span> FindResource<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SelectAllButtonTemplate&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    button<span style="color: #008000;">.</span><span style="color: #0000FF;">Template</span> <span style="color: #008000;">=</span> res <span style="color: #0600FF; font-weight: bold;">as</span> ControlTemplate
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The template SelectAllButtonTemplate is simply defined as a resource of our Window.</p>
<p>This works just fine for our single grid, but what if we want to use the same trick on multiple DataGrids? (Besides, we have code-behind, which in WPF is a bit of a dirty word!).</p>
<p>The <a href="http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx">Attached Behaviour</a> pattern is a useful WPF pattern that proves very useful in this situation. In brief, attached properties add state to a your WPF elements, whereas attached behaviours add behaviour. When an attached property becomes associated with a dependency object, an event is raised. We can handle this event and register handlers on the events of the object being attached to, in this case, our DataGrid&#8217;s Loaded event.</p>
<p>The following code is our attached behaviour in its entirety:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> DataGridStyleBehaviour
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080;">#region attached property</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> ControlTemplate GetSelectAllButtonTemplate<span style="color: #008000;">&#40;</span>DataGrid obj<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>ControlTemplate<span style="color: #008000;">&#41;</span>obj<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>SelectAllButtonTemplateProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetSelectAllButtonTemplate<span style="color: #008000;">&#40;</span>DataGrid obj, ControlTemplate value<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        obj<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>SelectAllButtonTemplateProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty SelectAllButtonTemplateProperty <span style="color: #008000;">=</span>
        DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAttached</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SelectAllButtonTemplate&quot;</span>,
        <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ControlTemplate<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DataGridStyleBehaviour<span style="color: #008000;">&#41;</span>,
        <span style="color: #008000;">new</span> UIPropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span>, OnSelectAllButtonTemplateChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region property behaviour</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// property change event handler for SelectAllButtonTemplate</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnSelectAllButtonTemplateChanged<span style="color: #008000;">&#40;</span>
        DependencyObject depObj, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        DataGrid grid <span style="color: #008000;">=</span> depObj <span style="color: #0600FF; font-weight: bold;">as</span> DataGrid<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>grid <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// handle the grid's Loaded event</span>
        grid<span style="color: #008000;">.</span><span style="color: #0000FF;">Loaded</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>Grid_Loaded<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Handles the DataGrid's Loaded event</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Grid_Loaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        DataGrid grid <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> DataGrid<span style="color: #008000;">;</span>
        DependencyObject dep <span style="color: #008000;">=</span> grid<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Navigate down the visual tree to the button</span>
        <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #008000;">&#40;</span>dep <span style="color: #008000;">is</span> Button<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            dep <span style="color: #008000;">=</span> VisualTreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">GetChild</span><span style="color: #008000;">&#40;</span>dep, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        Button button <span style="color: #008000;">=</span> dep <span style="color: #0600FF; font-weight: bold;">as</span> Button<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// apply our new template</span>
        ControlTemplate template <span style="color: #008000;">=</span> GetSelectAllButtonTemplate<span style="color: #008000;">&#40;</span>grid<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        button<span style="color: #008000;">.</span><span style="color: #0000FF;">Template</span> <span style="color: #008000;">=</span> template<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This attached property can be used as illustrated below, where we apply the SelectAllButtonTemplate from our Window&#8217;s resources to a DataGrid:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> ...</span>
<span style="color: #009900;">    <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;BBC News RSS&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;400&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;XmlDataProvider</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;NewsFeed&quot;</span></span>
<span style="color: #009900;">                     <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml&quot;</span>  </span>
<span style="color: #009900;">                     <span style="color: #000066;">XPath</span>=<span style="color: #ff0000;">&quot;//item&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;SelectAllButtonTemplate&quot;</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type Button}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span>  <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;Border&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;Pink&quot;</span> </span>
<span style="color: #009900;">                            <span style="color: #000066;">SnapsToDevicePixels</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Polygon</span>   <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;Arrow&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Right&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Bottom&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;8,8,3,3&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">Opacity</span>=<span style="color: #ff0000;">&quot;0.15&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;Black&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">Stretch</span>=<span style="color: #ff0000;">&quot;Uniform&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">Points</span>=<span style="color: #ff0000;">&quot;0,10 10,10 10,0&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>            
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dg:DataGrid</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;dataGrid&quot;</span> <span style="color: #000066;">AutoGenerateColumns</span>=<span style="color: #ff0000;">&quot;False&quot;</span> </span>
<span style="color: #009900;">                     <span style="color: #000066;">local:DataGridStyleBehaviour.SelectAllButtonTemplate</span>=<span style="color: #ff0000;">&quot;{StaticResource SelectAllButtonTemplate}&quot;</span></span>
<span style="color: #009900;">                     <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Source={StaticResource NewsFeed}}&quot;</span> <span style="color: #000066;">RowHeaderWidth</span>=<span style="color: #ff0000;">&quot;25&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dg:DataGrid.Columns<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dg:DataGridTextColumn</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Title&quot;</span> <span style="color: #000066;">Binding</span>=<span style="color: #ff0000;">&quot;{Binding XPath=title}&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dg:DataGridHyperlinkColumn</span>   <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Url&quot;</span> <span style="color: #000066;">Binding</span>=<span style="color: #ff0000;">&quot;{Binding XPath=link}&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dg:DataGridTextColumn</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Date&quot;</span> <span style="color: #000066;">Binding</span>=<span style="color: #ff0000;">&quot;{Binding XPath=pubDate}&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dg:DataGrid.Columns<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dg:DataGrid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The result is a beautiful pink select-all button that I don&#8217;t think any user would miss in a hurry:</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/02/pinkbutton.png"><img class="alignnone size-full wp-image-121" title="pinkbutton" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/02/pinkbutton.png" alt="pinkbutton" width="368" height="205" /></a></p>
<p>And a clean code-behind file for our Window <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>You can download the demo project for this blog post in the following zip file, <a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/02/wpfstylinghiddenelements.zip">wpfstylinghiddenelements</a>.</p>
<p>One last thing &#8230; if you are implementing a WPF / Silverlight control, please expose the styles and templates for all component parts of your control!</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

