<?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>Alkampfer&#039;s Place&#187; WPF</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/category/net-framework/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet By Ricci Gian Maria</description>
	<lastBuildDate>Wed, 08 Feb 2012 17:49:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Design time data in WPF and Blend with little effort</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/01/20/design-time-data-in-wpf-and-blend-with-little-effort/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/01/20/design-time-data-in-wpf-and-blend-with-little-effort/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 18:57:30 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[Blend]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/01/20/design-time-data-in-wpf-and-blend-with-little-effort/</guid>
		<description><![CDATA[The power of WPF binding really shines when you use design time data to have a live preview of the aspect of your UI without the need to press F5 to load actual data. Design time data is a cool feature you can have with little effort, suppose you have a simple windows and you [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F20%2Fdesign-time-data-in-wpf-and-blend-with-little-effort%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F20%2Fdesign-time-data-in-wpf-and-blend-with-little-effort%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Blend,WPF&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The power of WPF binding really shines when you use design time data to have a live preview of the aspect of your UI without the need to press F5 to load actual data. Design time data is a cool feature you can have with little effort, suppose you have a simple windows and you want to show a list of customers, taken from the northwind datadabase inside a Listbox, personlizing the DataTemplate.</p>
<p>I start with&#160; simple ViewModelBase, that is really far to be a real Base View Model to implement a full <a title="MVVM" href="http://www.codewrecks.com/blog/index.php/tag/mvvm/">MVVM</a> solution, but it serves me to show how to use design time data without scare people telling them to implement complex <a title="patterns" href="http://www.codewrecks.com/blog/index.php/tag/patterns/">patterns</a> or similar stuff.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">abstract</span> <span style="color: rgb(0, 0, 255);">class</span> ViewModelBase : MarkupExtension, INotifyPropertyChanged</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">event</span> PropertyChangedEventHandler PropertyChanged;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>     <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">virtual</span> <span style="color: rgb(0, 0, 255);">void</span> OnPropertyChanged(String propertyName)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>         var temp = PropertyChanged;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>         <span style="color: rgb(0, 0, 255);">if</span> (temp != <span style="color: rgb(0, 0, 255);">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span>             temp(<span style="color: rgb(0, 0, 255);">this</span>, <span style="color: rgb(0, 0, 255);">new</span> PropertyChangedEventArgs(propertyName));</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum13">  13:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum14">  14:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum15">  15:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum16">  16:</span>     <span style="color: rgb(0, 0, 255);">private</span> DependencyObject syncRoot = <span style="color: rgb(0, 0, 255);">new</span> DependencyObject();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum17">  17:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum18">  18:</span>     <span style="color: rgb(0, 0, 255);">protected</span> Boolean IsInDesignMode</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum19">  19:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum20">  20:</span>         get { <span style="color: rgb(0, 0, 255);">return</span> DesignerProperties.GetIsInDesignMode(syncRoot); }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum21">  21:</span>     }</pre>
<p><!--CRLF--></div>
</div>
<p>This is really simple base class that implements INotifyPropertyChanged and inherits from MarkupExtension, it has also an IsInDesign mode property to detect if the code is running inside a designer.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">abstract</span> <span style="color: rgb(0, 0, 255);">void</span> CommonInit();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">abstract</span> <span style="color: rgb(0, 0, 255);">void</span> RuntimeInit();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span> <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">abstract</span> <span style="color: rgb(0, 0, 255);">void</span> DesignTimeInit();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span> <span style="color: rgb(0, 0, 255);">protected</span> ViewModelBase()</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>     <span style="color: rgb(0, 0, 255);">this</span>.CommonInit();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>     <span style="color: rgb(0, 0, 255);">if</span> (!IsInDesignMode)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span>         <span style="color: rgb(0, 0, 255);">this</span>.RuntimeInit();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum13">  13:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum14">  14:</span> }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum15">  15:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum16">  16:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">object</span> ProvideValue(IServiceProvider serviceProvider)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum17">  17:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum18">  18:</span>     <span style="color: rgb(0, 0, 255);">this</span>.DesignTimeInit();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum19">  19:</span>     <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 255);">this</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum20">  20:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Then I defined three abstract methods, the first is called CommonInit() and is used to do common initialization, then the RuntimeInit() is called to do all initialization I do not want to do when the viewmodel is used inside the designer, finally the DesignTimeInit() is called when the object is constructed inside the Designer. The trick is that the constructor call the RuntimeInit() only when we are outside the designer and the DesignTimeInit() is simply called inside the ProvideValue() virtual method of the base MarkupExtension class</p>
<p>Then I create a MainWindowViewModel inheriting from this class, add an ObservableCollection&lt;Customers&gt; to show data and implemented the initialization methods, the RuntimeInit() loads data from database.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">void</span> RuntimeInit()</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>  {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>      <span style="color: rgb(0, 0, 255);">using</span> (NorthWind context = <span style="color: rgb(0, 0, 255);">new</span> NorthWind())</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>          <span style="color: rgb(0, 0, 255);">foreach</span> (var customer <span style="color: rgb(0, 0, 255);">in</span> context.Customers)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>          {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>              LoadedCustomers.Add(customer);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>          }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>      }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>  }</pre>
<p><!--CRLF--></div>
</div>
<p>Then in the DesignTimeInit() I create some fake objects that will be used by the designer, you can do it simple using Fizzware NBuilder library.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">void</span> DesignTimeInit()</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>     <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">int</span> i = 0; i &lt; 10; i++)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>         Customers customerDummy = FizzWare.NBuilder.Builder&lt;Customers&gt;.CreateNew().Build();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>         LoadedCustomers.Add(customerDummy);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Now I fire <a title="blend" href="http://www.codewrecks.com/blog/index.php/tag/blend/">blend</a> and open the solution, create a windows then add the design time data context to the first Grid control</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Grid</span> <span style="color: rgb(255, 0, 0);">d:DataContext</span><span style="color: rgb(0, 0, 255);">=&quot;{sampleproject:MainWindowViewModel}&quot;</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>This will call the ProvideValue of the MarkupExtension class, so the object will be constructed with some dummy design data, then I drop a ListBox inside the window and bind its ItemsSource property with designer</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image3.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image_thumb3.png" width="778" height="551" /></a></p>
<p><strong>Figure 1: </strong><em>Bind with designer</em></p>
<p>The cool part is that the designer correctly recognize the ViewModel inside the DataContext and shows me the list of properties that can be bound to the ItemsControl property. Now I right click the ListBox and ask <a title="blend" href="http://www.codewrecks.com/blog/index.php/tag/blend/">blend</a> to edit the ItemTemplate, then create a simple layout with a border and a 2&#215;2 grid.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image4.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image_thumb4.png" width="607" height="309" /></a></p>
<p><strong>Figure 2: </strong><em>The layout for the DataTemplate of the ListBox</em></p>
<p>Now that I created the grid with four cells, I need to bind the label of the second column to the right properties of the Customers object, so I simply select the label, then ask to DataBind the Content:</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image5.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image_thumb5.png" width="830" height="528" /></a></p>
<p><strong>Figure 3: </strong><em>Thanks to Design Time Data, blend designer can use reflection to understand the properties of the Customers Object, so we can easily choose the property to bind</em></p>
<p>The cool part is that the interface in the designer immediately reflects the result with the Design Time Data</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image6.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image_thumb6.png" width="438" height="225" /></a></p>
<p><strong>Figure 4: </strong><em>Designer uses design time data to render the interface directly inside the designer</em></p>
<p>This is a killer feature because permits you to have a real look at how the UI will be rendered with data. </p>
<p><a href="http://www.codewrecks.com/Files/DesignDataTemplate.zip">Code sample is here</a>.</p>
<p>Gian Maria</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/01/20/design-time-data-in-wpf-and-blend-with-little-effort/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disable Javascript errors in WPF Web Browser Control take2</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/11/17/disable-javascript-errors-in-wpf-web-browser-control-take2/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/11/17/disable-javascript-errors-in-wpf-web-browser-control-take2/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 15:59:08 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/11/17/disable-javascript-errors-in-wpf-web-browser-control-take2/</guid>
		<description><![CDATA[I’ve dealt with this problem some time ago and the solution I posted worked well, but requires you to manipulate the content of the page, injecting script on the DOM. If you want to get rid of annoying messagebox with javascript error, there is another solution based on simply setting the Silent property on the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F11%2F17%2Fdisable-javascript-errors-in-wpf-web-browser-control-take2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F11%2F17%2Fdisable-javascript-errors-in-wpf-web-browser-control-take2%2F&amp;source=alkampfer&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I’ve dealt with this problem <a href="http://www.codewrecks.com/blog/index.php/2010/08/31/disable-javascript-error-in-wpf-webbrowser-control/">some time ago</a> and the solution I posted worked well, but requires you to manipulate the content of the page, injecting script on the DOM. If you want to get rid of annoying messagebox with <a title="javascript" href="http://www.codewrecks.com/blog/index.php/tag/javascript/">javascript</a> error, there is another solution based on simply setting the Silent property on the AXIWebBrowser2 COM control. </p>
<p>The full solution <a target="_blank" href="http://www.brentlamborn.com/post/WPF-Web-Browser-ScriptErrorsSupressed.aspx">was described here</a> by Brent Lamborn and is a pretty good solution that does not require you to manipulate the content of the DOM. The Silent property of the AXIWebBrowser2 control is probably the same Property that you can set through the <em>ScriptErrorSuppressed </em>property of the winform version of <a title="WebBrowser" href="http://www.codewrecks.com/blog/index.php/tag/webbrowser/">WebBrowser</a> control. </p>
<p>I wonder why this property was not ported to the WPF version of the control.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/11/17/disable-javascript-errors-in-wpf-web-browser-control-take2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Write a PropertyViewer like control in WPF</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/07/14/write-a-propertyviewer-like-control-in-wpf/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/07/14/write-a-propertyviewer-like-control-in-wpf/#comments</comments>
		<pubDate>Thu, 14 Jul 2011 08:46:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/07/14/write-a-propertyviewer-like-control-in-wpf/</guid>
		<description><![CDATA[In WPF there is no PropertyViewer control, but if your need is simply to show a read-only list of all properties of an object, for dump purpose, you can obtain this with few lines of code. First of all write a special IValueConverter. 1: [ValueConversion(typeof(Object), typeof(ObservableCollection&#60;PropertyInfo&#62;))] 2: public class PropertyViewerConverter : IValueConverter 3: { 4: [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F07%2F14%2Fwrite-a-propertyviewer-like-control-in-wpf%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F07%2F14%2Fwrite-a-propertyviewer-like-control-in-wpf%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=WPF&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In WPF there is no <a target="_blank" href="http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.controls.propertyviewer.aspx">PropertyViewer</a> control, but if your need is simply to show a read-only list of all properties of an object, for dump purpose, you can obtain this with few lines of code. First of all write a special IValueConverter.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> [ValueConversion(<span style="color: rgb(0, 0, 255);">typeof</span>(Object), <span style="color: rgb(0, 0, 255);">typeof</span>(ObservableCollection&lt;PropertyInfo&gt;))]</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>    <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> PropertyViewerConverter : IValueConverter</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> PropertyInfo</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>            <span style="color: rgb(0, 0, 255);">public</span> String PropertyName { get; <span style="color: rgb(0, 0, 255);">private</span> set; }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>            <span style="color: rgb(0, 0, 255);">public</span> String PropertyValue { get; <span style="color: rgb(0, 0, 255);">private</span> set; }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>            <span style="color: rgb(0, 0, 255);">public</span> PropertyInfo(<span style="color: rgb(0, 0, 255);">string</span> propertyName, <span style="color: rgb(0, 0, 255);">string</span> propertyValue)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>            {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span>                PropertyName = propertyName;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum13">  13:</span>                PropertyValue = propertyValue;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum14">  14:</span>            }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum15">  15:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum16">  16:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum17">  17:</span>        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">object</span> Convert(<span style="color: rgb(0, 0, 255);">object</span> <span style="color: rgb(0, 0, 255);">value</span>, Type targetType, <span style="color: rgb(0, 0, 255);">object</span> parameter, System.Globalization.CultureInfo culture)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum18">  18:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum19">  19:</span>            var retvalue = <span style="color: rgb(0, 0, 255);">new</span> ObservableCollection&lt;PropertyInfo&gt;();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum20">  20:</span>            <span style="color: rgb(0, 0, 255);">if</span> (<span style="color: rgb(0, 0, 255);">value</span> != <span style="color: rgb(0, 0, 255);">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum21">  21:</span>            {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum22">  22:</span>                var properties = <span style="color: rgb(0, 0, 255);">value</span>.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum23">  23:</span>                <span style="color: rgb(0, 0, 255);">foreach</span> (var propertyInfo <span style="color: rgb(0, 0, 255);">in</span> properties)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum24">  24:</span>                { PropertyInfo info;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum25">  25:</span>                    <span style="color: rgb(0, 0, 255);">try</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum26">  26:</span>                    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum27">  27:</span>                        <span style="color: rgb(0, 0, 255);">object</span> pvalue = propertyInfo.GetValue(<span style="color: rgb(0, 0, 255);">value</span>, <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 255);">object</span>[] { }) ?? <span style="color: rgb(0, 96, 128);">&quot;Null&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum28">  28:</span>                        info = <span style="color: rgb(0, 0, 255);">new</span> PropertyInfo(propertyInfo.Name, pvalue.ToString());</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum29">  29:</span>                    }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum30">  30:</span>                    <span style="color: rgb(0, 0, 255);">catch</span> (Exception ex)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum31">  31:</span>                    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum32">  32:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum33">  33:</span>                        info = <span style="color: rgb(0, 0, 255);">new</span> PropertyInfo(propertyInfo.Name, <span style="color: rgb(0, 96, 128);">&quot;Exception &quot;</span> + ex.Message);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum34">  34:</span>                    }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum35">  35:</span>                   </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum36">  36:</span>                    </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum37">  37:</span>                    retvalue.Add(info);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum38">  38:</span>                }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum39">  39:</span>            }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum40">  40:</span>            <span style="color: rgb(0, 0, 255);">return</span> retvalue;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum41">  41:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum42">  42:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum43">  43:</span>        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">object</span> ConvertBack(<span style="color: rgb(0, 0, 255);">object</span> <span style="color: rgb(0, 0, 255);">value</span>, Type targetType, <span style="color: rgb(0, 0, 255);">object</span> parameter, System.Globalization.CultureInfo culture)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum44">  44:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum45">  45:</span>            <span style="color: rgb(0, 0, 255);">throw</span> <span style="color: rgb(0, 0, 255);">new</span> NotImplementedException();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum46">  46:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum47">  47:</span>    }</pre>
<p><!--CRLF--></div>
</div>
<p>The code is straightforward, the converter accepts an object and return an ObservableCollection of PropertyInfo, where propertyinfo is a simple class declared in the converter to hold name and value of the property. You can use this converter in a really simple way.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);"><a title="ListView" href="http://www.codewrecks.com/blog/index.php/tag/listview/">ListView</a></span> <span style="color: rgb(255, 0, 0);">ItemsSource</span><span style="color: rgb(0, 0, 255);">=&quot;{Binding SearchParam, Converter={StaticResource pvc}}&quot;</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>   <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);"><a title="ListView" href="http://www.codewrecks.com/blog/index.php/tag/listview/">ListView</a>.ItemTemplate</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>       <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">DataTemplate</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>           <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">StackPanel</span> <span style="color: rgb(255, 0, 0);">Orientation</span><span style="color: rgb(0, 0, 255);">=&quot;Horizontal&quot;</span> <span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>               <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Label</span> <span style="color: rgb(255, 0, 0);">Content</span><span style="color: rgb(0, 0, 255);">=&quot;{Binding PropertyName}&quot;</span> <span style="color: rgb(255, 0, 0);">Width</span><span style="color: rgb(0, 0, 255);">=&quot;200&quot;</span><span style="color: rgb(0, 0, 255);">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>               <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Label</span> <span style="color: rgb(255, 0, 0);">Content</span><span style="color: rgb(0, 0, 255);">=&quot;{Binding PropertyValue}&quot;</span> <span style="color: rgb(0, 0, 255);">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>           <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">StackPanel</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>       <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">DataTemplate</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>   <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">ListView.ItemTemplate</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span> <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">ListView</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>In the above code I used a Listview bound to a property of the viewmodel called SearchParam that is a complex object with many properties, and thanks to the Converter Iâ€™m able to obtain a list of propertyname-propertyvalue that I can show simply with two labels.</p>
<p>Clearly if you need a real property editor you can check <a target="_blank" href="http://wpfpropertygrid.codeplex.com/">Wpf Property Grid control</a>, or <a target="_blank" href="http://wpg.codeplex.com/">WPF property grid</a>, both of them are open source.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Wpf" rel="tag">Wpf</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/07/14/write-a-propertyviewer-like-control-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick reminder on How-To map items of a list view to a VM command</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/06/23/quick-reminder-on-how-to-map-items-of-a-list-view-to-a-vm-command/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/06/23/quick-reminder-on-how-to-map-items-of-a-list-view-to-a-vm-command/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 13:07:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[MVVM]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/06/23/quick-reminder-on-how-to-map-items-of-a-list-view-to-a-vm-command/</guid>
		<description><![CDATA[The problem is really simple, but sometimes I see people tend to forget a little bit how the DataContext works in WPF and being stuck in wandering why a command is not invoked when a button inside a DataTemplate is pressed. Suppose you have a ListView bounds to a list of items called SingleResult, for [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F06%2F23%2Fquick-reminder-on-how-to-map-items-of-a-list-view-to-a-vm-command%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F06%2F23%2Fquick-reminder-on-how-to-map-items-of-a-list-view-to-a-vm-command%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=MVVM,WPF&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The problem is really simple, but sometimes I see people tend to forget a little bit how the DataContext works in WPF and being stuck in wandering why a command is not invoked when a button inside a DataTemplate is pressed. </p>
<p>Suppose you have a <a title="ListView" href="http://www.codewrecks.com/blog/index.php/tag/listview/">ListView</a> bounds to a list of items called <em>SingleResult</em>, for each SingleResult I have a complex layout and the main ViewModel contains a command that expect a SingleResult parameter called â€œShowDetailsâ€ that simply shows the details of a SingleResult item. I see people do binding in this way.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image18.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb18.png" width="340" height="107" /></a></p>
<p><strong>Figure 1: </strong><em>Code to bind a button to a command</em></p>
<p>When the program runs this is the UI.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image19.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb19.png" width="414" height="159" /></a></p>
<p><strong>Figure 2: </strong><em>The UI in action.</em></p>
<p>Now clicking on the Details button makes nothing to happen. The reason is quite simple, with the syntax in Figure 1, you are actually binding the command to the <em>ShowDetails</em> command of the SingleResult item bound to that row.</p>
<p>This happens because the code is inside a <strong>DataTemplate</strong>, and the DataContext is the specific object used to render the row. In an good <a title="MVVM" href="http://www.codewrecks.com/blog/index.php/tag/mvvm/">MVVM</a> scenario the <em>SingleResult </em>object should have ShowDetails command and everything works good, but if you prefer to define the command to the main ViewModel, you need to change the syntax in this way.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> Command=<span style="color: rgb(0, 96, 128);">&quot;{Binding DataContext.ShowDetails, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}&quot;</span> </pre>
<p><!--CRLF--></div>
</div>
<p>This code works because the command is bound to the ShowDetails defined on the DataContext of the ancestor of type Window, thus, actually binding to the main ViewModel. </p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/MVVM" rel="tag">MVVM</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/06/23/quick-reminder-on-how-to-map-items-of-a-list-view-to-a-vm-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CanResizeWithGrip seems does not work in WPF</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/06/21/canresizewithgrip-seems-does-not-work-in-wpf/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/06/21/canresizewithgrip-seems-does-not-work-in-wpf/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 10:21:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[MetroUi]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/06/21/canresizewithgrip-seems-does-not-work-in-wpf/</guid>
		<description><![CDATA[I have an application that is built with a Metro-Like UI, so each windows has no border and can be resized with the ResizeMode equal to CanResizeWithGrip. Figure 1: The windows can be resized by a little grip on the bottom right border of the window I created another view, use a similar layout from [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F06%2F21%2Fcanresizewithgrip-seems-does-not-work-in-wpf%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F06%2F21%2Fcanresizewithgrip-seems-does-not-work-in-wpf%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=MetroUi,WPF&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I have an application that is built with a Metro-Like UI, so each windows has no border and can be resized with the ResizeMode equal to CanResizeWithGrip.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image13.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb13.png" width="187" height="106" /></a></p>
<p><strong>Figure 1: </strong><em>The windows can be resized by a little grip on the bottom right border of the window</em></p>
<p>I created another view, use a similar layout from the preceding one, but this time the little grip to resize the windows does not appear in the windows.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image14.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb14.png" width="346" height="149" /></a></p>
<p><strong>Figure 2: </strong><em>The resize grip of this view is missing</em></p>
<p>To understand why the grip is missing you need to understand how the window without border is build, here is the important properties of the windows that determines the â€œno borderâ€ style.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image15.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb15.png" width="321" height="79" /></a></p>
<p>One thing you should be aware of, is that the key of everything is the AllowsTransparency set to True, if you set this property to false, all the background will be rendered in black, because even if Bakground=â€Transparentâ€, transparency is disabled in the Windows. Now here is what I see with AllowTransparency disabled.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image16.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb16.png" width="526" height="123" /></a></p>
<p><strong>Figure 3: </strong><em>With AllowTransparency to false I realized that the main grid does not span the entire width of the window</em></p>
<p>The problem is quite subtle, since the main control of the window (a grid) is smaller then the entire window, when the WPF engine renders the window, the little grip maker to resize the window is rendered on a transparent region, as you can see in Figure 4.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image17.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb17.png" width="493" height="159" /></a></p>
<p><strong>Figure 4: </strong><em>Turning transparency on and moving the windows against a dark background now I see the marker</em></p>
<p>The marker was correctly rendered, but since the real dimension of the window is bigger than the visible content, I do not see him where Iâ€™m expecting it to be, and this makes me think that it was not correctly rendered. </p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Wpf" rel="tag">Wpf</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/06/21/canresizewithgrip-seems-does-not-work-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manipulate a FlowDocument in WPF</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/06/20/manipulate-a-flowdocument-in-wpf/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/06/20/manipulate-a-flowdocument-in-wpf/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 15:39:19 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/06/20/manipulate-a-flowdocument-in-wpf/</guid>
		<description><![CDATA[Sample Code is Here. This is the scenario: I have a FlowDocument in a FlowDocumentScrollViewer as in Figure 1. Figure 1: Initial scenario, a simple flow document. My goal is to be able to highlight some words in the document and show some detailed info about highlighted word to the user. It turns out after [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F06%2F20%2Fmanipulate-a-flowdocument-in-wpf%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F06%2F20%2Fmanipulate-a-flowdocument-in-wpf%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=WPF&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.codewrecks.com/files/wpfflowdocument.zip">Sample Code is Here.</a></p>
<p>This is the scenario: I have a <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms601064.aspx">FlowDocument</a> in a FlowDocumentScrollViewer as in Figure 1.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image20.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb20.png" width="509" height="194" /></a></p>
<p><strong>Figure 1: </strong><em>Initial scenario, a simple flow document.</em></p>
<p>My goal is to be able to highlight some words in the document and show some detailed info about highlighted word to the user. It turns out after some experiments that the code is quite simple. In the first part of the snippet I search content inside the Paragraph.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> Paragraph p = (Paragraph)document.Document.Blocks.FirstBlock;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> originalRunText = ((Run)p.Inlines.FirstInline).Text;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> String word = <span style="color: rgb(0, 96, 128);">&quot;consectetur&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span> var textSearchRange = <span style="color: rgb(0, 0, 255);">new</span> TextRange(p.ContentStart, p.ContentEnd);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span> Int32 position = textSearchRange.Text.IndexOf(word, StringComparison.OrdinalIgnoreCase);</pre>
<p><!--CRLF--></div>
</div>
<p>The code works because I already know that my <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms601064.aspx">FlowDocument</a> has only one <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms522790.aspx">Paragraph</a> that in turn contains a single <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms522796.aspx">Run</a>, so I can easily grab the original Text (it will be used to restore the original <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> and remove the highligh), in line 5 I create a TextRange that span the whole paragraph, then I use the IndexOf function to find the text in the range. Now I can proceed to text modification</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">if</span> (position &lt; 0) <span style="color: rgb(0, 0, 255);">return</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> TextPointer start;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span> start = textSearchRange.Start.GetPositionAtOffset(position);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span> var end = textSearchRange.Start.GetPositionAtOffset(position + word.Length);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span> var textR = <span style="color: rgb(0, 0, 255);">new</span> TextRange(start, end);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span> textR.Text = <span style="color: rgb(0, 96, 128);">&quot;&quot;</span>;</pre>
<p><!--CRLF--></div>
</div>
<p>After a simple check to verify that we found the text to highlight, I create a range that comprehend the whole match and set Text value to an empty string to completely remove the text. This is done because I will highlight the text inserting a completely new Run.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> ToolTip tt = <span style="color: rgb(0, 0, 255);">new</span> ToolTip();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>  tt.Background = Brushes.LightYellow;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>  tt.Content = <span style="color: rgb(0, 0, 255);">new</span> Label() {Content = <span style="color: rgb(0, 96, 128);">&quot;Tooltip of HighLighted word&quot;</span>};</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>  Run newRun = <span style="color: rgb(0, 0, 255);">new</span> Run(word, start);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>  newRun.FontSize = 30;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>  newRun.ToolTip = tt;</pre>
<p><!--CRLF--></div>
</div>
<p>I created a tooltip, but the important part is in line 6 where I create another run with the word I want to highlight setting as start location the location where I found the word, I set the font size to 30 to highlight it (you can change whatever property you want) and assign the tooltip to the run. Here is the result.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/SNAGHTML1adb227.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="SNAGHTML1adb227" border="0" alt="SNAGHTML1adb227" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/SNAGHTML1adb227_thumb.png" width="531" height="362" /></a></p>
<p><strong>Figure 2: </strong><em>The code will highlight a chosen word, and we also have a cool tooltip to show advanced info</em></p>
<p>The restore button simply clear all the paragraph content and insert again the original text in a single Run.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> Paragraph p = (Paragraph)document.Document.Blocks.FirstBlock;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> p.Inlines.Clear();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> p.Inlines.Add(<span style="color: rgb(0, 0, 255);">new</span> Run(originalRunText));</pre>
<p><!--CRLF--></div>
</div>
<p>Quick and simple. <a href="http://www.codewrecks.com/files/wpfflowdocument.zip">Code is Here</a>.</p>
<p>alk.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/06/20/manipulate-a-flowdocument-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Event to Command in WPF MVVM application</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/05/04/event-to-command-in-wpf-mvvm-application/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/05/04/event-to-command-in-wpf-mvvm-application/#comments</comments>
		<pubDate>Wed, 04 May 2011 09:21:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[MVVM]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/05/04/event-to-command-in-wpf-mvvm-application/</guid>
		<description><![CDATA[I needed a simple way to obtain this simple result: whenever a certain component in the UI (a WebBrowser control) raises some specific event, I want a command in the VM to be executed, without the need to specify any command parameter. The only requirement I want is avoiding a single line of code in [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F05%2F04%2Fevent-to-command-in-wpf-mvvm-application%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F05%2F04%2Fevent-to-command-in-wpf-mvvm-application%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=MVVM,WPF&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I needed a simple way to obtain this simple result: whenever a certain component in the UI (a <a title="WebBrowser" href="http://www.codewrecks.com/blog/index.php/tag/webbrowser/">WebBrowser</a> control) raises some specific event, I want a command in the VM to be executed, without the need to specify any command parameter. The only requirement I want is avoiding a single line of code in the UI <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  because, having no code in the UI is one of the main benefit of the <a title="MVVM" href="http://www.codewrecks.com/blog/index.php/tag/mvvm/">MVVM</a> model.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/04/image15.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/04/image_thumb15.png" width="380" height="285" /></a></p>
<p>To keep everything simple I want a simple syntax that permits me to specify that when an event of type X is raised, a command of name Y should be called.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> &lt;Controls:xxxWebBrowserManagerFlexible x:Name=<span style="color: #006080">&quot;wbBrowser&quot;</span> Margin=<span style="color: #006080">&quot;0,0,0,0&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>                BrowserType=<span style="color: #006080">&quot;InternetExplorer&quot;</span>   </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>                Links=<span style="color: #006080">&quot;{Binding LinksOfThePage}&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>                Behaviours:EventToCommandBehavior.Bind=<span style="color: #006080">&quot;DocumentCompleted-SignalDocumentComplete&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>                RawHtmlContent=<span style="color: #006080">&quot;{Binding FullHtmlContent, Mode=TwoWay}&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>                SelectedText=<span style="color: #006080">&quot;{Binding BrowserSelectedText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>                CurrentUrl=<span style="color: #006080">&quot;{Binding UrlToNavigate, Mode=TwoWay}&quot;</span> /&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>As you can see I created a simple behavior called <strong>EventToCommandBehavior</strong> that accepts a very simple syntax: a string of format <em>EvenName-CommandName. </em>This solution probably is not so elegant, it has no intellisense or designer support, but it works and it is supersimple. My Behavior has an Attached property called <strong>Bind</strong></p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> DependencyProperty BindProperty =</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>    DependencyProperty.RegisterAttached</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>    (</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>        <span style="color: #006080">&quot;Bind&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>        <span style="color: #0000ff">typeof</span>(String),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>        <span style="color: #0000ff">typeof</span>(EventToCommandBehavior),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>        <span style="color: #0000ff">new</span> UIPropertyMetadata(String.Empty, OnBindChanged)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>    );</pre>
<p><!--CRLF--></div>
</div>
<p>All the dirty work is done inside the OnBindChanged.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Dictionary&lt;Object, String&gt; _routes</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>      = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #0000ff">object</span>, String&gt;();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>  <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> OnBindChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>  {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>      <span style="color: #0000ff">if</span> (String.IsNullOrWhiteSpace((String) args.NewValue)) <span style="color: #0000ff">return</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>      String[] bind = ((String) args.NewValue).Split(<span style="color: #006080">'-'</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>      EventInfo ev = dpo.GetType().GetEvent(bind[0]);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>      MethodInfo handler = <span style="color: #0000ff">typeof</span>(EventToCommandBehavior)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>          .GetMethod(<span style="color: #006080">&quot;Handler&quot;</span>, BindingFlags.NonPublic | BindingFlags.Static);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>      var eh = Delegate.CreateDelegate(ev.EventHandlerType, <span style="color: #0000ff">null</span>, handler);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>      var minfo = ev.GetAddMethod();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>      minfo.Invoke(dpo, <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>[] { eh });</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>      </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>      <span style="color: #008000">//now go for the command.</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>      _routes.Add(dpo, bind[1]);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>  }</pre>
<p><!--CRLF--></div>
</div>
<p>The core part is finding the EventInfo for choosen event, and <a href="http://www.codewrecks.com/blog/index.php/2011/04/22/handle-an-event-with-reflection/">create dynamically an handler</a> capable to handle every event. The association between EventName and CommandName is stored inside a dictionary object, and the handler is really simple, just get a reference to the command with reflection and call the Execute method.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Handler(Object sender, EventArgs e)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>  {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>      String commandName;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>      <span style="color: #0000ff">if</span> (_routes.TryGetValue(sender, <span style="color: #0000ff">out</span> commandName))</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>          FrameworkElement fe = sender <span style="color: #0000ff">as</span> FrameworkElement;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>          <span style="color: #0000ff">if</span> (fe.DataContext != <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>          {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>              PropertyInfo pinfo = fe.DataContext.GetType().GetProperty(commandName);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>              <span style="color: #0000ff">if</span> (pinfo != <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>              {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>                  ICommand command = (ICommand) pinfo.GetValue(fe.DataContext, <span style="color: #0000ff">null</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>                  <span style="color: #0000ff">if</span> (command.CanExecute(<span style="color: #0000ff">null</span>))</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>                  {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>                      command.Execute(<span style="color: #0000ff">null</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>                  }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>              }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>          }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>      }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>  }</pre>
<p><!--CRLF--></div>
</div>
<p>Since the View Model that contains the ICommand is the DataContext of the element that raised the event, I simply use the <a target="_blank" href="http://msdn.microsoft.com/en-us/library/3x3ss54h.aspx">GetProperty()</a> method to find the right <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms616869.aspx">ICommand</a>, and finally call the Execute method to actually call the command. Since one of the prerequisites states that we need to pass no command parameters, I simply pass null to Execute method.</p>
<p>Alk.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/05/04/event-to-command-in-wpf-mvvm-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

