<?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>Mindscape Blog &#187; WPF</title>
	<atom:link href="http://www.mindscapehq.com/blog/index.php/category/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mindscapehq.com/blog</link>
	<description>The official blog of Mindscape</description>
	<lastBuildDate>Fri, 14 Jun 2013 05:34:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>WPF Elements 5.1 Released!</title>
		<link>http://www.mindscapehq.com/blog/index.php/2012/05/31/wpf-elements-5-1-released/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2012/05/31/wpf-elements-5-1-released/#comments</comments>
		<pubDate>Thu, 31 May 2012 10:39:23 +0000</pubDate>
		<dc:creator>John-Daniel Trask</dc:creator>
				<category><![CDATA[Products]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF Elements]]></category>
		<category><![CDATA[Data Grid]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[Stock Charts]]></category>
		<category><![CDATA[WPF Candle Stick Charts]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=5043</guid>
		<description><![CDATA[We&#8217;re really pleased to be releasing WPF Elements 5.1 today with new controls, high powered performance improvements and new features. If you&#8217;re a WPF developer we think you&#8217;ll love the full WPF Elements Suite. Here&#8217;s a high level run through of key improvements in 5.1. Data Grid Enhancements The WPF Data Grid control in WPF [...]]]></description>
				<content:encoded><![CDATA[<p>We&#8217;re really pleased to be releasing <a href="http://www.mindscapehq.com/products/wpfelements">WPF Elements</a> 5.1 today with new controls, high powered performance improvements and new features. If you&#8217;re a WPF developer we think you&#8217;ll love the full WPF Elements Suite. Here&#8217;s a high level run through of key improvements in 5.1.</p>
<p><strong>Data Grid Enhancements</strong></p>
<p><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/05/datagrid.gif" alt="WPF Datagrid control" title="WPF Datagrid control" width="718" height="190" class="alignnone size-full wp-image-5063" /></p>
<p>The <a href="http://www.mindscapehq.com/products/wpfelements/controls/data-grid">WPF Data Grid</a> control in WPF Elements 5.1 is a much improved beast when compared with the 5.0 release. We had a lot of feedback from developers and we have been listening. Improvements in the 5.1 Data grid include:</p>
<ul>
<li>Grouping support with support for multi-level grouping and the ability to expand and collapse groups</li>
<li>Exporting support (CSV exporting built in)</li>
<li>Custom column sorting is supported</li>
<li>Additional cell editing modes (e.g. double click to edit, etc)</li>
<li>Row selection mode when clicking on a row header</li>
<li>Support for hierarchical data in the same way as a TreeView control</li>
<li>Allow users to add new rows which adds items to the underlying collection</li>
<li>Binding to DataTable and IBindingList data sources is now supported</li>
<li>RowHeaderTemplate support added for custom row headers</li>
<li>ColumnHeaderTemplate support for custom column headers</li>
<li>DataGrid.DisplayMemberBinding support for more complex binding scenarios without needing custom cell templates</li>
<li>Background and foreground colors can be set for rows, columns or even individual cells</li>
<li>Programmatically control scroll position (e.g. scroll to selected row, or end, etc)</li>
<li>Auto sizing and * (star) sizing support for column widths</li>
</ul>
<p>Of course these are just the key new features on top of our already super fast virtualized data grid engine.</p>
<p><strong>Charting Enhancements</strong></p>
<p>Along with the new charts detailed below, we made many improvements to the existing charts. The key focus for this release with regards to charts: Speed. LOTS OF SPEED. We already had pretty good charting performance (one customer compared against 10 different WPF Charting options and found we provided the best speed + features offering. They&#8217;ll be impressed with this release then!).</p>
<p><center><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/05/wpfchartsspeedup.gif" alt="WPF Charting Speed - Supremely fast!" title="WPF Charting Speed - Supremely fast!" width="718" height="469" class="alignnone size-full wp-image-5061" /></center></p>
<p><center><em>WPF Elements 5.1 charts can render millions of data points in a matter of milliseconds!</em></center></p>
<p>We&#8217;ll be posted more about these improvements in the coming days. Improvements in performance are across all chart types &#8211; you don&#8217;t need to choose between features and performance with WPF Elements!</p>
<p>Other charting enhancements include:</p>
<ul>
<li>BarSeries, StackedBarSeries and ScatterSeries now support millions of data points.</li>
<li>Setting ChartAxis.IsSliderVisible to true will display the built-in axis slider. This is an alternate way for the user to zoom and pan the chart.</li>
<li>You can now specify the starting value of the logarithmic axis scale.</li>
<li>MarkedStripeGrid supports both vertical and horizontal orientations.</li>
<li>You can now specify the logical position of the baseline in bar charts.</li>
<li>You can now specify the number of minor tick marks rendered between major tick marks.</li>
<li>Setting the DataSeries.IsShownInLegend property can hide the legend item for that series.</li>
</ul>
<p><strong>New Chart: Boxplot</strong></p>
<p>WPF Elements now includes the <a href="http://www.mindscapehq.com/products/wpfelements/controls/wpf-boxplot">WPF Boxplot</a>. It&#8217;s built on the same high performance core charting engine as all the other charts in WPF Elements.</p>
<p><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/05/WpfBoxPlotChart.jpg" alt="WPF Box Plot Chart" title="WPF Box Plot Chart" width="718" height="166" class="alignnone size-full wp-image-5053" /></p>
<p><strong>New Chart: Candle Stick</strong></p>
<p>WPF Elements users also get the <a href="http://www.mindscapehq.com/products/wpfelements/controls/wpf-candlestick-chart">WPF Candle Stick chart</a>. A handy chart for financial data and, like all other charts, super fast as well!</p>
<p><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/05/WPFCandleStickChart.jpg" alt="WPF Candle Stick Chart" title="WPF Candle Stick Chart" width="718" height="166" class="alignnone size-full wp-image-5054" /></p>
<p><strong>New Chart: Stock Chart</strong></p>
<p>The third new chart type added is the <a href="http://www.mindscapehq.com/products/wpfelements/controls/wpf-stock-chart">WPF Stock Chart</a>. Guess what? It&#8217;s also super fast and built on the same engine as the other charts!</p>
<p><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/05/WPFStockChart.jpg" alt="WPF Stock Chart" title="WPF Stock Chart" width="718" height="166" class="alignnone size-full wp-image-5055" /></p>
<p><strong>It&#8217;s not all charts and grids!</strong></p>
<p>While the Data Grid and Charts got a good number of enhancements and performance boosts, we love all our controls the same. A few of the other changes found across the suite are:</p>
<ul>
<li>You can now template the various scheduler dialog boxes. This is useful for providing custom styles or for localizing the labels.</li>
<li>Added a new IntegerUpDown control.</li>
<li>Added PercentageTextBox control.</li>
<li>Some more free WPF controls included in our free suite (we&#8217;ll post more about this later)</li>
</ul>
<p><strong>Dig in, build awesome apps</strong></p>
<p>Last but not least is that we&#8217;ve added more samples to our sample explorer that&#8217;s included in all releases of WPF Elements. You can explore new functionality, examine the code used to make that sample and adjust the look and feel from within the sample explorer. Once you&#8217;ve installed WPF Elements you can find the Sample Explorer in the Start Menu. If you need a hand getting started, <a href="http://www.mindscapehq.com/forums/forum/15">we would love to help</a>.</p>
<p><a href="http://www.mindscapehq.com/products/wpfelements/download">Get the 60 day free trial of WPF Elements 5.1 </a><br />
<a href="http://www.mindscapehq.com/store/myaccount">Existing customers can download 5.1 from their account</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2012/05/31/wpf-elements-5-1-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caliburn Micro Part 5: The Window Manager</title>
		<link>http://www.mindscapehq.com/blog/index.php/2012/03/13/caliburn-micro-part-5-the-window-manager/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2012/03/13/caliburn-micro-part-5-the-window-manager/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 06:59:29 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[Caliburn Micro]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=4680</guid>
		<description><![CDATA[Hello again and welcome to the next post in our Caliburn Micro tutorial series. A relatively simple tutorial this time where we will be looking at the Window Manager. Before we begin, here are the links to the previous tutorials in this series: Part 1: Getting Started Part 2: Data Binding and Events Part 3: [...]]]></description>
				<content:encoded><![CDATA[<p>Hello again and welcome to the next post in our <a href="http://caliburnmicro.codeplex.com/">Caliburn Micro</a> tutorial series. A relatively simple tutorial this time where we will be looking at the Window Manager. Before we begin, here are the links to the previous tutorials in this series:</p>
<p>Part 1: <a href="http://www.mindscapehq.com/blog/index.php/2012/1/12/caliburn-micro-part-1-getting-started/">Getting Started</a><br />
Part 2: <a href="http://www.mindscapehq.com/blog/index.php/2012/1/16/caliburn-micro-part-2-data-binding-and-events/">Data Binding and Events</a><br />
Part 3: <a href="http://www.mindscapehq.com/blog/index.php/2012/1/24/caliburn-micro-part-3-more-about-events-and-parameters/">More About Events and Parameters</a><br />
Part 4: <a href="http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/">The Event Aggregator</a></p>
<p>Many of you who have played around with Caliburn Micro will know there is not much mentioned about the Window Manager. Due to this, I won&#8217;t be covering everything about the Window Manager, I&#8217;ll simply explain what I know about it. To demonstrate how to use the Window Manager, we will be extending the application we made in the <a href="http://www.mindscapehq.com/blog/index.php/2012/1/12/caliburn-micro-part-1-getting-started/">first blog post of this series</a>.</p>
<p><strong>Using a Window Manager</strong></p>
<p>You may remember from the getting started blog post that one of the first things we did was delete MainWindow.xaml from the project. Caliburn Micro takes care of initializing the window, setting its data context and displaying the appropriate view for us. The Window Manager is one of the mechanisms responsible for getting this done. When you run an application built with Caliburn Micro, the Window Manager is automatically used to create the startup window. For small applications, this is all you really need to know about the Window Manager. When you are building larger applications that need to display other windows or dialogs, it&#8217;s time to learn how to use the Window Manager. To demonstrate this we will add a button to the application which will open a new window when clicked. Start by adding a button to AppView.xaml and hooking the click event to a method in AppViewModel.cs. You can do this using Caliburn Micro conventions as explained in the previous tutorials. I have called this method &#8220;OpenWindow&#8221;. In the OpenWindow method we are going to need access to an instance of a Window Manager. Although we could simply create a new instance of WindowManager and use that, it is best practice to get a hold of the global Window Manager instance that Caliburn Micro makes available to the application. We can do this by making a constructor on AppViewModel.cs that takes in an IWindowManager and store it in a field. You may remember from part 4 of this blog series of what needs to be done when you create a constructor on a view model that has at least 1 parameter. Here is a recap in 3 easy step:</p>
<p>1. Update the bootstrapper as follows. Remember to add System.ComponentModel.Composition.dll as a reference in your project.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Caliburn.Micro</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel.Composition</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel.Composition.Hosting</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel.Composition.Primitives</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AppBootstrapper <span style="color: #008000;">:</span> Bootstrapper<span style="color: #008000;">&lt;</span>AppViewModel<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> CompositionContainer container<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Configure<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CompositionContainer<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AggregateCatalog<span style="color: #008000;">&#40;</span>AssemblySource<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> AssemblyCatalog<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>ComposablePartCatalog<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    CompositionBatch batch <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CompositionBatch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    batch<span style="color: #008000;">.</span><span style="color: #0000FF;">AddExportedValue</span><span style="color: #008000;">&lt;</span>IWindowManager<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> WindowManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    batch<span style="color: #008000;">.</span><span style="color: #0000FF;">AddExportedValue</span><span style="color: #008000;">&lt;</span>IEventAggregator<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> EventAggregator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    batch<span style="color: #008000;">.</span><span style="color: #0000FF;">AddExportedValue</span><span style="color: #008000;">&#40;</span>container<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    container<span style="color: #008000;">.</span><span style="color: #0000FF;">Compose</span><span style="color: #008000;">&#40;</span>batch<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;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetInstance<span style="color: #008000;">&#40;</span>Type serviceType, <span style="color: #6666cc; font-weight: bold;">string</span> key<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> contract <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> AttributedModelServices<span style="color: #008000;">.</span><span style="color: #0000FF;">GetContractName</span><span style="color: #008000;">&#40;</span>serviceType<span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> key<span style="color: #008000;">;</span>
    var exports <span style="color: #008000;">=</span> container<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExportedValues</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>contract<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>exports<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> exports<span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><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;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Could not locate any instances of contract {0}.&quot;</span>, contract<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>2. Use the Export attribute on the AppViewModel class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Export<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>AppViewModel<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AppViewModel <span style="color: #008000;">:</span> PropertyChangedBase
<span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>3. Use the ImportingConstructor attribute on the AppViewModel constructor:</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: #0600FF; font-weight: bold;">readonly</span> IWindowManager _windowManager<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>ImportingConstructor<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> AppViewModel<span style="color: #008000;">&#40;</span>IWindowManager windowManager<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  _windowManager <span style="color: #008000;">=</span> windowManager<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now we can use the Window Manager instance in our OpenWindow method to open a new window. To keep this tutorial simple, we will simply create a new window that also uses the AppViewModel as the data context. This is done using the following code:</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;">void</span> OpenWindow<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  _windowManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ShowWindow</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AppViewModel<span style="color: #008000;">&#40;</span>_windowManager<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Run this up and press the button to see another window appear. All we needed to do was pass in an instance of a view model, then everything else from creating the window instance and displaying the appropriate view for our view model is done for us. Once again Caliburn Micro makes our life easier!</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/03/WindowManager.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/03/WindowManager.png" alt="Using the Window Manager in Caliburn Micro" title="Using the Window Manager in Caliburn Micro" width="436" height="383" class="alignnone size-full wp-image-4682" /></a></p>
<p>The Window Manager has all sorts of methods and method overloads for opening windows, dialogs and popups. All these methods are fairly straight forward and easy to use. You can see more usage of the Window Manager in the HelloWindowManager sample that comes with Caliburn Micro. One more thing I wanted to look at is the &#8216;settings&#8217; parameter. Here you can pass in a dynamic object used to set properties on the new window. This gives you fine-grained control on the appearance of your application if you need it. Here is an example where I am setting the WindowStartupLocation to be manual rather than center-owner.</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;">void</span> OpenWindow<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  dynamic settings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ExpandoObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  settings<span style="color: #008000;">.</span><span style="color: #0000FF;">WindowStartupLocation</span> <span style="color: #008000;">=</span> WindowStartupLocation<span style="color: #008000;">.</span><span style="color: #0000FF;">Manual</span><span style="color: #008000;">;</span>
&nbsp;
  _windowManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ShowWindow</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AppViewModel<span style="color: #008000;">&#40;</span>_windowManager<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, settings<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Custom Window Managers</strong></p>
<p>There are scenarios where it is useful to implement a custom Window Manager. This is good if you need to set properties on all Window instances to be common values. For example, properties could include the icon, the window state, window size and applying custom chrome. The most useful property that I have found to set on windows is the SizeToContent property. By default, Caliburn Micro sets this to be SizeToContent.WidthAndHeight. This means that the window automatically sizes itself based on its content. Although this can be convenient at times, I have found this to cause some issues with certain application layouts and seems to be buggy when setting the window to be maximized by default. Creating a custom Window Manager is very simple. Start by adding a class that extends WindowManager. Next, you can override the EnsureWindow method and do something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> Window EnsureWindow<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> model, <span style="color: #6666cc; font-weight: bold;">object</span> view, <span style="color: #6666cc; font-weight: bold;">bool</span> isDialog<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Window window <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EnsureWindow</span><span style="color: #008000;">&#40;</span>model, view, isDialog<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  window<span style="color: #008000;">.</span><span style="color: #0000FF;">SizeToContent</span> <span style="color: #008000;">=</span> SizeToContent<span style="color: #008000;">.</span><span style="color: #0000FF;">Manual</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">return</span> window<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In this method, we start by calling base.EnsureWindow() to create the window instance. Next you can set any properties on the window that you want, and then simply return the window instance. The last step is to set an instance of your custom Window Manager to be globally used by the application. In the first code snippet in this blog post we make modifications to the bootstrapper. Here you can see we are adding a WindowManager instance to the CompositionBatch. You can replace this to use an instance of your custom window manager instead which would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">batch<span style="color: #008000;">.</span><span style="color: #0000FF;">AddExportedValue</span><span style="color: #008000;">&lt;</span>IWindowManager<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AppWindowManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>That&#8217;s everything I wanted to cover with the Window Manager, I hope you found it useful in building great WPF applications powered by Caliburn Micro! I appreciate your comments and feedback on the series!</p>
<p><a href='http://www.mindscapehq.com/blog/wp-content/uploads/2012/03/CaliburnMicroApp.zip'>Download the full Visual Studio 2010 project for this tutorial.</a></p>
<p>Happy coding :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2012/03/13/caliburn-micro-part-5-the-window-manager/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Caliburn Micro Part 4: The Event Aggregator</title>
		<link>http://www.mindscapehq.com/blog/index.php/2012/02/01/caliburn-micro-part-4-the-event-aggregator/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2012/02/01/caliburn-micro-part-4-the-event-aggregator/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 21:21:50 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=4453</guid>
		<description><![CDATA[For those of you who are new to this tutorial series, we have been learning about how to use Caliburn Micro to build WPF application with a robust MVVM architecture. Caliburn Micro is a framework used to help build .NET applications (WPF, Silverlight and Windows Phone 7) using several popular UI patterns including MVVM, MVP [...]]]></description>
				<content:encoded><![CDATA[<p>For those of you who are new to this tutorial series, we have been learning about how to use <a href="http://caliburnmicro.codeplex.com/">Caliburn Micro</a> to build WPF application with a robust MVVM architecture. Caliburn Micro is a framework used to help build .NET applications (WPF, Silverlight and Windows Phone 7) using several popular UI patterns including MVVM, MVP and MVC. It provides lots of neat ways to reduce the amount of work you need to do for common tasks such as setting up bindings and hooking up events. The various features of Caliburn Micro make it easy to have a clean line between the model objects and the UI. That means your application is easy to test and maintain. Here are links for the previous blog posts:</p>
<p><a href="https://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/">Part 1: Getting Started</a><br />
<a href="https://www.mindscapehq.com/blog/index.php/2012/01/16/caliburn-micro-part-2-data-binding-and-events/">Part 2: Data Binding and Events</a><br />
<a href="http://www.mindscapehq.com/blog/index.php/2012/01/24/caliburn-micro-part-3-more-about-events-and-parameters/#comments">Part 3: More About Events and Parameters</a></p>
<p>In this week&#8217;s tutorial we will be learning about how to use the event aggregator included with Caliburn Micro. The event aggregator is a service that makes it easy for multiple parts of your application to send messages to each other. This is useful when your application is made up of several view-models that need to communicate. To do this, you subscribe objects (such as view models) to the event aggregator and specify what type of message they should listen for. You also define what the object does when it receives such messages. Then, when another part of the application publishes a message, the event aggregator makes sure the appropriate subscribed objects receive it and perform the appropriate action. Throughout this tutorial we will be extending the application we made in the getting started tutorial. (<a href='http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/CaliburnMicroApp2.zip'>You can download the application here.</a>)</p>
<p>Although the event aggregator is more useful for larger applications that have multiple view-models, we will be keeping our tutorial application rather small. Also be warned that this tutorial has a lot more to digest compared to the previous ones! By the end of this tutorial, we will have an application that displays two views, each with their own view-model. One of the views will be displaying some radio buttons, each representing a different color. When a radio button is clicked, we&#8217;ll publish a message that includes the appropriate color. The second view will listen for these messages and change the color of a Rectangle. We will do this in 4 steps: Adding another view to the application, implementing the IHandle&lt;TMessage&gt; interface, subscribe one of the view models to an event aggregator and finally publish events from the other view model.</p>
<p><strong>Step 1: Adding Another View and View-Model</strong></p>
<p>In order to demonstrate the event aggregator, we will need at least 2 view models in our application. We already have one (AppViewModel), so lets start by adding another one. Remember the naming convention described in the getting started tutorial? Add a new class called ColorViewModel, and a UserControl called ColorView. Lets also change the background of the ColorView so that we at least have something to see when we first add it to the application. In terms of the visual structure, we are going to get the existing AppView to contain the new ColorView. (Views do not need to be nested in order to use the event aggregator; a view-model can listen to messages being published from anywhere in the application.) To do this, AppViewModel will need a property of type ColorViewModel which we will set in the constructor like this:</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> AppViewModel <span style="color: #008000;">:</span> PropertyChangedBase
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> AppViewModel<span style="color: #008000;">&#40;</span>ColorViewModel colorModel<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    ColorModel <span style="color: #008000;">=</span> colorModel<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ColorViewModel ColorModel <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In AppView.xaml, we will split the grid into 2 columns and display the ColorModelView in the first column. AppView.xaml will now look like this:</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> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&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>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentControl</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;ColorModel&quot;</span> <span style="color: #000066;">Margin</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;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>See what&#8217;s going to happen here? We have set the name of the ContentControl to be the same name as the property we just added to AppViewModel. From this, Caliburn Micro will kindly bind the Content property of the ContentControl to the ColorModel property for us. When we run this up later, Caliburn Micro will make sure that an instance of ColorView is displayed for the ColorViewModel.</p>
<p>If we were to run up the application now, we&#8217;d run into an exception saying that the default constructor of AppViewModel can&#8217;t be found. Hmm, that&#8217;s a good point: we have included a constructor on AppViewModel that requires a parameter &#8211; a ColorViewModel object. To resolve this, we&#8217;ll need to update our AppBootstrapper as seen below. (Make sure to include a reference to System.ComponentModel.Composition.dll to the application.)</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Caliburn.Micro</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel.Composition</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel.Composition.Hosting</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel.Composition.Primitives</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AppBootstrapper <span style="color: #008000;">:</span> Bootstrapper<span style="color: #008000;">&lt;</span>AppViewModel<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> CompositionContainer container<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Configure<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CompositionContainer<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AggregateCatalog<span style="color: #008000;">&#40;</span>AssemblySource<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> AssemblyCatalog<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>ComposablePartCatalog<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    CompositionBatch batch <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CompositionBatch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    batch<span style="color: #008000;">.</span><span style="color: #0000FF;">AddExportedValue</span><span style="color: #008000;">&lt;</span>IWindowManager<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> WindowManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    batch<span style="color: #008000;">.</span><span style="color: #0000FF;">AddExportedValue</span><span style="color: #008000;">&lt;</span>IEventAggregator<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> EventAggregator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    batch<span style="color: #008000;">.</span><span style="color: #0000FF;">AddExportedValue</span><span style="color: #008000;">&#40;</span>container<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    container<span style="color: #008000;">.</span><span style="color: #0000FF;">Compose</span><span style="color: #008000;">&#40;</span>batch<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;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetInstance<span style="color: #008000;">&#40;</span>Type serviceType, <span style="color: #6666cc; font-weight: bold;">string</span> key<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> contract <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> AttributedModelServices<span style="color: #008000;">.</span><span style="color: #0000FF;">GetContractName</span><span style="color: #008000;">&#40;</span>serviceType<span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> key<span style="color: #008000;">;</span>
    var exports <span style="color: #008000;">=</span> container<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExportedValues</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>contract<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>exports<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> exports<span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><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;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Could not locate any instances of contract {0}.&quot;</span>, contract<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This is similar to the bootstrappers used in the samples that come with the Caliburn Micro download. So as not to make this blog post too long, I won&#8217;t be diving into the details of what this code is doing (search for MEF or Managed Extensibility Framework if you need more details).</p>
<p>Next, we need to include an Export attribute on both our view-model classes. This is for the GetInstance method in the AppBootstrapper to work.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Export<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>AppViewModel<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AppViewModel <span style="color: #008000;">:</span> PropertyChangedBase
<span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>Export<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ColorViewModel<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ColorViewModel
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And finally, include the ImportingConstructor attribute on the AppViewModel constructor. This is to indicate that this constructor should be used since there is no default constructor. When Caliburn Micro creates the AppViewModel, it will also create an instance of the ColorViewModel to pass into the constructor for us.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>ImportingConstructor<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> AppViewModel<span style="color: #008000;">&#40;</span>ColorViewModel colorModel<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  ColorModel <span style="color: #008000;">=</span> colorModel<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now we can run the application and see that the ColorView is successfully being displayed within the AppView:</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/MultipleViewModels.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/MultipleViewModels.png" alt="The application now displays 2 view models" title="The application now displays 2 view models" width="349" height="373" class="alignnone size-full wp-image-4480" /></a></p>
<p>Let&#8217;s add a Rectangle into the second column. This will be the Rectangle that changes color when messages are handled, so its color will be controlled by a property on the AppViewModel like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> SolidColorBrush _Color<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> SolidColorBrush Color
<span style="color: #008000;">&#123;</span>
  get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _Color<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  set
  <span style="color: #008000;">&#123;</span>
    _Color <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
    NotifyOfPropertyChange<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Color<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#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;Rectangle</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;{Binding Color}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p><strong>Step 2: Implementing the IHandle&lt;TMessage&gt; Interface</strong></p>
<p>We are going to be publishing messages from the ColorViewModel to be picked up by the AppViewModel. To do this we are going to need to implement a class that holds the message information. such a class is usually very small and simple. It mainly needs to have some properties that hold any information that we want to send. Here is the message class that we are going to use:</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> ColorEvent
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ColorEvent<span style="color: #008000;">&#40;</span>SolidColorBrush color<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Color <span style="color: #008000;">=</span> color<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> SolidColorBrush Color <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In order for the AppViewModel to handle the appropriate events, it will need to implement the IHandle&lt;TMessage&gt; interface. In our case, we will be using ColorEvent as the generic type. The IHandle interface has a single method that we need to implement which is called Handle. In the Handle method of our AppViewModel, we will look at the SolidColorBrush sent in the ColorEvent and use it to set the Color property. This will in turn change the color of the Rectangle in the view.</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;">void</span> Handle<span style="color: #008000;">&#40;</span>ColorEvent message<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Color <span style="color: #008000;">=</span> message<span style="color: #008000;">.</span><span style="color: #0000FF;">Color</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Step 3: Subscribe</strong></p>
<p>Now we need to subscribe the AppViewModel to an event aggregator so that it can actually listen to published messages. We do this by adding another parameter to the constructor which will be an IEventAggregator. When the time comes to create the AppViewModel, Caliburn Micro will pass in the event aggregator that we set up in the bootstrapper. Now within the constructor, we simply call the Subscribe method like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>ImportingConstructor<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> AppViewModel<span style="color: #008000;">&#40;</span>ColorViewModel colorModel, IEventAggregator events<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  ColorModel <span style="color: #008000;">=</span> colorModel<span style="color: #008000;">;</span>
&nbsp;
  events<span style="color: #008000;">.</span><span style="color: #0000FF;">Subscribe</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Step 4: Publish</strong></p>
<p>The ColorViewModel is also going to need the event aggregator so that it can publish messages. Add a constructor to the ColorViewModel which takes an IEventAggregator and stores it in a field. Remember to include the ImportingConstructor attribute:</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: #0600FF; font-weight: bold;">readonly</span> IEventAggregator _events<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>ImportingConstructor<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ColorViewModel<span style="color: #008000;">&#40;</span>IEventAggregator events<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  _events <span style="color: #008000;">=</span> events<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now we just need add the radio buttons to the ColorView, listen to their click event and then publish a message. You may remember from the second tutorial in this series about the quick way to listen to the click event. Simply set the name of the RadioButton to be the same as the method we want to be called. We could of course use event parameters rather than having an action method for every RadioButton, but I have done it like this so you can clearly see what&#8217;s going on:</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;RadioButton</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Red&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Red&quot;</span> <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;White&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;RadioButton</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Green&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Green&quot;</span> <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;White&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: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RadioButton</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Blue&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Blue&quot;</span> <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;White&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: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>


<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;">void</span> Red<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  _events<span style="color: #008000;">.</span><span style="color: #0000FF;">Publish</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> ColorEvent<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Red</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><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: #6666cc; font-weight: bold;">void</span> Green<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  _events<span style="color: #008000;">.</span><span style="color: #0000FF;">Publish</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> ColorEvent<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Green</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><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: #6666cc; font-weight: bold;">void</span> Blue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  _events<span style="color: #008000;">.</span><span style="color: #0000FF;">Publish</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> ColorEvent<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Blue</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And that&#8217;s it. Run up the application now and click the radio buttons. See that the AppViewModel is successfully getting the messgaes from the ColorViewModel to change the color of the Rectangle.</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/EventAggregator.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/EventAggregator.png" alt="Using the event aggregator" title="Using the event aggregator" width="343" height="364" class="alignnone size-full wp-image-4481" /></a></p>
<p>One thing to note is that I have been passing around SolidColorBrushes in the messages to set the color of the Rectangle. In general you would pass around more primitive values and then use a converter in the UI to interpret the value as a SolidColorBrush or the like.</p>
<p><a href='http://www.mindscapehq.com/blog/wp-content/uploads/2012/02/CaliburnMicroApp_EventAggregator.zip'>The full Visual Studio 2010 project for this tutorial can be downloaded from here.</a> I hope you find the event aggregator useful for orchestrating communication across the various view-models in your application. <a href="http://caliburnmicro.codeplex.com/wikipage?title=The%20Event%20Aggregator&#038;referringTitle=Documentation">Check out the documention</a> for more information including how to do polymorphic event subscriptions, custom publication marshaling and unsubscribe from the event aggregator.</p>
<p><a href="http://www.mindscapehq.com/blog/index.php/2012/3/13/caliburn-micro-part-5-the-window-manager/">See you next time</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2012/02/01/caliburn-micro-part-4-the-event-aggregator/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Caliburn Micro Part 3: More About Events and Parameters</title>
		<link>http://www.mindscapehq.com/blog/index.php/2012/01/24/caliburn-micro-part-3-more-about-events-and-parameters/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2012/01/24/caliburn-micro-part-3-more-about-events-and-parameters/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 01:00:28 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=4375</guid>
		<description><![CDATA[So far in this Caliburn Micro tutorial series we have looked at how to get started with using Caliburn Micro in a WPF application, and how to link the view and the view-model together using data binding and events. In today&#8217;s tutorial, we will take a look at the more advanced ways of hooking up [...]]]></description>
				<content:encoded><![CDATA[<p>So far in this <a href="http://caliburnmicro.codeplex.com/">Caliburn Micro</a> tutorial series we have looked at <a href="https://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/">how to get started with using Caliburn Micro in a WPF application</a>, and <a href="https://www.mindscapehq.com/blog/index.php/2012/01/16/caliburn-micro-part-2-data-binding-and-events/">how to link the view and the view-model together using data binding and events</a>. In today&#8217;s tutorial, we will take a look at the more advanced ways of hooking up events and specifying event parameters. To experiment with these features, we will be further extending the application created in the previous two blog posts.</p>
<p>You may remember from the previous blog post that we hooked up events using the help of Caliburn Micro&#8217;s naming conventions. All we needed to do was set the name of a button in the view to be the same name as a method in the view-model. Then the Click event of the button will be automatically hooked up to call the appropriate method. Sometimes however you will need to explicitly hook up the events in order to provide event parameters or specify exactly which event you want to use. To do this, Caliburn Micro provides a long syntax and a short syntax. The advantage of the long syntax is its compatability with Microsoft Expression Blend. The short syntax is great if you&#8217;re not interested in using a designer and want to keep things short and sweet. Let&#8217;s start off with learning how to use the long syntax.</p>
<p><strong>Long Syntax</strong></p>
<p>The best way to begin is to modify the existing application to use the long syntax without changing the current behaviour. Open up AppView.xaml and include these 2 namespaces:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">xmlns:i=&quot;clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity&quot;
xmlns:cal=&quot;http://www.caliburnproject.org&quot;</pre></div></div>

<p>Then, simply replace the repeat button with this:</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;RepeatButton</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Up&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;15&quot;</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;i:Interaction.Triggers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;i:EventTrigger</span> <span style="color: #000066;">EventName</span>=<span style="color: #ff0000;">&quot;Click&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cal:ActionMessage</span> <span style="color: #000066;">MethodName</span>=<span style="color: #ff0000;">&quot;IncrementCount&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i:EventTrigger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i:Interaction.Triggers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/RepeatButton<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Run the application now and you will see it looks and behaves just as it did before. What we have done here is used System.Windows.Interactivity triggers to hook up an event to a method. In the EventTrigger we can specify which event we want to listen to, and using the Caliburn Micro ActionMessage we can specify which method should be called. Using this approach you can include any number of event triggers to listen to other events on the same control. So you could listen to MouseEnter and MouseLeave to perform additional actions.</p>
<p>Next let&#8217;s look at event parameters. To demonstrate this, we will add another button that increments the count by 2. In AppViewModel.cs we need to modify the IncrementCount method to include an integer parameter. This parameter will be used to change the Count property. The IncrementCount method will now look like this:</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;">void</span> IncrementCount<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> delta<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Count <span style="color: #008000;">+=</span> delta<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Back in AppView.xaml, update the existing repeat button by adding a Caliburn Micro Parameter to the ActionMessage like this:</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;cal:ActionMessage</span> <span style="color: #000066;">MethodName</span>=<span style="color: #ff0000;">&quot;IncrementCount&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cal:Parameter</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cal:ActionMessage<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now when the button is pressed, it will pass a value of 1 as a parameter to the IncrementCount method. Add another button to the application with a parameter of 2 and then run up the application. Clicking on the different buttons will increment the count value according to their different parameters. The Value property of the Caliburn Micro Parameter is a dependency property, which means it also supports the usual WPF data binding. This allows you to use it in all sorts of different scenarios you come across.</p>
<p>Pro Tip: Because of the flexibity of using data binding to set the parameter value, it is possible to pass UI elements from the view into the view-model. You should try to avoid doing this as hard as you possibly can! UI elements in the view-model can fracture your MVVM archetecture and can cause maintenance issues in the future.</p>
<p><strong>Short Syntax</strong></p>
<p>Now to learn how to use the short syntax to do the same thing. To look into this, let&#8217;s start off with a fresh copy of the application that doesn&#8217;t have our recent long syntax experiments in it. Again we will start by modifying the application without changing its current behaviour. This time we only need to add the Caliburn Micro namespace, and replace the repeat button with this new one:</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;RepeatButton</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Up&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;15&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">cal:Message.Attach</span>=<span style="color: #ff0000;">&quot;[Event Click] = [Action IncrementCount]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>All we have done this time is use one of Caliburn Micro&#8217;s attached properties (Message.Attach) to specify which event we are interested in, and which method to call.</p>
<p>Next we look at event parameters using this short syntax. Modify the IncrementCount method in same way we did for the long syntax. Including an event parameter using Message.Attach will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">cal:Message.Attach=&quot;[Event Click] = [Action IncrementCount(1)]&quot;</pre></div></div>

<p>The event parameter is written in the brackets after the method name. I&#8217;m sure you can work out how to add a button that increments the value by 2.</p>
<p>The short syntax even supports a special form of data binding. To demonstrate this, let&#8217;s add a button that increments the count by the count value itself. In other words, a button that doubles the count value. You can remove the CanIncrementCount event guard mentioned in the previous blog post to make the value go higher than 100. The repeat button code would look something like this:</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;RepeatButton</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Double&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;15&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">cal:Message.Attach</span>=<span style="color: #ff0000;">&quot;[Event Click] = [Action IncrementCount(Count.Text)]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Here I have set the parameter to be Count.Text. This sets up a binding to the Text property of the TextBlock (named Count) which is displaying the current value. Notice that Caliburn Micro automatically converts string values that we want to pass into a method that takes a numerical value. Another shortcut that Caliburn Micro provides here is that it will automatically use an appropriate property on a user control if we do not specify a property. In the example above, we could just write the name of the TextBlock (&#8220;Count&#8221;) as the parameter and Caliburn Micro will bind to the Text property by default. Here is the what the application looks like so far:</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Event-Parameters.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Event-Parameters.png" alt="Event Parameters" title="Event Parameters" width="358" height="379" class="alignnone size-full wp-image-4391" /></a></p>
<p><strong>Automatically Finding Parameters</strong></p>
<p>To finish off this tutorial I&#8217;ll mention that Caliburn Micro even has conventions for automatically picking up parameters when you don&#8217;t explicitly set them. If you do not explicitly specify a parameter, Caliburn Micro will look at the parameter name in the method signiture and try find any user control in the view that matches this name (ignoring the case). If a matching user control is found, an appropriate property on the control will be used to provide the parameter. For example, if the user control is a TextBlock, the Text property value will be used as the parameter. Again, Caliburn Micro can automatically convert strings to ints and so on if necessary.</p>
<p>To understand this convention more easily, lets try this out in the application. Add a slider to the application and call it &#8220;Delta&#8221;. Then add another button called &#8220;IncrementCount&#8221;. As explained in the previous blog post, the button is going to automatically call the IncrementCount method when it is clicked. This time however, the method has a parameter, but we havn&#8217;t specified what value the button should use. Notice that the slider we added is the same name as the parameter (delta). Thus Caliburn Micro will automatically use the Value property of the slider as the parameter to the method whenever the button is clicked. Here is all the code we needed to add to do this:</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;UniformGrid</span> <span style="color: #000066;">Columns</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Bottom&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Slider</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Delta&quot;</span> <span style="color: #000066;">Minimum</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">Maximum</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;15&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;IncrementCount&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Increment&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;15&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UniformGrid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Parameter-convention.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Parameter-convention.png" alt="Parameter Convention" title="Parameter Convention" width="355" height="377" class="alignnone size-full wp-image-4394" /></a></p>
<p><strong>Extra for Experts</strong></p>
<p>Here I&#8217;ve only covered some of the support that <a href="http://caliburnmicro.codeplex.com/">Caliburn Micro</a> provides for hooking up events. Some other topics of intrest include:</p>
<ul>
<li>Setting action targets</li>
<li>Special parameter values for data binding</li>
<li>Action bubbling</li>
<li>Action guards with parameters</li>
</ul>
<p>You can find more information about hooking up events using Caliburn Micro from the <a href="http://caliburnmicro.codeplex.com/wikipage?title=Cheat%20Sheet&#038;referringTitle=Documentation">online documentation</a>. I recommend reading through the documentation so you can better choose which approaches to use for the various event scenarios in your application. You&#8217;ll find there is a lot of helpful support that Caliburn Micro provides, and a lot of convenient conventions that will take work off your shoulders. The <a href="http://caliburnmicro.codeplex.com/wikipage?title=Cheat%20Sheet&#038;referringTitle=Documentation">Cheat Sheet</a> is a useful page to bookmark. You&#8217;ll also find that the HelloExplicitAction, HelloParameters and BubblingAction samples are a good source of knowledge.</p>
<p>You can download the application from this tutorial used to demonstrate the <a href='http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/LongSyntax.zip'>long syntax from here</a>, and the <a href='http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/ShortSyntax1.zip'>short syntax from here</a>.</p>
<p><a href="http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/">Next let&#8217;s take a look at the Event Aggregator</a>.</p>
<p>Until next time, Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2012/01/24/caliburn-micro-part-3-more-about-events-and-parameters/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Caliburn Micro Part 2: Data Binding and Events</title>
		<link>http://www.mindscapehq.com/blog/index.php/2012/01/16/caliburn-micro-part-2-data-binding-and-events/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2012/01/16/caliburn-micro-part-2-data-binding-and-events/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 01:27:05 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=4291</guid>
		<description><![CDATA[In my previous blog post I showed you how to get started with using the Caliburn Micro Framework in a WPF application. Caliburn Micro helps us implement the application using the MVVM design pattern to get a clean separation between the view and the data model. In this blog post we&#8217;ll take a look at [...]]]></description>
				<content:encoded><![CDATA[<p><a href="https://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/">In my previous blog post</a> I showed you how to get started with using the <a href="http://caliburnmicro.codeplex.com/">Caliburn Micro Framework</a> in a WPF application. Caliburn Micro helps us implement the application using the MVVM design pattern to get a clean separation between the view and the data model. In this blog post we&#8217;ll take a look at how Caliburn Micro assists us with data binding and events. We will be building on top of the application outlined in the previous blog post to add some simple user interaction and data display.</p>
<p><strong>Data binding</strong></p>
<p>We&#8217;ll start by getting the application to display a numerical value that is stored in the model. In the AppViewModel class created in the previous blog post, add a property called Count as seen in the code snippet below. The property value is stored in the _count field which we have given a default value of 50. You may recall from last time that we made the AppViewModel class extend the PropertyChangedBase provided by Caliburn Micro to preform property change notifications. Rather than implementing INotifyPropertyChanged in all of your models, you can simply call the NotifyOfPropertyChange method within the setter of your properties.</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> AppViewModel <span style="color: #008000;">:</span> PropertyChangedBase
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> _count <span style="color: #008000;">=</span> <span style="color: #FF0000;">50</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Count
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _count<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
      _count <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
      NotifyOfPropertyChange<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Count<span style="color: #008000;">&#41;</span><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>Next we modify the view to display this property value. In AppView.xaml, add a TextBox to the grid 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;Grid</span> <span style="color: #000066;">MinWidth</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">MinHeight</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&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;">Name</span>=<span style="color: #ff0000;">&quot;Count&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;150&quot;</span> <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></pre></div></div>

<p>Now run up the application and see that the TextBlock is displaying the default value of the Count property.</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/DataDisplay.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/DataDisplay.png" alt="A Caliburn Micro app displaying some data" title="A Caliburn Micro app displaying some data" width="346" height="369" class="alignnone size-full wp-image-4293" /></a></p>
<p>Wait&#8230; what? I don&#8217;t remember setting any binding on the TextBlock to get the Count property, and yet the TextBlock is displaying the correct data!</p>
<p>Notice that I&#8217;ve set the name of the TextBlock to be the same as the property we want to bind to. This is a convenient short cut that Caliburn Micro provides. For elements that display data such as TextBlock or TextBox, setting their name to match a property on the data model will automatically hook it up for you.</p>
<p><strong>Handling events</strong></p>
<p>Next, let&#8217;s work on adding a button that increments the displayed value. The quick and dirty way of doing this is to hook up the Click event of a button to an event handler in the code behind. However, when using the MVVM pattern it&#8217;s usually best (but not absolutely necessary) to avoid using the code behind where you can. So let&#8217;s look at how to handle the events Caliburn Micro style. First, add a method to the AppViewModel for incrementing the Count property like this:</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;">void</span> IncrementCount<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Count<span style="color: #008000;">++;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now add a button to the grid in AppView.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;Grid</span> <span style="color: #000066;">MinWidth</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">MinHeight</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RepeatButton</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;IncrementCount&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Up&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;15&quot;</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;TextBlock</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Count&quot;</span> <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;150&quot;</span> <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></pre></div></div>

<p>Running the application now and clicking the button will increment the count value as advertised. Once again you&#8217;ll notice that we don&#8217;t need to do much work to hook the click event of the button to the IncrementCount method. For certain user interface controls such as buttons, you can simply set the name of the control to be the name of the method you want it to be hooked to. Caliburn Micro will hook the appropriate event of the user control to the specified method in the model. In the case of buttons, Caliburn Micro deals with the Click event. (You can also manually specifying which event to hook up to which method which we&#8217;ll look at next time).</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Increment.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Increment.png" alt="Clicking the button increments the value" title="Clicking the button increments the value" width="346" height="369" class="alignnone size-full wp-image-4295" /></a></p>
<p><strong>Event guards</strong></p>
<p>When Caliburn Micro automatically hooks up the Click event to the IncrementCount method, it also looks for a method or property called CanIncrementCount. By adding a CanIncrementCount method or property, you can include additional logic that determines whether the event is allowed to be handled based on the current state of the model. Let&#8217;s do this now by adding the following property to the AppViewModel:</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;">bool</span> CanIncrementCount
<span style="color: #008000;">&#123;</span>
  get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> Count <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Since this logic is based on the value of the Count property, we also need to raise property change notification for the CanIncrementCount property whenever the Count value changes. This is done by adding this line of code on the Count property setter:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">NotifyOfPropertyChange<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> CanIncrementCount<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Run up the application once more and increment the value to 100. Once the limit has been reached, the button will become disabled and prevent the user from further incrementing the value.</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Limit.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Limit.png" alt="Button is disabled when count is 100" title="Button is disabled when count is 100" width="346" height="369" class="alignnone size-full wp-image-4294" /></a></p>
<p>In this tutorial we have looked at a few ways that Caliburn Micro takes some of the work off our shoulders. Now you can more quickly use data binding, property change notifications, event handlers and event guards, while at the same time implementing a sturdy MVVM application that is easier to test and maintain.</p>
<p><a href='http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/CaliburnMicroApp1.zip'>You can download the full Visual Studio 2010 solution for this tutorial here.</a> Use it to practice your new skills by adding a button to decrement the value.</p>
<p>In the <a href="http://www.mindscapehq.com/blog/index.php/2012/1/24/caliburn-micro-part-3-more-about-events-and-parameters/">next instalment of this blog series</a>, we&#8217;ll take a look at more advanced scenarios of event handling with Caliburn Micro, specifically with passing event parameters. See you then.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2012/01/16/caliburn-micro-part-2-data-binding-and-events/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Caliburn Micro Part 1: Getting Started</title>
		<link>http://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 02:09:34 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Data Grid]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=4270</guid>
		<description><![CDATA[A few days ago I blogged about our new Dashboard sample included in WPF Elements. The sample demonstrates how to create a sales dashboard using a few of our WPF controls like the Data Grid, Time Explorer and Charts. It was built with the help of the Caliburn Micro framework to give it a robust [...]]]></description>
				<content:encoded><![CDATA[<p>A few days ago <a href="https://www.mindscapehq.com/blog/index.php/2012/01/09/wpf-elements-5-dashboard-sample/">I blogged about our new Dashboard sample</a> included in <a href="https://www.mindscapehq.com/products/wpfelements">WPF Elements</a>. The sample demonstrates how to create a sales dashboard using a few of our WPF controls like the <a href="http://www.mindscapehq.com/products/wpfelements/controls/data-grid">Data Grid</a>, <a href="http://www.mindscapehq.com/products/wpfelements/controls/time-explorer">Time Explorer</a> and <a href="http://www.mindscapehq.com/products/wpfelements/wpf-charting">Charts</a>. It was built with the help of the <a href="http://caliburnmicro.codeplex.com/">Caliburn Micro framework</a> to give it a robust MVVM architecture. In this blog post I&#8217;ll walk you through a simple tutorial for getting started with using Caliburn Micro in a WPF application. This is just part 1 in a series I will be writing so I hope you&#8217;ll subscribe and follow along with me as we build up to a useful application.</p>
<p><strong>Step 1: Getting Started</strong></p>
<p>Caliburn Micro targets .NET framework 4.0, so you&#8217;ll need Visual Studio 2010 to create the application. Start by creating a new WPF Application and add a reference to Caliburn.Micro.dll and System.Windows.Interactivity.dll which come with the Caliburn Micro download. Since Caliburn Micro is going to take care of creating the window for you, delete MainWindow.xaml and remove the StartupUri attribute from App.xaml. App.xaml will now look like this:</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;Application</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;CaliburnMicroApp.App&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Application.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;/Application.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>Step 2: The View Model</strong></p>
<p>Caliburn Micro promotes a View-Model-First approach, so the next step is to add a class to represent the view model. Large applications can have many view models, each of which provides the logic for a different view. Below is a code example of an empty view model. Since this tutorial is focused on simply getting started with incorporating Caliburn Micro into a WPF application, we are not going to make the view model do anything for now.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Caliburn.Micro</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> CaliburnMicroApp
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AppViewModel <span style="color: #008000;">:</span> PropertyChangedBase
  <span style="color: #008000;">&#123;</span>
&nbsp;
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The first thing to notice here is the name of the class. Caliburn Micro expects a particular naming convention so it can hook up the view model to the appropriate view. The class name of a view model should end with &#8220;ViewModel&#8221;. What you put in front of &#8220;ViewModel&#8221; is up to you. The other thing to notice here is that this class extends the PropertyChangedBase. This is provided by Caliburn Micro and makes it easy to raise property change notifications without needing to implement INotifyPropertyChanged in all your view models. Although this example view model doesn&#8217;t do anything, I&#8217;ve included the PropertyChangedBase as good practice. When adding properties to the view model, it will come in handy.</p>
<p><strong>Step 3: The View</strong></p>
<p>In order to display something in the window, you&#8217;ll need to create a view for the view model created in the previous step. This is as simple as adding a new UserControl to the project as seen below. I should point out again that Caliburn Micro expects a particular naming convention so it can hook up the view to the appropriate view model. View names should end with &#8220;View&#8221; and start with the same name you used for the view model. So for my example here, &#8220;AppView&#8221; is the view for rendering the &#8220;AppViewModel&#8221;. In the code below I have also set the width, height and background of the grid so that when you run this application you can see that it is working correctly.</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;UserControl</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;CaliburnMicroApp.AppView&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns:mc</span>=<span style="color: #ff0000;">&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&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;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&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;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>Step 4: The Bootstrapper</strong></p>
<p>The bootstrapper is the mechanism used to incorporate Caliburn Micro into your application. It is also a place where you can configure the framework for the needs of your application. For the purposes of this tutorial, I have used a very simple bootstrapper implementation seen here:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Caliburn.Micro</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> CaliburnMicroApp
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AppBootstrapper <span style="color: #008000;">:</span> Bootstrapper<span style="color: #008000;">&lt;</span>AppViewModel<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Caliburn Micro has 2 different bootstrappers available. The one used above lets you set the generic type to be the view model that you want to be used at startup. The last step is to tell the application to use the bootstrapper. This is done by adding your bootstrapper to a resource dictionary in App.xaml. After doing this, App.xaml will now look something like this:</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;Application</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;CaliburnMicroApp.App&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns:local</span>=<span style="color: #ff0000;">&quot;clr-namespace:CaliburnMicroApp&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Application.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ResourceDictionary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ResourceDictionary.MergedDictionaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ResourceDictionary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:AppBootstrapper</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;bootstrapper&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ResourceDictionary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ResourceDictionary.MergedDictionaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ResourceDictionary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Application.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>And that&#8217;s it</strong></p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/CaliburnMicroApp.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/CaliburnMicroApp.png" alt="Stay tuned for Part 2 to see something with a little more substance" title="Stay tuned for Part 2 to see something with a little more substance" width="349" height="369" class="alignnone size-full wp-image-4282" /></a></p>
<p>Now when you run up the application, you&#8217;ll see a small window with a light blue background. Your application is now primed and ready to take advantage of all the great support provided by <a href="http://caliburnmicro.codeplex.com/">Caliburn Micro</a>. It&#8217;s easy to get help for using this framework due to its growing community. You can also <a href="http://caliburnmicro.codeplex.com/documentation">look through the documentation on CodePlex.</a></p>
<p><a href='http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/CaliburnMicroApp.zip'>You can grab the full Visual Studio 2010 solution I made for this tutorial here.</a></p>
<p>In the <a href="http://www.mindscapehq.com/blog/index.php/2012/1/16/caliburn-micro-part-2-data-binding-and-events/">next part of this blog series</a> we will look at adding some interactivity using events and data binding with the help of Caliburn Micro.</p>
<p>Until next time, Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>WPF Elements 5 Dashboard Sample</title>
		<link>http://www.mindscapehq.com/blog/index.php/2012/01/09/wpf-elements-5-dashboard-sample/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2012/01/09/wpf-elements-5-dashboard-sample/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 00:13:14 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[MegaPack]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF Elements]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=4243</guid>
		<description><![CDATA[Not long ago we released version 5 of WPF Elements, a suite of fantastic WPF controls (if I do say so myself, as the lead developer&#8230;). This version includes a high performance data grid control, a time explorer for selecting time ranges in a convenient manner and hundreds of enhancements across the board. In the [...]]]></description>
				<content:encoded><![CDATA[<p>Not long ago we released version 5 of <a href="https://www.mindscapehq.com/products/wpfelements">WPF Elements</a>, a suite of fantastic WPF controls (if I do say so myself, as the lead developer&#8230;). This version includes a <a href="http://www.mindscapehq.com/products/wpfelements/controls/data-grid">high performance data grid control</a>, a <a href="http://www.mindscapehq.com/products/wpfelements/controls/time-explorer">time explorer</a> for selecting time ranges in a convenient manner and hundreds of enhancements across the board. In the latest <a href="http://www.mindscapehq.com/products/wpfelements/nightly-builds">nightly build</a> you will find a new sample app that demonstrates how to use various controls from WPF elements to build a sales dashboard. This app is built using the <a href="http://caliburnmicro.codeplex.com/">Caliburn Micro framework</a> and is implemented using the popular MVVM design pattern.</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Pipper-Catering-Dashboard.gif"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/Pipper-Catering.gif" alt="Pipper Catering Dashboard" title="Pipper Catering Dashboard" width="718" height="440" class="alignnone size-full wp-image-4252" /></a></p>
<p><strong>User interaction</strong></p>
<p>First lets have a look at what this dashboard provides in the user interface. Along the top of the dashboard is the <a href="https://www.mindscapehq.com/products/wpfelements/controls/time-explorer">time explorer</a> control. This allows the user to select a time range by dragging or clicking the mouse. By zooming in with the mouse wheel, the time explorer will display shorter time intervals such as weeks and days. Within the time explorer is a line chart displaying some summary data for the overall dashboard. This makes it easy for the user to select a time range around any interesting points in the data. Every time the user selects a new time range, the data displayed in the charts, gauges and data grid are updated to only include the data in the selected time range.</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/UserInteraction.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/UserInteraction.png" alt="Time explorer interaction" title="Time explorer interaction" width="718" height="458" class="alignnone size-full wp-image-4248" /></a></p>
<p><strong>Data drill down</strong></p>
<p>The <a href="https://www.mindscapehq.com/products/wpfelements/controls/wpf-stacked-bar-chart">stacked bar chart</a> below the time explorer is plotting a category breakdown of the data for each point displayed on the X axis. An individual bar shows the data for either a month, day, or 2 hour time slot based on the magnitude of the selected time range. Clicking on one of the bars will drill down to the next time interval.</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/DataDrillDown.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/DataDrillDown.png" alt="Data drill down" title="Data drill down" width="718" height="284" class="alignnone size-full wp-image-4249" /></a></p>
<p><strong>Category filtering</strong></p>
<p>Selecting one of the segments in the doughnut chart will filter the data grid by the selected category, and emphasise the appropriate points in the stacked bar chart and the equivalent gauge.</p>
<p><a href="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/CategoryFiltering1.png"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2012/01/CategoryFiltering1.png" alt="Category filtering" title="Category filtering" width="718" height="628" class="alignnone size-full wp-image-4250" /></a></p>
<p>Along the bottom of the dashboard is a text block displaying the total sales value, animated linear gauges, and our <a href="http://www.mindscapehq.com/products/wpfelements/controls/data-grid">WPF data grid</a>. Users can reorder, resize and sort the columns in the data grid to display the current data however they would prefer.</p>
<p><strong>MVVM architecture</strong></p>
<p>The MVVM design pattern is commonly used to create robust WPF applications that are easy to maintain. The main idea behind this pattern is to cleanly separate the data model from the user interface. The only things linking these two parts together should be bindings, commands and event handlers. The data model of the dashboard application listens to changes made to the various controls based on the user interaction. These changes are analysed and the displayed data held within the model is updated to reflect the selected time ranges or filters. The controls within the user interface listen to the changes made to the data model and then updates the visuals for the user to see.</p>
<p>You can <a href="http://assets.mindscape.co.nz/Trial/Nightlies/20120109/WpfElements5Trial-20120109.msi">download the trial version of the latest nightly build from here</a>, or go to your <a href="http://www.mindscapehq.com/store/myaccount">account page</a> if you are already a customer. The smaller data model classes can be found in the Model folder. The main data model class is called DashboardViewModel, and the UI is built up in the DashboardView.xaml file. The SimpleLinearGauge extends our AnimatedValueDisplay, and the remaining class files are for hooking into the Caliburn Micro framework.</p>
<p>Have any questions about WPF Elements? <a href="http://www.mindscapehq.com/forums/forum/15">We&#8217;d love to hear from you in the forum!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2012/01/09/wpf-elements-5-dashboard-sample/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Working with the Data Grid &#8211; Day 2</title>
		<link>http://www.mindscapehq.com/blog/index.php/2011/12/14/working-with-the-data-grid-day-2/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2011/12/14/working-with-the-data-grid-day-2/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 00:11:54 +0000</pubDate>
		<dc:creator>CallumG</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF Elements]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=4149</guid>
		<description><![CDATA[Following on from the first post about the new WPF Data Grid control in WPF Elements 5, today I&#8217;ll be writing about some of the options for customizing it. If you haven&#8217;t already checked out the new version with it and over 50 other controls head over and grab the 60 day trial here. Getting [...]]]></description>
				<content:encoded><![CDATA[<p>Following on from the first post about the new <a href="http://www.mindscapehq.com/products/wpfelements/controls/data-grid">WPF Data Grid</a> control in <a href="http://www.mindscapehq.com/products/wpfelements">WPF Elements 5</a>, today I&#8217;ll be writing about some of the options for customizing it. If you haven&#8217;t already checked out the new version with it and over 50 other controls head over and <a href="http://www.mindscapehq.com/products/wpfelements/download">grab the 60 day trial here</a>. Getting the control up and running was pretty easy, and setting the behavior to your specifications is just as simple. So what properties can we set? The follow is a list of some of the important ones, many more tweaks can be made through the API. Adjust the following to taste:</p>
<h3>Rows &#038; Columns</h3>
<ul>
<li>ShowColumnHeaders- will hide the row of column labels if False. Defaults to True</li>
<li>ShowRowHeaders- as above</li>
<li>AllowColumnReorder &#8211; allows the user to change the order of columns by dragging and dropping</li>
<li>RowHeaderWidth &#8211; sets the width of the row header block</li>
<li>AutoGenerateColumns &#8211; will create the columns based on the properties in the bound collection</li>
</ul>
<h3>Frozen Columns</h3>
<ul>
<li>FrozenColumnCount &#8211; the specified number of columns will remain stationary when the Data Grid is scrolled horizontally</li>
<li>ShowFirstFrozenLine &#8211; if True if frozen column shadow line will be displayed on the left when FrozenColumnCount is set to 0</li>
</ul>
<h3>Selection &#038; Editing</h3>
<ul>
<li>HighlightedCell &#8211; gets or sets the cell that has focus (black border)</li>
<li>SelectedCell &#038; SelectedCells &#8211; gets or sets the cell, or collection of cells that have been selected (yellow highlight). Both highlighted and selected cells can be set programmatically</li>
<li>SelectionType &#8211; sets a user&#8217;s click to highlight either a Row or Cell</li>
<li>SelectionMode &#8211; Single allows one cell/row to be selected at a time; Multiple adds a single cell/row to the current selection with a click; Extended allows a block of cells/rows to be highlighted through click and drag functionality</li>
<li>AllowEditing &#8211; if set to True the user will be able to double-click a cell to edit its contents (if False the Data Grid is read-only). Once in editing mode the user can tab across the row</li>
<li>SetAllowEditing() &#8211; when called on a DataGrid, can set either a cell or a row to be read-only. Can be passed a row or a DataGridCell</li>
</ul>
<h3>Paging</h3>
<ul>
<li>IsPagerVisible &#8211; shows the pager control if is True (defaults to False)</li>
<li>PageSize &#8211; sets number of items on a page. Default is 0 (all items on one page)</li>
<li>PageIndex &#8211; sets the current page to be displayed</li>
<li>PagerStyle &#8211; allows you to set a custom style for the pager control</li>
<li>MaxPagerButtonCount &#8211; sets the maximum amount of page buttons to display (including the ellipsis)</li>
<li>EllipsisMode &#8211; place an ellipsis button (indicating further pages) at positions After, Before, None or Both</li>
</ul>
<p>For example the following code:</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;ms:DataGrid</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;DataGrid&quot;</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Data}&quot;</span> <span style="color: #000066;">RowHeaderWidth</span>=<span style="color: #ff0000;">&quot;15&quot;</span> <span style="color: #000066;">SelectionMode</span>=<span style="color: #ff0000;">&quot;Extended&quot;</span></span>
<span style="color: #009900;">                 <span style="color: #000066;">SelectionType</span>=<span style="color: #ff0000;">&quot;Cell&quot;</span> <span style="color: #000066;">IsPagerVisible</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000066;">PageSize</span>=<span style="color: #ff0000;">&quot;16&quot;</span> <span style="color: #000066;">EllipsisMode</span>=<span style="color: #ff0000;">&quot;Both&quot;</span> <span style="color: #000066;">MaxPagerButtonCount</span>=<span style="color: #ff0000;">&quot;5&quot;</span> </span>
<span style="color: #009900;">                 <span style="color: #000066;">AllowColumnReorder</span>=<span style="color: #ff0000;">&quot;False&quot;</span> <span style="color: #000066;">FrozenColumnCount</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>produces this result:</p>
<p><a href="http://www.mindscapehq.com/blog/index.php/2011/12/14/working-with-the-data-grid-day-2/datagrid/" rel="attachment wp-att-4163"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2011/12/DataGrid.gif" alt="" title="DataGrid" width="601" height="452" class="alignnone size-full wp-image-4163" /></a></p>
<p>There is also a whole set of properties for the columns themselves. Naturally you can set Widths &#038; MinWidths, alongside setting the Header (which can be any object, just like a cell). Other properties include AllowSort, AllowResize, AllowEditing, SortDirection and most importantly the DisplayTemplate and EditorTemplate properties, for completely customizing the look and feel of a particular column.</p>
<p>Next time we&#8217;ll look at styling the DataGrid, then adding in a custom editor. <a href="http://www.mindscapehq.com/products/wpfelements/download">Download the 60 day trial of WPF Elements now to explore this control plus dozens more</a>. Until next time!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2011/12/14/working-with-the-data-grid-day-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FREE: 5 Professional WPF Themes</title>
		<link>http://www.mindscapehq.com/blog/index.php/2011/12/13/free-5-professional-wpf-themes/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2011/12/13/free-5-professional-wpf-themes/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 03:04:59 +0000</pubDate>
		<dc:creator>John-Daniel Trask</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Products]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF Elements]]></category>
		<category><![CDATA[Expression Theme WPF]]></category>
		<category><![CDATA[WPF Themes]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=3998</guid>
		<description><![CDATA[Everyone loves free things! With the recent release of WPF Elements 5 we decided to beef up the free WPF control pack even more. Now in addition to the already free WPF controls you will find 5 professionally built themes to bring your applications to life. 5 superbly crafted themes We have poured months of [...]]]></description>
				<content:encoded><![CDATA[<p>Everyone loves free things! With the <a href="http://www.mindscapehq.com/blog/index.php/2011/12/07/wpf-elements-5-released/">recent release of WPF Elements 5</a> we decided to beef up the <a href="http://www.mindscapehq.com/products/wpfelements/free-controls">free WPF control pack</a> even more. Now in addition to the already <a href="http://www.mindscapehq.com/products/wpfelements/free-controls">free WPF controls</a> you will find 5 professionally built themes to bring your applications to life.</p>
<p><strong>5 superbly crafted themes</strong><br />
We have poured months of effort into creating <a href="http://www.mindscapehq.com/products/wpfelements/themes">pixel perfect themes both for the standard WPF controls and the WPF Elements controls</a>. There&#8217;s Office Blue, Office Black, Office Silver, Expression (Alloy) and Expression Light (Alloy Light). You&#8217;ll be impressed by how easy these themes are to add to your project and your users will be really impressed at how great the application looks.</p>
<p><img src="http://www.mindscapehq.com/upload/wpfelements/screenshots/Version5Screens/ThemeMultipleWeb.png" alt="Three of five themes" /></p>
<p><strong>Autocomplete Text Box for WPF</strong></p>
<p>Autocomplete text boxes are great. We&#8217;ve made some great additions to this control recently and it will really help bring your applications up a level. Check out the new multi-value autocomplete box support that was included recently.</p>
<p><img src="http://www.mindscapehq.com/upload/wpfelements/screenshots/AutoCompleteBoxDelimiters.png" alt="Autocomplete text box" /></p>
<p><strong>Coverflow for WPF</strong></p>
<p>Popularised in iTunes, Coverflow is a unique way of presenting data to your users. Beyond just static images and thanks to the power of WPF you will find you can set any arbitrary content as your covers &#8212; images, videos, other WPF controls, whatever you want! We&#8217;ve seen some imaginative uses of the Coverflow control over the years. You will be pleased to find everything you need in the box to configure the display exactly as you want it to be. </p>
<p><img src="http://www.mindscapehq.com/upload/wpfelements/screenshots/wpfcoverflow.png" alt="Coverflow" /></p>
<p><strong>Prompt Decorator for WPF</strong></p>
<p>This control adds a polish to your applications. Want to have hint text on your text box and drop down lists? Say &#8220;Search&#8221; appearing in your search box but disappearing when the user clicks in it? It couldn&#8217;t be easier with the Prompt Decorator control.</p>
<p><img src="http://www.mindscapehq.com/upload/wpfelements/screenshots/Version5Screens/PromptDecorator.png" alt="Prompt Decorator" /></p>
<p>Get the <a href="http://www.mindscapehq.com/products/wpfelements/free-controls">free controls with the WPF Elements 5 installer.</a> You&#8217;re welcome to use the controls and themes listed here at no charge, with no royalties. </p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2011/12/13/free-5-professional-wpf-themes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Working with the Data Grid &#8211; Day 1</title>
		<link>http://www.mindscapehq.com/blog/index.php/2011/12/12/working-with-the-data-grid-day-1/</link>
		<comments>http://www.mindscapehq.com/blog/index.php/2011/12/12/working-with-the-data-grid-day-1/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 03:33:18 +0000</pubDate>
		<dc:creator>CallumG</dc:creator>
				<category><![CDATA[Products]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF Elements]]></category>
		<category><![CDATA[DataGrid]]></category>

		<guid isPermaLink="false">http://www.mindscapehq.com/blog/?p=4052</guid>
		<description><![CDATA[As we&#8217;ve previously mentioned, WPF Elements 5 comes with a powerful new control for displaying data in a tabular format, the Data Grid. While I could write about its super fast performance under heavy loads, or its many options, the first thing to mention is how to get a Data Grid up on the screen. [...]]]></description>
				<content:encoded><![CDATA[<p>As we&#8217;ve previously mentioned, <a href="http://www.mindscapehq.com/products/wpfelements">WPF Elements 5</a> comes with a powerful new control for displaying data in a tabular format, the <a href="http://www.mindscapehq.com/products/wpfelements/controls/data-grid">Data Grid</a>. While I could write about its super fast performance under heavy loads, or its many options, the first thing to mention is how to get a Data Grid up on the screen. Fortunately that&#8217;s a simple matter, and in this introductory post I&#8217;ll be showing you how to do just that. From there the Data Grid is easily sculpted to fit the needs of your application, and over this series I&#8217;ll continue the process by showing you how to build a fully functional data grid customized to specifications.</p>
<p>You might like to <a href="http://www.mindscapehq.com/products/wpfelements/download">check out the demos of the Data Grid in the Sample Explorer</a> that comes bundled with the <a href="http://www.mindscapehq.com/products/wpfelements/download">60 day trial of WPF Elements</a>. Alongside being able to see the Data Grid in action XAML and code samples are included, ready to inspire your own applications.</p>
<p>But for now we shall build one from scratch! Where to begin?</p>
<p><strong>Setting up the references</strong></p>
<p>The easiest way is to add a reference to WpfElements.dll and then drag a Data Grid control from the toolbox onto your Window or Page. This will add the licenses.licx file to your properties folder and also add the following namespace to your XAML:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">xmlns:ms=&quot;http://namespaces.mindscape.co.nz/wpf&quot;</pre></div></div>

<p>Our Data Grid is going to be fairly uninteresting without binding it to a model. To save time I&#8217;ve made the following simple example to populate the grid; you can <a href='http://www.mindscapehq.com/blog/index.php/2011/12/12/working-with-the-data-grid-day-1/datagrid1/' rel='attachment wp-att-4088'>download the source code here</a> and put it in the code-behind (the archive also includes the finished XAML).</p>
<p>We&#8217;re ready to go! Here&#8217;s the code:</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;ms:DataGrid</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Data}&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></pre></div></div>

<p>The Data Grid was designed following WPF best practices therefore setting its model is like any other WPF control &#8211; just bind the ItemsSource property to a collection of data. Run it, edit cell data, rearrange column order by dragging and dropping, click on a column header to sort &#8211; all behave as expected. All with one line of XAML!</p>
<p><a href="http://www.mindscapehq.com/blog/index.php/2011/12/12/working-with-the-data-grid-day-1/datagrid1-1/" rel="attachment wp-att-4100"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2011/12/datagrid1-1.png" alt="" title="DataGrid" width="600" height="450" class="alignnone size-full wp-image-4100" /></a></p>
<p>You&#8217;ll notice that some of the column widths are a little small for many of the records. Also, while the Generic theme is perfectly serviceable, we&#8217;ve got five other theme options so we could apply one of those too. Column widths are set inside the DataGrid control 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;ms:DataGrid</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Data}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ms: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;ms:DataGridColumn</span> <span style="color: #000066;">PropertyName</span>=<span style="color: #ff0000;">&quot;Name&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;120&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ms:DataGridColumn</span> <span style="color: #000066;">PropertyName</span>=<span style="color: #ff0000;">&quot;Address&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;140&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ms:DataGridColumn</span> <span style="color: #000066;">PropertyName</span>=<span style="color: #ff0000;">&quot;Balance&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;60&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ms: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;/ms:DataGrid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>As for the theme, I&#8217;ll be setting the entire window theme, but if you just want to set the Data Grid control that&#8217;s available by setting the ResourceDictionary inside ms:DataGrid.Resources.</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.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ResourceDictionary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ResourceDictionary.MergedDictionaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ms:OfficeBlue</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ResourceDictionary.MergedDictionaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ResourceDictionary<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></pre></div></div>

<p>Feel free to check out how OfficeSilver, OfficeBlack, Alloy and AlloyLight look too. The finished result:</p>
<p><a href="http://www.mindscapehq.com/blog/index.php/2011/12/12/working-with-the-data-grid-day-1/datagrid1-21/" rel="attachment wp-att-4103"><img src="http://www.mindscapehq.com/blog/wp-content/uploads/2011/12/datagrid1-21.png" alt="Data Grid with Office Blue" title="DataGrid" width="600" height="450" class="alignnone size-full wp-image-4103" /></a></p>
<p>As you can see it&#8217;s fairly easy to get the Data Grid up and running! In future posts we&#8217;ll look at some of the properties we can set on the Data Grid, to control sorting, column reordering, selection modes and of course the editors, both built-in and custom. Finally we&#8217;ll apply a custom colored template to the Balance column!</p>
<p>Head on over and grab the 60 day trial of WPF Elements to try out the Data Grid and over 50 other controls here:<br />
<a href="http://www.mindscapehq.com/products/wpfelements/download">Download WPF Elements</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/blog/index.php/2011/12/12/working-with-the-data-grid-day-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: basic (Requested URI is rejected)
Database Caching 5/24 queries in 0.011 seconds using disk: basic

 Served from: www.mindscapehq.com @ 2013-06-19 23:39:33 by W3 Total Cache -->