<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>ArgumentNullException("blogPost")</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/feed.xml</link>
    <description>Oliver&amp;#039;s musings about .NET and Windows development</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:15:29 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>301 Moved Permanently</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/301-moved-permanently</link>
      <pubDate>Tue, 17 Jun 2014 12:40:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2014/06/17/301-moved-permanently/</guid>
      <description><![CDATA[I moved my MSDN blog over to a Ghost Blog instance that I host on my Azure account. To my suprise...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size: large;">I moved my MSDN blog over to a <a href="https://github.com/TryGhost">Ghost Blog</a> instance that I host on my Azure account. To my suprise the setup was super easy. Basically fire up a new Azure Web Site and use the Ghost Blog image from the gallery to get started. After the Azure web site setup I upgraded the blog to the latest version (v0.4.2) and rolled out a new template. A few customizations here and there - et voil&agrave; - done! </span></p>
<p><span style="font-size: large;">Link to my new blog: <a href="http://www.oliver-lohmann.me">http://www.oliver-lohmann.me</a></span></p>
<p><span style="font-size: large;">Happy reading!</span></p>]]></content:encoded>
    </item>
    <item>
      <title>Little Helper - Random Contact/Name Generator</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/little-helper-random-contactname-generator</link>
      <pubDate>Mon, 10 Feb 2014 09:03:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2014/02/10/little-helper-random-contactname-generator/</guid>
      <description><![CDATA[I am writing a lot of sample code and usually I have to fill my samples with some random data. A...]]></description>
      <content:encoded><![CDATA[<p>I am writing a lot of sample code and usually I have to fill my samples with some random data. A typical entity in most scenarios is a person or a contact. Since I am tired with putting in some static names here and there and copy and paste them between samples, I wrote a little library that generates random contact sequences.</p>
<p>It takes the most common first and last names of the US and generates random combinations of them. It also supports genders so you can differentiate between female and male contacts.</p>
<p><strong>Usage/Installation</strong>&nbsp;is straight forward, see GitHub for details:&nbsp;<a title="https://github.com/olohmann/Lohmann.RndContacts" href="https://github.com/olohmann/Lohmann.RndContacts">https://github.com/olohmann/Lohmann.RndContacts</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Øredev 2013</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/redev-2013</link>
      <pubDate>Thu, 14 Nov 2013 08:32:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2013/11/14/redev-2013/</guid>
      <description><![CDATA[In this post I summarize my highlights from visiting the &Oslash;redev 2013 conference. First of...]]></description>
      <content:encoded><![CDATA[<p>In this post I summarize my highlights from visiting the <a href="http://oredev.org/2013/">&Oslash;redev 2013 conference</a>. First of all, I really enjoyed the broad scope of the conference! In my opinion, it is always healthy to think outside of your technology/approach box to avoid a certain routine-blindness.</p>
<p>Here is my at-a-glance-list of hot topics from &Oslash;redev 2013:</p>
<ul>
<li>JavaScript</li>
<li>Functional Programming</li>
<li>Domain-Driven Design</li>
<li>Micro Services</li>
<li>Continuous Delivery</li>
</ul>
<h3>JavaScript</h3>
<p>JavaScript is everywhere - with <a href="http://nodejs.org/">NodeJS</a> even on the server side. Some people already talk about JavaScript as the <a href="http://www.hanselman.com/blog/JavaScriptisAssemblyLanguagefortheWebPart2MadnessorjustInsanity.aspx">VM or assembly language of the web</a>. I do not want to recite all of these right statements. Instead I would like to share the impressions about JavaScipt I got from the sessions at &Oslash;redev. A lot of speakers had something to say about JavaScript. Unsurprisingly all of them agreed that JavaScript is a flawed language - and I concur. The <a href="http://oredev.org/2013/wed-fri-conference/go-to-there-and-back-again">most interesting session</a> on JavaScript was held by Douglas Crockford. He predicted that we might be stuck another 40(!) years with JavaScript and its shortcomings. Looking back in our industry's history of dealing with sub-optimal programming concepts like the the Go To statement, a number like 40 years might be an optimistic guess. It took us around 30 years from <a href="http://en.wikipedia.org/wiki/Considered_harmful">Edsger Dijkstra famous letter "Go To Statement Considered Harmful"</a> to the actual demise of the Go To statement in a mainstream programming language (Java, 1995). And this was a rather small issue to fix.</p>
<p>What can you do today if you are not happy with JavaScript but need to work with it? Tools like <a href="http://www.jslint.com/">JSLint</a> or <a href="http://www.jshint.com">JSHint</a> can help you in identifying the obvious bad parts of JavaScript in your code base (look <a href="https://www.destroyallsoftware.com/talks/wat">WAT</a> if you don't know what I mean). In addition, you might benefit from <a href="http://www.typescriptlang.org/">TypeScript</a> or <a href="http://coffeescript.org/">CoffeeScript</a> and their approach of compiling into JavaScript. They are bringing things like classical inheritance to the table. Typescript even introduces type-safety to the language.</p>
<p><em style="font-size: 12px;">As a side note</em><span style="font-size: 12px;">: Douglas Crockford was not really enthusiastic about bringing the classical inheritance concept to JavaScript (nor about the existing prototypical inheritance concept). He argues that statefulness and conditional logic in OO-languages are also flawed and tend to over-complicate stuff. In his regard, the future lies more in functional programming. No surprise that functional programming was also one at the hot topics at the conference.</span></p>
<h3>Functional Programming</h3>
<p>Functional programming was present with a lot of sessions. Why is functional programming getting so much traction over the last years?</p>
<p>One of the biggest challenges in nowadays' systems is dealing with their high complexity. Complexity arises from mutating state, piling up conditional logic and producing vast amounts of code. Functional programming is no silver bullet but offers concepts to deal with these challenges. Immutable types, pattern matching and a succinct but readable notation help developers to write better code. There was quite a controversial talk from Bodil Stokke titled <a href="http://oredev.org/2013/wed-fri-conference/programming-only-better">"Programming, Only Better"</a>. Unfortunately it does no appear on the &Oslash;redev video listing. It analyzed the state of our current mainstream programming concepts and demonstrated how functional languages may address some of the issues.</p>
<p>If you are programming on the .NET platform, you are in the lucky spot to have already a lot of functional goodness at your fingertips. In 2007 we introduced C# 3 and added features like lambdas or LINQ to the language. And you can even go further into functional programming on the .NET platform with <a href="http://www.tryfsharp.org/">F#</a>. The great thing is that it is absolutely easy to combine C# and F# together in your code base. They both compile to same CLR bytecode. You should watch <a href="http://oredev.org/2013/wed-fri-conference/f-for-c-developers">Phil Trelford's talk</a> to get an in depth impression. From his talk I took away the idea to model my domain classes in F# from now on. I really loved the terseness (hope to blog about it soon).</p>
<h3>Domain-Driven Design</h3>
<p>Domain-driven design (DDD) cannot be overlooked these days as a conceptual approach to develop and architect complex software. If you have not heard of it you might want to read a <a href="http://www.infoq.com/minibooks/domain-driven-design-quickly">quick summary</a>. Understanding the idea of a bounded context will be one of the "aha" moments in your SW development career (at least it was for me). Although not a silver bullet, it seems to be the best approach we have these days to tackle complexity at an architectural level. A couple of sessions addressed DDD. One of them was <a href="http://oredev.org/2013/wed-fri-conference/entity-framework-in-core-business-applications-that-leverage-ddd">Julie Lerman's talk</a> on applying DDD concepts with Entity Framework as the persistence infrastructure. Another one was <a href="http://oredev.org/2013/wed-fri-conference/aesthetics-and-the-beauty-of-an-architecture-adventures-in-cqrs-and-event-sourcing">Tom Scott's talk</a> on CQRS/EventSourcing and DDD.</p>
<h3>Micro Services</h3>
<p>If you drive the DDD idea of bounded contexts to an absolute extreme you end up with something called Micro Services. Think of masses of tiny REST/web services that are hooked together via a high throughput bus. Mix that with a few radical ideas such as a wild mix of technologies, allowance of copy &amp; paste programming and a deliberate throw-away mentality. Sounds a bit crazy? Yes, that was my reaction, too. Fred George's talk was the most unsettling one at &Oslash;redev. Unfortunately, the <a href="http://oredev.org/2013/wed-fri-conference/implementing-micro-service-architectures">session recording</a> is not available (yet). As an alternative you can watch the <a href="https://www.youtube.com/watch?v=2rKEveL55TY">recording</a> from a ruby conference in 2012. <em>Side note:</em> I read about a similar concept on <a href="http://blog.ralfw.de/">Ralf Westphal's blog</a>. Unfortunately, <a href="http://blog.ralfw.de/2013/03/software-als-web-of-services.html">his article</a> is only available in German.</p>
<h3>Continuous Delivery</h3>
<p>How often do you deploy your software? Once every two months? Every week? There are people out there deploying their Software more than 100 times - EACH DAY. Yes, deployments to the live system - not only to the alpha or staging area. Sounds crazy? Not when you actually think through it. What is the biggest issue in SW development these days? Yes, you read that already in another paragraph - it is complexity. How many changes are typically rolled out when you deploy to production once every two months? 10 features, a couple of bug fixes summing up to maybe 3000 lines of code that were touched? Is your system always stable after deployment? Is it likely that - although you applied best practices as automatic testing - some issues will still pop up? How would you monitor your production system to detect issues that might have been raised from a deployment?</p>
<p>Wouldn't it be easier if you could deploy each tiny feature/fix immediately and monitor their effectiveness in a very advanced fashion? Just touching a couple of lines of code would also reduce the time you need to resolve potential issues. And while you resolve the issue, the tiny increment is reverted. And that's the main idea of continuous delivery (CD) - a high speed feedback loop that allows you to deliver new value to your software as often as you need it.</p>
<p>But who actually needs several updates a day? Companies want to be more nimble than their competitors when it comes to explore new possibilities for products and offerings! They want to experiment with stuff quickly and easily. There are <a href="http://ai.stanford.edu/~ronnyk/ExPThinkWeek2009Public.pdf">statistics</a> that only a third of all features that are incorporated into a SW are used to the extend that was originally thought off. Yes, you read right. Two thirds are usually <em>waste</em> that most people don't need nor use. In <a href="http://msdn.microsoft.com/en-us/library/vstudio/hh533841.aspx">lean development</a> you want to detect and eliminate waste quickly. With CD you have the tool, to put some initial effort into a new idea and drive some statistics how users respond to it. If they like it, continue the development. If not shut it down quickly. The turn-around times allow you to experiment.</p>
<p>As you can imagine the shift to CD is not something you just do over night. It requires a very sophisticated environment that allows operations and development to work seamlessly together - also known under the headline <a href="http://puppetlabs.com/solutions/devops">DevOps</a>. Jez Humble's <a href="http://oredev.org/2013/wed-fri-conference/adopting-continuous-delivery">talk</a> gives a great introduction into adopting continuous delivery. There is also a <a href="http://martinfowler.com/books/continuousDelivery.html">book</a> by Jez for anyone that wants to deep dive into the topic.</p>
<p>When putting the idea of Micro Services into context with CD it becomes clear that both approaches fit together nicely. The Micro Services architecture is all about quick integration and easy discontinuation of services. Exactly the thing that you want to achieve in CD.</p>
<h3>My Takeaway</h3>
<p>The conference confirmed my feeling that our industry is evolving at different speed levels. A majority that does not evolve at all or very slow and a minority that picks up change and (r)evoluational ideas pretty quickly. The latter are present at conferences, develop cool new products and drive the community. The others that are the gross of our industry cling longer to old things and do not embrace change progressively. Scott Hanselman recently <a href="http://hanselminutes.com/394/demystifying-scala-with-netflixs-dianne-marsh">called them</a> <em>dark matter programmers</em>. They make up the majority of our industry but you do not know that they exist. They program procedural are typically a bit waterfall'y and are not eager to try new things. Their systems get bigger and bigger day by day and they drown in complexity. And instead of investing into things like functional programming, Continuous Delivery, DDD or Micro Services they continue to do the same stuff as 10-40 years ago. Lost opportunities?</p>]]></content:encoded>
    </item>
    <item>
      <title>An Rx-Enabled WPF AutoComplete TextBox - Part 2/2</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/an-rx-enabled-wpf-autocomplete-textbox-part-22</link>
      <pubDate>Fri, 21 Jun 2013 05:51:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2013/06/21/an-rx-enabled-wpf-autocomplete-textbox-part-22/</guid>
      <description><![CDATA[In my last blog post I described the general idea of using Rx to handle typical query situation with...]]></description>
      <content:encoded><![CDATA[<p>In my <a href="http://blogs.msdn.com/b/argumentnullexceptionblogpost/archive/2013/06/21/an-rx-enabled-wpf-control-part-1-2.aspx">last blog post</a> I described the general idea of using Rx to handle typical query situation with all their pitfalls. Let us take the idea one step further in developing a WPF control that directly makes use of Rx. The source code excerpts in this post are simplified and I&nbsp;tried to focus just on the important aspects. The final code looks a bit different and can be downloaded from <a href="https://skydrive.live.com/redir?resid=DDD7D9C98FDFC0E2!2295&amp;authkey=!ADi1kRhSSlgG6q8">SkyDrive</a>.</p>
<h3>Basic Control Architecture</h3>
<p>&nbsp;Let us start with the general control architecture of our AutoCompleteTextBox. I really like WPF's <em>Lego pieces&nbsp;</em>nature in this regard. Instead of doing everything from scratch we can utilize existing controls to implement a new one: We need a TextBox for the input, a Popup for providing a panel for our results and a ListBox to show the actual results. The one thing that we are going to implement is a custom control that orchestrates the existing controls. The following figure should clarify this:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/1638.ControlArchitecture.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/1638.ControlArchitecture.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/1638.ControlArchitecture.png" original-url="http://blogs.msdn.com/resized-image.ashx/__size/350x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/1638.ControlArchitecture.png" alt="" border="0" /></a></p>
<p>The AutoCompleteTextBox is not really TextBox, that is it does not inherit from TextBox but from control. It declares the parts mentioned above via the TemplatePart attribute:</p>
<pre style="background: white; color: black; font-family: Consolas;">[<span style="color: #2b91af;">TemplatePart</span>(Name&nbsp;=&nbsp;PartTextBox,&nbsp;Type&nbsp;=&nbsp;<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">TextBox</span>))]
[<span style="color: #2b91af;">TemplatePart</span>(Name&nbsp;=&nbsp;PartPopup,&nbsp;Type&nbsp;=&nbsp;<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Popup</span>))]
[<span style="color: #2b91af;">TemplatePart</span>(Name&nbsp;=&nbsp;PartListBox,&nbsp;Type&nbsp;=&nbsp;<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">ListBox</span>))]
<span style="color: blue;">public</span>&nbsp;<span style="color: blue;">class</span>&nbsp;<span style="color: #2b91af;">AutoCompleteTextBox</span>&nbsp;:&nbsp;<span style="color: #2b91af;">Control</span>
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">public</span>&nbsp;<span style="color: blue;">const</span>&nbsp;<span style="color: blue;">string</span>&nbsp;PartTextBox&nbsp;=&nbsp;<span style="color: #a31515;">"PART_TextBox"</span>;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">public</span>&nbsp;<span style="color: blue;">const</span>&nbsp;<span style="color: blue;">string</span>&nbsp;PartPopup&nbsp;=&nbsp;<span style="color: #a31515;">"PART_Popup"</span>;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">public</span>&nbsp;<span style="color: blue;">const</span>&nbsp;<span style="color: blue;">string</span>&nbsp;PartListBox&nbsp;=&nbsp;<span style="color: #a31515;">"PART_ListBox"</span>;</pre>
<pre style="background: white; color: black; font-family: Consolas;">    [...]</pre>
<p>As always when working with parts you need to get hold on them in the OnApplyTemplate method:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">public</span>&nbsp;<span style="color: blue;">override</span>&nbsp;<span style="color: blue;">void</span>&nbsp;OnApplyTemplate()
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">base</span>.OnApplyTemplate();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;partTextBox&nbsp;=&nbsp;<span style="color: blue;">this</span>.GetTemplateChild(PartTextBox)&nbsp;<span style="color: blue;">as</span>&nbsp;<span style="color: #2b91af;">TextBox</span>;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">if</span>&nbsp;(partTextBox&nbsp;==&nbsp;<span style="color: blue;">null</span>)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">throw</span>&nbsp;<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Associated&nbsp;ControlTemplate&nbsp;has&nbsp;a&nbsp;bad&nbsp;part&nbsp;configuration.&nbsp;Expected&nbsp;a&nbsp;TextBox&nbsp;part."</span>);
&nbsp;&nbsp;&nbsp;&nbsp;}</pre>
<pre style="background: white; color: black; font-family: Consolas;">    [...] // grab all other parts </pre>
<p>&nbsp;I am being a bit careful here since just declaring&nbsp;our Parts as Meta-Information does not guarantee that they are really present. Failing immediately with an exception makes it easier to spot the misconfiguration in the Control Template. Speaking of the Control Template - here is an simplified version of it:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">Style</span><span style="color: red;">&nbsp;TargetType</span><span style="color: blue;">="{</span><span style="color: #a31515;">x</span><span style="color: blue;">:</span><span style="color: #a31515;">Type</span><span style="color: red;">&nbsp;local</span><span style="color: blue;">:</span><span style="color: red;">AutoCompleteTextBox</span><span style="color: blue;">}</span><span style="color: blue;">"</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Setter</span><span style="color: red;">&nbsp;Property</span><span style="color: blue;">=</span><span style="color: blue;">"Focusable"</span><span style="color: red;">&nbsp;Value</span><span style="color: blue;">=</span><span style="color: blue;">"False"</span><span style="color: blue;">&gt;&lt;/</span><span style="color: #a31515;">Setter</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Setter</span><span style="color: red;">&nbsp;Property</span><span style="color: blue;">=</span><span style="color: blue;">"Template"</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Setter.Value</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">ControlTemplate</span><span style="color: red;">&nbsp;TargetType</span><span style="color: blue;">="{</span><span style="color: #a31515;">x</span><span style="color: blue;">:</span><span style="color: #a31515;">Type</span><span style="color: red;">&nbsp;local</span><span style="color: blue;">:</span><span style="color: red;">AutoCompleteTextBox</span><span style="color: blue;">}</span><span style="color: blue;">"</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Grid</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">TextBox</span><span style="color: red;">&nbsp;x</span><span style="color: blue;">:</span><span style="color: red;">Name</span><span style="color: blue;">=</span><span style="color: blue;">"PART_TextBox"</span><span style="color: blue;">&nbsp;/&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Popup</span><span style="color: red;">&nbsp;x</span><span style="color: blue;">:</span><span style="color: red;">Name</span><span style="color: blue;">=</span><span style="color: blue;">"PART_Popup"</span><span style="color: blue;">&gt;</span>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Grid</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">ListBox</span><span style="color: red;">&nbsp;x</span><span style="color: blue;">:</span><span style="color: red;">Name</span><span style="color: blue;">=</span><span style="color: blue;">"PART_ListBox"</span><span style="color: red;">&nbsp;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red;">&nbsp;ItemsSource</span><span style="color: blue;">="{</span><span style="color: #a31515;">TemplateBinding</span><span style="color: red;">&nbsp;Results</span><span style="color: blue;">}</span><span style="color: blue;">" </span><span style="color: blue;">/&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Grid</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Popup</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Grid</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Setter.Value</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Setter</span><span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Style</span><span style="color: blue;">&gt;</span></pre>
<p><br />Very good - all required&nbsp;parts are present in the control template. With the visuals and outlets&nbsp;in place let us investigate the internals of the control.</p>
<h3>Control Internals</h3>
<p>So far we have a custom control called AutoCompleteTextBox that has access to&nbsp;a TextBox, Popup and ListBox. Now let us apply some Rx (I won't explain the Rx code, see my <a title="previous post" href="http://blogs.msdn.com/b/argumentnullexceptionblogpost/archive/2013/06/21/an-rx-enabled-wpf-control-part-1-2.aspx">previous post</a>&nbsp;for background information). What we would like to achieve is that we can combine the TextBox' TextChanged event stream with a query function that returns query results. Hooking up the TextChanged events is simple enough:</p>
<p><span style="color: blue;">public</span>&nbsp;<span style="color: blue;">class</span>&nbsp;<span style="color: #2b91af;">AutoCompleteTextBox</span>&nbsp;:&nbsp;<span style="color: #2b91af;">Control</span><br />{<br />&nbsp;<span style="color: blue;">&nbsp;&nbsp;&nbsp; public</span>&nbsp;<span style="color: blue;">override</span>&nbsp;<span style="color: blue;">void</span>&nbsp;OnApplyTemplate() &nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; {&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">base</span>.OnApplyTemplate();</p>
<pre style="background: white; color: black; font-family: Consolas;">        // [...]</pre>
<pre style="background: white; color: black; font-family: Consolas;">        var textChangedObsevable&nbsp;=&nbsp;<span style="color: #2b91af;">Observable</span>.FromEventPattern&lt;<span style="color: #2b91af;">TextChangedEventArgs</span>&gt;(partTextBox,&nbsp;<span style="color: #a31515;">"TextChanged"</span>)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(evt&nbsp;=&gt;&nbsp;((<span style="color: #2b91af;">TextBox</span>)evt.Sender).Text);</pre>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;"> var</span>&nbsp;constraintTextChangedObservable&nbsp;=&nbsp;textChangedObsevable
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(text&nbsp;=&gt;&nbsp;text&nbsp;!=&nbsp;<span style="color: blue;">null</span>&nbsp;&amp;&amp;&nbsp;text.Length&nbsp;&gt;=&nbsp;<span style="color: blue;">this</span>.<span style="background-color: #ffff00;">MinimumCharacters</span>)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.DistinctUntilChanged()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Throttle(<span style="color: #2b91af;">TimeSpan</span>.FromMilliseconds(<span style="color: blue;">this</span>.<span style="background-color: #ffff00;">Delay</span>));<br />        // [...]<br />     }<br />}</pre>
<p>The highlighted properties in the code snippet are simple Dependency Properties declared on the AutoCompleteTextBoxControl. They constraint the text event stream to avoid too frequent query polling. Next thing we need to do is use the stream of text input to call into a query result provider. And here the control follows a rather unusual path. It basically allows you to bind a provider function that looks like this:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: #2b91af;">Func</span>&lt;<span style="color: #2b91af;">AutoCompleteQuery</span>,&nbsp;<span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IAutoCompleteQueryResult</span>&gt;&gt;&gt;
<br /><span style="font-family: arial,helvetica,sans-serif;"><em>with the </em><strong>AutoCompleteQuery</strong><em> defined as:</em></span></pre>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">public</span>&nbsp;<span style="color: blue;">class</span>&nbsp;<span style="color: #2b91af;">AutoCompleteQuery</span>
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">public</span>&nbsp;AutoCompleteQuery(<span style="color: blue;">string</span>&nbsp;term)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">this</span>.Term&nbsp;=&nbsp;term;
&nbsp;&nbsp;&nbsp;&nbsp;}
 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">public</span>&nbsp;<span style="color: blue;">string</span>&nbsp;Term&nbsp;{&nbsp;<span style="color: blue;">get</span>;&nbsp;<span style="color: blue;">private</span>&nbsp;<span style="color: blue;">set</span>;&nbsp;}
}</pre>
<pre style="background: white; color: black; font-family: Consolas;"><em><span style="font-family: arial,helvetica,sans-serif;">and </span></em><span style="font-family: arial,helvetica,sans-serif;"><strong>IAutoCompleteQueryResult</strong></span><em><span style="font-family: arial,helvetica,sans-serif;"> defined as:<br /></span></em></pre>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">public</span>&nbsp;<span style="color: blue;">interface</span>&nbsp;<span style="color: #2b91af;">IAutoCompleteQueryResult</span>
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">string</span>&nbsp;Title&nbsp;{&nbsp;<span style="color: blue;">get</span>;&nbsp;}
}&nbsp;&nbsp;</pre>
<pre style="background: white; color: black; font-family: Consolas;">&nbsp;</pre>
<p>This deserves some explanation.&nbsp;The function that takes an AutoCompleteQuery as input. This is in the&nbsp;end the input a user&nbsp;has put into the&nbsp;TextBox.&nbsp;In response it returns an Observable which yields IEnumerables of query results. I use an interface here to make sure that there is a textual representation of the query result which can in the end be put into the TextBox. The result provider function is not directly defined as a dependency property because the WPF designer had some issues in the preview although run-time and compile-time wise everything would be fine. Therefore there is a little wrapper that works around this issue. The code of the dependency property:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">public</span>&nbsp;<span style="color: #2b91af;">AutoCompleteQueryResultProvider</span>&nbsp;AutoCompleteQueryResultProvider
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">get</span>
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">return</span>&nbsp;(<span style="color: #2b91af;">AutoCompleteQueryResultProvider</span>)GetValue(QueryResultFunctionProperty);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">set</span>
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetValue(QueryResultFunctionProperty,&nbsp;<span style="color: blue;">value</span>);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
 
<span style="color: blue;">public</span>&nbsp;<span style="color: blue;">static</span>&nbsp;<span style="color: blue;">readonly</span>&nbsp;<span style="color: #2b91af;">DependencyProperty</span>&nbsp;QueryResultFunctionProperty&nbsp;=&nbsp;<span style="color: #2b91af;">DependencyProperty</span>.Register(
&nbsp;&nbsp;&nbsp;&nbsp;"AutoCompleteQueryResultProvider",
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">AutoCompleteQueryResultProvider</span>),
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">AutoCompleteTextBox</span>),
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">UIPropertyMetadata</span>(<span style="color: #2b91af;">AutoCompleteQueryResultProvider</span>.Empty));
</pre>
<pre style="background: white; color: black; font-family: Consolas;">&nbsp;</pre>
<pre style="background: white; color: black; font-family: Consolas;"><span style="font-family: arial,helvetica,sans-serif;"><em>...and the wrapper class </em><strong>AutoCompleteQueryResultProvider</strong><em>:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /></em></span></pre>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">public</span>&nbsp;<span style="color: blue;">class</span>&nbsp;<span style="color: #2b91af;">AutoCompleteQueryResultProvider</span>
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">[...]<br /> </span> 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;summary&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;Initializes&nbsp;a&nbsp;new&nbsp;instance&nbsp;of&nbsp;the&nbsp;</span><span style="color: gray;">&lt;see&nbsp;cref=</span><span style="color: gray;">"AutoCompleteQueryResultProvider"</span><span style="color: gray;">&nbsp;/&gt;</span><span style="color: green;">&nbsp;class.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;/summary&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;param&nbsp;name=</span><span style="color: gray;">"getResults"</span><span style="color: gray;">&gt;</span><span style="color: green;">The&nbsp;get&nbsp;results.</span><span style="color: gray;">&lt;/param&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">public</span>&nbsp;AutoCompleteQueryResultProvider(<span style="color: #2b91af;">Func</span>&lt;<span style="color: #2b91af;">AutoCompleteQuery</span>,&nbsp;<br />                   <span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IAutoCompleteQueryResult</span>&gt;&gt;&gt;&nbsp;getResults)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">this</span>.GetResults&nbsp;=&nbsp;getResults;
&nbsp;&nbsp;&nbsp;&nbsp;}
 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">public</span>&nbsp;<span style="color: #2b91af;">Func</span>&lt;<span style="color: #2b91af;">AutoCompleteQuery</span>,&nbsp;<span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">IAutoCompleteQueryResult</span>&gt;&gt;&gt;&nbsp;GetResults&nbsp;{&nbsp;<span style="color: blue;">get</span>;&nbsp;<span style="color: blue;">private</span>&nbsp;<span style="color: blue;">set</span>;&nbsp;}
}&nbsp;</pre>
<p><br />And finally the combined text changed event stream and the query result provider:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">var</span>&nbsp;textChanged&nbsp;=&nbsp;<span style="color: blue;">this</span>.textChangedObservable
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(text&nbsp;=&gt;&nbsp;text&nbsp;!=&nbsp;<span style="color: blue;">null</span>&nbsp;&amp;&amp;&nbsp;text.Length&nbsp;&gt;=&nbsp;<span style="color: blue;">this</span>.MinimumCharacters)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.DistinctUntilChanged()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Throttle(<span style="color: #2b91af;">TimeSpan</span>.FromMilliseconds(<span style="color: blue;">this</span>.Delay));
 
<span style="color: blue;">var</span>&nbsp;getResultsFunc&nbsp;=&nbsp;<span style="color: blue;">this</span>.AutoCompleteQueryResultProvider.GetResults;
<span style="color: blue;">var</span>&nbsp;resultsObservable&nbsp;=&nbsp;<span style="color: blue;">from</span>&nbsp;searchTerm&nbsp;<span style="color: blue;">in</span>&nbsp;textChanged
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">from</span>&nbsp;suggestions&nbsp;<span style="color: blue;">in</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getResultsFunc(<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">AutoCompleteQuery</span>(searchTerm)).TakeUntil(textChanged)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">select</span>&nbsp;suggestions;&nbsp;</pre>
<p>Now we just need to subscribe to the <em>resultsObservable&nbsp;</em>and add the&nbsp;query results to our Results collection (which is the&nbsp;ItemsSource for the ListBox):</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">resultsObservable</span>.ObserveOn(<span style="color: blue;">this</span>).Subscribe(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results&nbsp;=&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">this</span>.Results.Clear();
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">foreach</span>&nbsp;(<span style="color: blue;">var</span>&nbsp;result&nbsp;<span style="color: blue;">in</span>&nbsp;results)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">this</span>.Results.Add(result);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
</pre>
<h3>Hooking the Control Up to&nbsp;a ViewModel</h3>
<p>The control is ready but it requires an external query provider to work (the AutoCompleteQueryResultProvider). This is now really easy and very view model and test-friendly. Just define a ViewModel like this:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">public</span>&nbsp;<span style="color: blue;">class</span>&nbsp;<span style="color: #2b91af;">RottenTomatoesViewModel</span>&nbsp;:&nbsp;<span style="color: #2b91af;">ViewModelBase</span>
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">public</span>&nbsp;<span style="color: #2b91af;">AutoCompleteQueryResultProvider</span>&nbsp;AutoCompleteQueryResultProvider
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">get</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">return</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">AutoCompleteQueryResultProvider</span>(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query&nbsp;=&gt;&nbsp;<span style="color: #2b91af;">Observable</span>.FromAsync(()&nbsp;=&gt;&nbsp;<span style="color: blue;">this</span>.SearchRottenTomatoes(query.Term)));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
 
 
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">private</span>&nbsp;<span style="color: blue;">async</span>&nbsp;<span style="color: #2b91af;">Task</span>&lt;<span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: #2b91af;">AutoCompleteQueryResult</span>&gt;&gt;&nbsp;SearchRottenTomatoes(<span style="color: blue;">string</span>&nbsp;term)
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">string</span>&nbsp;address&nbsp;=&nbsp;<span style="color: blue;">string</span>.Format(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #a31515;">"http://api.rottentomatoes.com/api/public/v1.0/movies.json/?apikey=</span><span style="color: mediumseagreen;">{0}</span><span style="color: #a31515;">&amp;q=</span><span style="color: mediumseagreen;">{1}</span><span style="color: #a31515;">&amp;page_limit=</span><span style="color: mediumseagreen;">{2}</span><span style="color: #a31515;">&amp;page=</span><span style="color: mediumseagreen;">{3}</span><span style="color: #a31515;">"</span>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #a31515;">"&lt;&lt;&lt;YOURAPIKEY&gt;&gt;&gt;"</span>,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;term,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1);
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">var</span>&nbsp;client&nbsp;=&nbsp;<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">HttpClient</span>(<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">LegacyJsonMediaTypeConverterDelegatingHandler</span>()&nbsp;<br />                                   {&nbsp;InnerHandler&nbsp;=&nbsp;<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">HttpClientHandler</span>()&nbsp;});
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">HttpResponseMessage</span>&nbsp;response&nbsp;=&nbsp;<span style="color: blue;">await</span>&nbsp;client.GetAsync(address);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.EnsureSuccessStatusCode();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">JObject</span>&nbsp;content&nbsp;=&nbsp;<span style="color: blue;">await</span>&nbsp;response.Content.ReadAsAsync&lt;<span style="color: #2b91af;">JObject</span>&gt;();
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">var</span>&nbsp;res&nbsp;=&nbsp;<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">AutoCompleteQueryResult</span>&gt;();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">foreach</span>&nbsp;(<span style="color: blue;">var</span>&nbsp;movieDescription&nbsp;<span style="color: blue;">in</span>&nbsp;content[<span style="color: #a31515;">"movies"</span>])
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">var</span>&nbsp;movie&nbsp;=&nbsp;<span style="color: blue;">new</span>&nbsp;<span style="color: #2b91af;">AutoCompleteQueryResult</span>();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;movie.Title&nbsp;=&nbsp;movieDescription[<span style="color: #a31515;">"title"</span>].Value&lt;<span style="color: blue;">string</span>&gt;();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;movie.Thumbnail&nbsp;=&nbsp;movieDescription[<span style="color: #a31515;">"posters"</span>][<span style="color: #a31515;">"thumbnail"</span>].Value&lt;<span style="color: blue;">string</span>&gt;();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.Add(movie);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">return</span>&nbsp;res;
&nbsp;&nbsp;&nbsp;&nbsp;}
}</pre>
<p>&nbsp;</p>
<p>Perform a DataBinding in the associated view:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">ctrls</span><span style="color: blue;">:</span><span style="color: #a31515;">AutoCompleteTextBox</span>
&nbsp;&nbsp;&nbsp;<span style="color: red;">&nbsp;Grid.Row</span><span style="color: blue;">=</span><span style="color: blue;">"1"</span>
&nbsp;&nbsp;&nbsp;<span style="color: red; background-color: #ffff00;">&nbsp;AutoCompleteQueryResultProvider</span><span style="background-color: #ffff00;"><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span></span><span style="color: red; background-color: #ffff00;">&nbsp;AutoCompleteQueryResultProvider</span><span style="background-color: #ffff00;"><span style="color: blue;">}</span><span style="color: blue;">"</span> </span>&nbsp;&nbsp;&nbsp;<span style="color: red;">&nbsp;<br /> Margin</span><span style="color: blue;">=</span><span style="color: blue;">"10"</span><span style="color: red;">&nbsp;FontSize</span><span style="color: blue;">=</span><span style="color: blue;">"20"</span><span style="color: red;">&nbsp;PopupHeight</span><span style="color: blue;">=</span><span style="color: blue;">"300"</span><span style="color: blue;">&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">ctrls</span><span style="color: blue;">:</span><span style="color: #a31515;">AutoCompleteTextBox.ItemTemplate</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">DataTemplate</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Grid</span><span style="color: red;">&nbsp;Height</span><span style="color: blue;">=</span><span style="color: blue;">"100"</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Grid.ColumnDefinitions</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">ColumnDefinition</span><span style="color: red;">&nbsp;Width</span><span style="color: blue;">=</span><span style="color: blue;">"100"</span><span style="color: blue;">&gt;&lt;/</span><span style="color: #a31515;">ColumnDefinition</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">ColumnDefinition</span><span style="color: red;">&nbsp;Width</span><span style="color: blue;">=</span><span style="color: blue;">"*"</span><span style="color: blue;">&gt;&lt;/</span><span style="color: #a31515;">ColumnDefinition</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Grid.ColumnDefinitions</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">Image</span><span style="color: red;">&nbsp;Grid.Column</span><span style="color: blue;">=</span><span style="color: blue;">"0"</span><span style="color: red;">&nbsp;Stretch</span><span style="color: blue;">=</span><span style="color: blue;">"None"</span><span style="color: red;">&nbsp;Source</span><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;">&nbsp;Thumbnail</span><span style="color: blue;">}</span><span style="color: blue;">"</span><span style="color: blue;">&gt;&lt;/</span><span style="color: #a31515;">Image</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">TextBlock</span><span style="color: red;">&nbsp;MaxWidth</span><span style="color: blue;">=</span><span style="color: blue;">"280"</span><span style="color: red;">&nbsp;Margin</span><span style="color: blue;">=</span><span style="color: blue;">"5&nbsp;0&nbsp;0&nbsp;0"</span><span style="color: red;">&nbsp;TextWrapping</span><span style="color: blue;">=</span><span style="color: blue;">"Wrap"</span>&nbsp;<br />                          <span style="color: red;">&nbsp;Grid.Column</span><span style="color: blue;">=</span><span style="color: blue;">"1"</span><span style="color: red;">&nbsp;Text</span><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;">&nbsp;Title</span><span style="color: blue;">}</span><span style="color: blue;">"</span><span style="color: blue;">&gt;&lt;/</span><span style="color: #a31515;">TextBlock</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">Grid</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">DataTemplate</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">ctrls</span><span style="color: blue;">:</span><span style="color: #a31515;">AutoCompleteTextBox.ItemTemplate</span><span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/</span><span style="color: #a31515;">ctrls</span><span style="color: blue;">:</span><span style="color: #a31515;">AutoCompleteTextBox</span><span style="color: blue;">&gt;</span></pre>
<p>&nbsp;</p>
<p>... and the result:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/0121.AutoCompleteTB.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/0121.AutoCompleteTB.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/0121.AutoCompleteTB.png" original-url="http://blogs.msdn.com/resized-image.ashx/__size/350x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/0121.AutoCompleteTB.png" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p>Compared to the solution in the first post, it is very re-usable: Changing the backend just requires a change of the provider function and all the other parts stay the same.</p>
<h3>Closing Words</h3>
<p>The control is by no means perfect. It does not use the Visual State Manager, it does not support paging nor does it support nice UI candy such as showing the matched string highlighted in the query results. A little spinner animation would also be great... And an error reporting in the UI for remote queries would be nice, too.&nbsp;For the moment I leave these features as an exercise for the happy reader :-) I think as a demo that Rx can also have a place inside a control it has accomplished its purpose.</p>
<p>Source code on <a href="https://skydrive.live.com/redir?resid=DDD7D9C98FDFC0E2!2295&amp;authkey=!ADi1kRhSSlgG6q8">SkyDrive</a>&nbsp;and <a title="GitHub" href="https://github.com/olohmann/WpfRxControls">GitHub</a>.</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>An Rx-Enabled WPF AutoComplete TextBox - Part 1/2</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/an-rx-enabled-wpf-autocomplete-textbox-part-12</link>
      <pubDate>Fri, 21 Jun 2013 05:50:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2013/06/21/an-rx-enabled-wpf-autocomplete-textbox-part-12/</guid>
      <description><![CDATA[In this blog post we will focus on the Rx aspects of developing an autocomplete feature. In the next...]]></description>
      <content:encoded><![CDATA[<p><em>In this blog post we will focus on the Rx aspects of developing an autocomplete feature. In the next blog post we are going to work on a re-usable WPF auto-complete TextBox that uses Rx under the covers.</em></p>
<p>&nbsp;Some months ago I delivered a Workshop on Reactive Extensions (Rx). If you never heard about Rx, you should read <a title="this" href="http://msdn.microsoft.com/en-us/data/gg577609.aspx">this</a> first.</p>
<p>One little exercise I did with&nbsp;the workshop&nbsp;attendees was to develop a basic&nbsp;auto-complete TextBox that would query an online dictionary service and the results would be shown in the UI.&nbsp;The&nbsp;challenge was derived from the great <a title="Rx Workshop on Channel 9" href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/This is a challenge from the great Rx Workshop on Channel 9.">Rx Workshop on Channel 9</a>.&nbsp;Naturally, the challenge does not focus on developing a re-usable control, but on the Rx aspects.</p>
<p>What we ended with was a simple UI like that used Rx to handle all events:</p>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/6204.SimpleAutoCompleteTB.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/6204.SimpleAutoCompleteTB.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/6204.SimpleAutoCompleteTB.png" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/6204.SimpleAutoCompleteTB.png" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<h3>A&nbsp;Simple Scenario but Some Interesting Challenges</h3>
<p>Even if it looks simple to implement an UI that does the job, taking a look at the details reveals some (maybe) unexpected complexity:</p>
<ul>
<li><strong>How to do asynchronous service calls?</strong> The textbox input should be fed into a service endpoint. As it is the nature of remote endpoints, they won't answer instantly. That is, if we <em>wait </em>for the response on the UI thread our UI&nbsp;will freeze.&nbsp;Not acceptable. Thus we need to perform the endpoint calls asynchronously on a background thread.</li>
<li><strong>How to we handle minimum input length?</strong> Most often it does not make sense to start querying for results when the input is below a certain threshold of characters. For a dictionary search 2 letters might make sense.</li>
<li><strong>How do we avoid calling the service endpoint for every tiny keystroke?</strong> Some users type fast. Really fast. For those users we would like to avoid to create a request for every letter they put into the textbox. Only when a certain timespan of inactivity (300ms for example) is reached, we would like to send out a query to the remote service.</li>
<li><strong>How do we avoid calling the service when there are no changes in contents? </strong>Imagine that a user pastes the same text twice into the textbox or an undo resets the text to the previous contents. In these cases we should avoid doing an extra roundtrip and simply ignore the text changed event.</li>
<li><strong>How do we avoid mixing up the query result order?</strong> When we take the asynchronous approach we have to deal with a very difficult situation. A response to a request might appear out of order. That is another request's response reaches us earlier although the original request was sent later. There is no guarantee of order for us.</li>
</ul>
<p>The good news is that&nbsp;Rx has for&nbsp;all these challenges some clever solutions.</p>
<h3>Utilizing Rx on WPF Events</h3>
<p>To transition from a classic event to an Rx Observable we can utilize the Obervable.FromEventPattern method. It allows us to create observable from the TextBox TextChanged Event:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">EventPattern</span>&lt;<span style="color: #2b91af;">TextChangedEventArgs</span>&gt;&gt;&nbsp;textChangedObservable&nbsp;=&nbsp;</pre>
<pre style="background: white; color: black; font-family: Consolas;">    <span style="color: #2b91af;">Observable</span>.FromEventPattern&lt;<span style="color: #2b91af;">TextChangedEventArgs</span>&gt;(textBox,&nbsp;<span style="color: #a31515;">"TextChanged"</span>);
</pre>
<p>Looks a bit more complex than hooking a standard event handler, but it will pay off later. Think of Rx as an IEnumerable for events.</p>
<p>With the help of System.Reactive.Linq we can now start to work on this stream of TextChanged events. The first thing I would like to apply is a selection in order to get the thing we really want to deal with from our event stream.</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: #2b91af;">IObservable</span>&lt;<span style="color: blue;">string</span>&gt;&nbsp;textChangedObservable&nbsp;=&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Observable</span>.FromEventPattern&lt;<span style="color: #2b91af;">TextChangedEventArgs</span>&gt;(textBox,&nbsp;<span style="color: #a31515;">"TextChanged"</span>)
&nbsp;&nbsp;&nbsp;&nbsp;.Select(evt&nbsp;=&gt;&nbsp;((<span style="color: #2b91af;">TextBox</span>)evt.Sender).Text);</pre>
<p>&nbsp;What we now get is a nice stream of the user input that is currently present in the text box. If I would enter the search term "react" each with an individual&nbsp;key stroke&nbsp;the data that would be pushed to the subscriber our textChangedObservable&nbsp;would&nbsp;be&nbsp;the following:</p>
<ul>
<li>r</li>
<li>re</li>
<li>rea</li>
<li>reac</li>
<li>react</li>
</ul>
<p>Nice. We get the raw input now. Before we fine tune the input handling, let us think about how we can feed this data now into our query service.</p>
<h3>Connecting to the&nbsp;Dictionary Service&nbsp;</h3>
<p>&nbsp;</p>
<p>The dictionary service is brought in as a web service. The interface (excerpt):</p>
<p><span style="color: blue;">&nbsp; public</span>&nbsp;<span style="color: #2b91af;">DictionaryWord</span>[]&nbsp;MatchInDict(<span style="color: blue;">string</span>&nbsp;dictId,&nbsp;<span style="color: blue;">string</span>&nbsp;word,&nbsp;<span style="color: blue;">string</span>&nbsp;strategy);</p>
<p>&nbsp;</p>
<p>and the async counterpart:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">public</span>&nbsp;<span style="color: #2b91af;">Task</span>&lt;<span style="color: #2b91af;">DictionaryWord</span>[]&gt;&nbsp;MatchInDictAsync(<span style="color: blue;">string</span>&nbsp;dictId,&nbsp;<span style="color: blue;">string</span>&nbsp;word,&nbsp;<span style="color: blue;">string</span>&nbsp;strategy);</pre>
<p>As you can imagine the synchronous method is nothing we are interested in. Instead we&nbsp;will utilize one of Rx Observable Factory methods that convert a Task into an Observable. An Observable that is based on a Task will produce one result and than completes (or an error):</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">var</span>&nbsp;queryResultObservable&nbsp;=&nbsp;<span style="color: #2b91af;">Observable</span>.FromAsync(<br />             ()&nbsp;=&gt;&nbsp;service.MatchInDictAsync(<span style="color: #a31515;">"wn"</span>,&nbsp;input,&nbsp;<span style="color: #a31515;">"prefix"</span>));
</pre>
<pre style="background: white; color: black; font-family: Consolas;">&nbsp;</pre>
<p>&nbsp;We make the parameters for the dictId and strategy fixed and use the input as our variable. The goal is that the input is provided from the textbox' event data stream that we created before. How to combine these? With Linq!</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">var</span>&nbsp;results&nbsp;=&nbsp;<span style="color: blue;">from</span>&nbsp;searchTerm&nbsp;<span style="color: blue;">in</span>&nbsp;textChangedObservable<br />              <span style="color: blue;">from</span>&nbsp;dictionaryServicesuggestions&nbsp;<span style="color: blue;">in</span>&nbsp;<span style="background-color: #ffff00;">????</span><br />              <span style="color: blue;">select</span>&nbsp;dictionaryServiceSuggestions;</pre>
<p><br />What basically happens here is that we get updates from the text box text changes pushed into the searchTerm variable. What we want to do with this value is create a new observable&nbsp;for receiving the&nbsp;matching result to that search term. And here is our issue. We cannot just put the queryResultObservable here instead we need a little factory that will create us a&nbsp;new observable for&nbsp;every pushed down&nbsp;searchTerm.&nbsp;Let us&nbsp;introduce a little helper func to fill the gap:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: #2b91af;">Func</span>&lt;<span style="color: blue;">string</span>,&nbsp;<span style="color: #2b91af;">IObservable</span>&lt;<span style="color: #2b91af;">DictionaryWord</span>[]&gt;&gt;&nbsp;getSuggestions&nbsp;=&nbsp;<br />    (term)&nbsp;=&gt;&nbsp;<span style="color: #2b91af;">Observable</span>.FromAsync(()&nbsp;=&gt;&nbsp;service.MatchInDictAsync(<span style="color: #a31515;">"wn"</span>,&nbsp;term,&nbsp;<span style="color: #a31515;">"prefix"</span>));
</pre>
<p>&nbsp;We insert that into our results observable:</p>
<p><span style="color: blue;">var</span>&nbsp;results&nbsp;=&nbsp;<span style="color: blue;">from</span>&nbsp;searchTerm&nbsp;<span style="color: blue;">in</span>&nbsp;textChangedObservable<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">from</span>&nbsp;dictionaryServicesuggestions&nbsp;<span style="color: blue;">in</span>&nbsp;getSuggestions(searchTerm)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">select</span>&nbsp;dictionaryServiceSuggestions;</p>
<p>Now we can start observing the results Observable for query results from the dictionary service. If we would like to put them in a WPF ListBox named <em>listBox</em> it could look like this:</p>
<pre style="background: white; color: black; font-family: Consolas;">results.ObserveOn(listBox).Subscribe(
&nbsp;&nbsp;&nbsp;&nbsp;words&nbsp;=&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listBox.Items.Clear();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">foreach</span>&nbsp;(<span style="color: blue;">var</span>&nbsp;word&nbsp;<span style="color: blue;">in</span>&nbsp;words.Select(word&nbsp;=&gt;&nbsp;word.Word).Take(10).ToArray())
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listBox.Items.Add(word);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});</pre>
<pre style="background: white; color: black; font-family: Consolas;">&nbsp;</pre>
<p>The <em>ObserveOn</em> method is an extension method that comes with the <a title="Rx-WPF" href="http://nuget.org/packages/Rx-WPF/">Rx.WPF</a>&nbsp;helpers. It will make sure that we receive the callbacks for new results on the Dispatcher thread and not on a background thread. We just take out 10 of the words in the result and add them to our listbox.</p>
<h3>Fixing Issues&nbsp;</h3>
<p>From our list of challenges we can cross out the first one: we apply asynchronicity already. That is, the UI won't be blocked at any time by calling out to the remote dictionary service. However, there four other things are not addressed at the moment. Let us start to fix the <strong>minimum required input</strong>. We do not want to call the service before at least four characters are present in the textbox. To achieve that we simply apply a <a title="Where" href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.where(v=vs.103).aspx">Where</a> constraint to the textChangedObservable:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">var</span>&nbsp;textChangedObservable&nbsp;=&nbsp;<span style="color: #2b91af;">Observable</span>
&nbsp;&nbsp;&nbsp;&nbsp;.FromEventPattern&lt;<span style="color: #2b91af;">TextChangedEventArgs</span>&gt;(textBox,&nbsp;<span style="color: #a31515;">"TextChanged"</span>)
&nbsp;&nbsp;&nbsp;&nbsp;.Select(evt&nbsp;=&gt;&nbsp;((<span style="color: #2b91af;">TextBox</span>)evt.Sender).Text)
&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #ffff00;">.Where(text&nbsp;=&gt;&nbsp;text&nbsp;!=&nbsp;<span style="color: blue;">null</span>&nbsp;&amp;&amp;&nbsp;text.Length&nbsp;&gt;=&nbsp;4)</span>;</pre>
<p>This really shows the power of Rx. But it is getting even better. Let us make sure that we do not query the service if the <strong>actual value in the textbox has not changed</strong>. Here Rx' <a title="DistinctUntilChanged" href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.distinctuntilchanged(v=vs.103).aspx">DistinctUntilChange</a> comes handy:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">var</span>&nbsp;textChangedObservable&nbsp;=&nbsp;<span style="color: #2b91af;">Observable</span>
&nbsp;&nbsp;&nbsp;&nbsp;.FromEventPattern&lt;<span style="color: #2b91af;">TextChangedEventArgs</span>&gt;(textBox,&nbsp;<span style="color: #a31515;">"TextChanged"</span>)
&nbsp;&nbsp;&nbsp;&nbsp;.Select(evt&nbsp;=&gt;&nbsp;((<span style="color: #2b91af;">TextBox</span>)evt.Sender).Text)
&nbsp;&nbsp;&nbsp;&nbsp;.Select(timestampedText&nbsp;=&gt;&nbsp;timestampedText.Value)
&nbsp;&nbsp;&nbsp;&nbsp;.Where(text&nbsp;=&gt;&nbsp;text&nbsp;!=&nbsp;<span style="color: blue;">null</span>&nbsp;&amp;&amp;&nbsp;text.Length&nbsp;&gt;=&nbsp;4)
&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #ffff00;">.DistinctUntilChanged()</span>;</pre>
<p>&nbsp;And finally let us make sure that the text changes do not trigger service calls <strong>too frequently</strong>. We apply&nbsp;Rx' <a title="Throttle" href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.throttle(v=vs.103).aspx">Throttle</a>:</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">var</span>&nbsp;textChangedObservable&nbsp;=&nbsp;<span style="color: #2b91af;">Observable</span>
&nbsp;&nbsp;&nbsp;&nbsp;.FromEventPattern&lt;<span style="color: #2b91af;">TextChangedEventArgs</span>&gt;(textBox,&nbsp;<span style="color: #a31515;">"TextChanged"</span>)
&nbsp;&nbsp;&nbsp;&nbsp;.Select(evt&nbsp;=&gt;&nbsp;((<span style="color: #2b91af;">TextBox</span>)evt.Sender).Text)
&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #ffff00;">.Throttle(<span style="color: #2b91af;">TimeSpan</span>.FromMilliseconds(200)) </span>&nbsp;&nbsp;&nbsp;&nbsp;<br />    .Select(timestampedText&nbsp;=&gt;&nbsp;timestampedText.Value)
&nbsp;&nbsp;&nbsp;&nbsp;.Where(text&nbsp;=&gt;&nbsp;text&nbsp;!=&nbsp;<span style="color: blue;">null</span>&nbsp;&amp;&amp;&nbsp;text.Length&nbsp;&gt;=&nbsp;4)
&nbsp;&nbsp;&nbsp;&nbsp;.DistinctUntilChanged();</pre>
<p>&nbsp;</p>
<p>The textChangedObservable is looking great now. The constraints help us to do diligent on resources. One last thing though - and this is the nasty one. We need to make sure that the pending request events are discarded properly once a new search term in pushed down from the textChangedObservable. Lucky us, Rx can deal with that, too by applying <a title="TakeUntil" href="http://msdn.microsoft.com/en-us/library/hh229530(v=vs.103).aspx">TakeUntil</a>. TakeUntil takes an IObservable as its parameter and will hold on to the decorated observable until the parameter observable publishes a new value. In this case we will simply hold on the&nbsp;dictionary result observable until&nbsp;a new input has been accepted:&nbsp;</p>
<pre style="background: white; color: black; font-family: Consolas;"><span style="color: blue;">var</span>&nbsp;results&nbsp;=&nbsp;<span style="color: blue;">from</span>&nbsp;searchTerm&nbsp;<span style="color: blue;">in</span>&nbsp;textChangedObservable
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">from</span>&nbsp;dictionaryServiceSuggestions&nbsp;<span style="color: blue;">in</span>&nbsp;getSuggestions(searchTerm)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #ffff00;">.TakeUntil(textChangedObservable) <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">select</span>&nbsp;dictionaryServiceSuggestions;</pre>
<h3>Summary</h3>
<p>&nbsp;All well and good but not re-usable. What I would like to strive for is something like this - a reusable TextBox control:</p>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/1616.AutoCompleteTB.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/1616.AutoCompleteTB.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/1616.AutoCompleteTB.png" original-url="http://blogs.msdn.com/resized-image.ashx/__size/250x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/1616.AutoCompleteTB.png" alt="" border="0" /></a></p>
<p>&nbsp;... one that you can easily feed with the right connections from a ViewModel and use in various scenarios. But that's a topic for the second blog post in this series. The source code so far can be downloaded from <a title="SkyDrive" href="https://skydrive.live.com/redir?resid=DDD7D9C98FDFC0E2!2293&amp;authkey=!AAyKP7tVevBG1U4">SkyDrive</a>. I added lots of debug output to the observables to ease understanding the process. The final version is availble on <a title="GitHub" href="https://github.com/olohmann/WpfRxControls">GitHub</a>. See <a title="part 2 of the blog post" href="http://blogs.msdn.com/b/argumentnullexceptionblogpost/archive/2013/06/21/an-rx-enabled-wpf-control-part-2-2.aspx">part 2 of the blog post</a> for details.</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>A Simple SelectAll Behavior for TextBoxes</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/a-simple-selectall-behavior-for-textboxes</link>
      <pubDate>Fri, 12 Apr 2013 08:09:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2013/04/12/a-simple-selectall-behavior-for-textboxes/</guid>
      <description><![CDATA[Not exactly a brand new topic in the area of WPF development are attached behaviors. However, in...]]></description>
      <content:encoded><![CDATA[<p>Not exactly a brand new topic in the area of WPF development are attached behaviors. However, in my&nbsp;opinion, they should be used more often - that's why I decided to write about it. Let us start from the beginning.</p>
<p><strong>What is an attached behavior?</strong></p>
<p>From a high level perspective attached behaviors allow you to write a snippet of code that can add additional features to a WPF control without doing something that drastic as&nbsp;inheriting&nbsp;from an existing control class. As we all know&nbsp;inheritance should not be the default reaction to adding behavior to a class. Most often better results can be achieved in adding additional functionality via composition. And this is the approach of attached behaviors.</p>
<p><strong>How do I build an attached behavior?</strong></p>
<p>Two options: Build it from scratch or use a nice and simple SDK from the Blend team. This SDK allows you to build an attached behavior in a very straight forward manner. If you have VS2012 with Update 2 installed, you should find the <em>System.Windows.Interactivity.dll</em>&nbsp;assemby per default in "<em>C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5</em>". In earlier versions it is located in the Expression Blend installation folder. Many WPF developers seem to be unaware of this little gem so I would like to advertise it a bit by showing you a brief example.</p>
<p>The out-of-the-box WPF TextBox control has no property that can be changed to trigger the complete selection of the TextBox's text when a user navigates, e.g. with the TAB key, into that TextBox. When a user is required to enter a couple of input fields on a form this feature is often requested. An initial thought might be: OK, let us create a SelectAllTextBox that derives from the TextBox class and introduces a new property to enable that <em>behavior</em>. The alternative is as follows:</p>
<ul>
<li>Declare a normal TextBox in XAML that should be decorated with additional behavior</li>
<li>Declare an external class (i.e. our <em>attached behavior class</em>) that extends the features of the TextBox control by using the TextBox instance's events and properties.</li>
<li>Finally, hook the TextBox that needs additional functionality with the attached behavior.</li>
</ul>
<p>The first step is straight forward - we just declare a TextBox control in XAML:</p>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">TextBlock</span><span style="color: blue;">&gt;</span>TextBox&nbsp;without&nbsp;behavior:<span style="color: blue;">&lt;/</span><span style="color: #a31515;">TextBlock</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">TextBox</span><span style="color: blue;">&gt;</span>this&nbsp;is&nbsp;text<span style="color: blue;">&lt;/</span><span style="color: #a31515;">TextBox</span><span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">&gt;</span></pre>
<p>The second step requires us to define a new class - our attached behavior. To get a basic infrastructure that eases our development and wires things up seamlessly we need to implement a class that derives from the abstract base class Behavior&lt;T&gt; which is defined in the Blend SDK:</p>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white;"><span style="color: blue;">using</span>&nbsp;System.Windows;
<span style="color: blue;">using</span>&nbsp;System.Windows.Controls;
<span style="color: blue;">using</span>&nbsp;System.Windows.Input;
<span style="color: blue;">using</span>&nbsp;System.Windows.Interactivity;
 
<span style="color: blue;">public</span>&nbsp;<span style="color: blue;">class</span>&nbsp;<span style="color: #2b91af;">SelectAllTextBoxBehavior</span>&nbsp;:&nbsp;<span style="color: #2b91af;">Behavior</span>&lt;<span style="color: #2b91af;">TextBox</span>&gt;
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;summary&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;Called&nbsp;after&nbsp;the&nbsp;behavior&nbsp;is&nbsp;attached&nbsp;to&nbsp;an&nbsp;AssociatedObject.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;/summary&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;remarks&gt;</span><span style="color: green;">Override&nbsp;this&nbsp;to&nbsp;hook&nbsp;up&nbsp;functionality&nbsp;to&nbsp;the&nbsp;AssociatedObject.</span><span style="color: gray;">&lt;/remarks&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">protected</span>&nbsp;<span style="color: blue;">override</span>&nbsp;<span style="color: blue;">void</span>&nbsp;OnAttached()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">base</span>.OnAttached();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;summary&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;Called&nbsp;when&nbsp;the&nbsp;behavior&nbsp;is&nbsp;being&nbsp;detached&nbsp;from&nbsp;its&nbsp;AssociatedObject,&nbsp;but&nbsp;before&nbsp;it&nbsp;has&nbsp;actually&nbsp;occurred.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;/summary&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;remarks&gt;</span><span style="color: green;">Override&nbsp;this&nbsp;to&nbsp;unhook&nbsp;functionality&nbsp;from&nbsp;the&nbsp;AssociatedObject.</span><span style="color: gray;">&lt;/remarks&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">protected</span>&nbsp;<span style="color: blue;">override</span>&nbsp;<span style="color: blue;">void</span>&nbsp;OnDetaching()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green;">//&nbsp;Recommended&nbsp;best&nbsp;practice:&nbsp;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green;">//&nbsp;Detach&nbsp;the&nbsp;registered&nbsp;event&nbsp;handler&nbsp;to&nbsp;avoid&nbsp;memory&nbsp;leaks.</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">base</span>.OnDetaching();
&nbsp;&nbsp;&nbsp;&nbsp;}
}</pre>
<p>Blend's base class provides us two hooks and - most important - a property called <em>AssociatedObject</em>. This property will contain a reference to the control that is decorated with our behavior. The OnAttached and OnDetached hooks define the life cycle where our behavior is brought into play and is removed. In our small example we are interested in getting informed about focus changes of the TextBox. Let us extend the OnAttached method accordingly:</p>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white;"><span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;summary&gt;</span>
<span style="color: gray;">///</span><span style="color: green;">&nbsp;Called&nbsp;after&nbsp;the&nbsp;behavior&nbsp;is&nbsp;attached&nbsp;to&nbsp;an&nbsp;AssociatedObject.</span>
<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;/summary&gt;</span>
<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;remarks&gt;</span><span style="color: green;">Override&nbsp;this&nbsp;to&nbsp;hook&nbsp;up&nbsp;functionality&nbsp;to&nbsp;the&nbsp;AssociatedObject.</span><span style="color: gray;">&lt;/remarks&gt;</span>
<span style="color: blue;">protected</span>&nbsp;<span style="color: blue;">override</span>&nbsp;<span style="color: blue;">void</span>&nbsp;OnAttached()
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">base</span>.OnAttached();
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">this</span>.AssociatedObject.GotFocus&nbsp;+=&nbsp;<span style="color: blue;">this</span>.OnTextBoxGotFocus;
}
</pre>
<p>We wanted to be able to select all Text as soon as the TextBox receives focus. Luckily there is a <em>SelectAll</em> method defined that we can use to select the complete text in a TextBox &nbsp;In consequence, the code inside the event handler could look like this:</p>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white;"><span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;summary&gt;</span>
<span style="color: gray;">///</span><span style="color: green;">&nbsp;Handles&nbsp;the&nbsp;GotFocus&nbsp;event&nbsp;of&nbsp;the&nbsp;TextBox&nbsp;control.</span>
<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;/summary&gt;</span>
<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;param&nbsp;name=</span><span style="color: gray;">"sender"</span><span style="color: gray;">&gt;</span><span style="color: green;">The&nbsp;source&nbsp;of&nbsp;the&nbsp;event.</span><span style="color: gray;">&lt;/param&gt;</span>
<span style="color: gray;">///</span><span style="color: green;">&nbsp;</span><span style="color: gray;">&lt;param&nbsp;name=</span><span style="color: gray;">"e"</span><span style="color: gray;">&gt;</span><span style="color: green;">The&nbsp;</span><span style="color: gray;">&lt;see&nbsp;cref=</span><span style="color: gray;">"RoutedEventArgs"</span><span style="color: gray;">&nbsp;/&gt;</span><span style="color: green;">&nbsp;instance&nbsp;containing&nbsp;the&nbsp;event&nbsp;data.</span><span style="color: gray;">&lt;/param&gt;</span>
<span style="color: blue;">private</span>&nbsp;<span style="color: blue;">void</span>&nbsp;OnTextBoxGotFocus(<span style="color: blue;">object</span>&nbsp;sender,&nbsp;<span style="color: #2b91af;">RoutedEventArgs</span>&nbsp;e)
{
<span style="color: blue;"> this</span>.AssociatedObject.SelectAll();
}</pre>
<p>&nbsp;(<strong>Important</strong>: The code so far would actually not behave completely as expected - edge cases like mouse selection would not work. The full sample code attached to this post handles that.)&nbsp;</p>
<p>To be a nice citizen we should clean up after our&nbsp;selves&nbsp;in case the behavior is dynamically removed from the targeted TextBox at runtime. Leaving a dangling event handler could introduce a memory leak. So finally we should also take care in putting some clean-up code into the OnDetaching method:</p>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white;"><span style="color: blue;">protected</span>&nbsp;<span style="color: blue;">override</span>&nbsp;<span style="color: blue;">void</span>&nbsp;OnDetaching()
{
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green;">//&nbsp;Recommended&nbsp;best&nbsp;practice:&nbsp;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green;">//&nbsp;Detach&nbsp;the&nbsp;registered&nbsp;event&nbsp;handler&nbsp;to&nbsp;avoid&nbsp;memory&nbsp;leaks.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">this</span>.AssociatedObject.GotFocus&nbsp;-=&nbsp;<span style="color: blue;">this</span>.OnTextBoxGotFocus;
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">base</span>.OnDetaching();
}</pre>
<p>&nbsp;The final step is the registration of the Behavior. We could do it in code or (most often)&nbsp;declaratively&nbsp;in XAML:</p>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white;"><span style="color: red;">[...] xmlns</span><span style="color: blue;">:</span><span style="color: red;">i</span><span style="color: blue;">=</span><span style="color: blue;">"clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" [...]</span>
</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">TextBlock</span><span style="color: blue;">&gt;</span>TextBox&nbsp;with&nbsp;behavior:<span style="color: blue;">&lt;/</span><span style="color: #a31515;">TextBlock</span><span style="color: blue;">&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">TextBox</span><span style="color: blue;">&gt;</span>this&nbsp;is&nbsp;text
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #ffff00;">&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">i</span><span style="color: blue;">:</span><span style="color: #a31515;">Interaction.Behaviors</span><span style="color: blue;">&gt;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;</span><span style="color: #a31515;">local</span><span style="color: blue;">:</span><span style="color: #a31515;">SelectAllTextBoxBehavior</span><span style="color: blue;">&gt;&lt;/</span><span style="color: #a31515;">local</span><span style="color: blue;">:</span><span style="color: #a31515;">SelectAllTextBoxBehavior</span><span style="color: blue;">&gt;</span></span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;<span style="background-color: #ffff00;">/</span></span><span style="background-color: #ffff00;"><span style="color: #a31515;">i</span><span style="color: blue;">:</span><span style="color: #a31515;">Interaction.Behaviors</span><span style="color: blue;">&gt;</span></span>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">&lt;/</span><span style="color: #a31515;">TextBox</span><span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">&gt;</span></pre>
<p>&nbsp;That's it - all is hooked up now!</p>
<p><strong>Download Source</strong></p>
<p>The complete source code can be <a title="downloaded from SkyDrive" href="http://sdrv.ms/Yvgb9R">downloaded from SkyDrive</a>.</p>
<p><strong>Want to know more about Attached Behaviors?</strong></p>
<p>Josh Smith has an exhaustive article about Attached Behaviors on&nbsp;<a title="Codeplex" href="http://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF">Codeplex</a>. &nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Attended the SAP InnoJam in Madrid</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/attended-the-sap-innojam-in-madrid</link>
      <pubDate>Wed, 14 Nov 2012 03:40:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2012/11/14/attended-the-sap-innojam-in-madrid/</guid>
      <description><![CDATA[Together with five colleagues I had the opportunity to attend the SAP InnoJam in Madrid as a...]]></description>
      <content:encoded><![CDATA[<p>Together with five colleagues I had the opportunity to attend the SAP InnoJam in Madrid as a Microsoft technical expert for developing Windows Store applications. You might ask yourself: What is the SAP InnoJam and where is the connection to Windows store apps? Let me quickly introduce the InnoJam concept and then let's discover the connection to Windows 8 apps.</p>
<p>The&nbsp;<a title="#InnoJam" href="https://twitter.com/search?q=%23innojam&amp;src=typd">#InnoJam</a> (more background <a href="http://scn.sap.com/community/events/innojam" target="_blank">here</a>) is a prequel session to SAP's TechEd and provides the possibility for attendees to work in teams (~8 members each) on innovative ideas around various scenarios. Here are two&nbsp;examples of the five challenges that were tackled at the InnoJam in Madrid:</p>
<ul>
<li>Better my journey. Teams that took this challenge thought about redesigning the service experience for commuters and city inhabitants in the context of getting around in the city. Especially the personalization aspect of this challenge was interesting.</li>
<li>Ad-hoc parking lots. Teams that took this challenge thought about redesigning the service experience for visitors of the city or big events in the context of finding free parking lots.</li>
</ul>
<p>The whole event spanned over two days (and the night in between :-)) and started with a design session where each team worked on fine tuning their ideas around their chosen scenario. During this phase innovation trainers helped the teams in the design thinking process and in developing a great story and business case around their scenario. In the late afternoon of the first day all teams had a short presentation of their project and from there they went straight into the implementation phase. A picture is worth more than 1k words (look out for the number of post-its&nbsp;and brown paper drawings at the wall - that's&nbsp;only 1/3 of the impressive output, more pictures <a href="http://teched.event-hosting.com/p79471889">here</a>):</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/2018.Innojam.jpg" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/2018.Innojam.jpg"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/2018.Innojam.jpg" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/2018.Innojam.jpg" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p>The implementation challenge encompassed the whole spectrum from backend to (mobile) front-ends. And this is were Windows 8 came into play. We brought a big bag of new and shiny Windows 8 devices with us and helped those&nbsp;teams that wanted to work on Windows 8 to develop apps for their scenarios. Especially our strengths in&nbsp;providing a modern UI and in connecting to SOAP web services, OData and REST services&nbsp;were a great fit to reach out to SAP solutions. Another cool thing that helped us a lot is our <a href="https://tfs.visualstudio.com/">TFS in the cloud</a>. You can spin up a source control in seconds - really important to parallelize the development efforts. We also received a lot of praise for the great integration of TFS in Visual Studio. Even devs that were new to our platform picked it up easily and enjoyed it a lot.</p>
<p>One of the teams that we supported throughout the event was team <a href="https://twitter.com/search?q=%23innojam%20%23parkit&amp;src=typd">#parkit</a>. They were working&nbsp;on a solution for finding ad-hoc parking lots. Together with team <a href="https://twitter.com/search?q=%23innojam%20%23parkit&amp;src=typd">#parkit</a> we built an app in roughly 18h&nbsp;that utilized Bing maps to route app users to a free parking lot by connecting to&nbsp;their&nbsp;SAP ByDesign web service that was build up in parallel and provided information about parking slot positions and their availability. The app featured additional&nbsp;customizations&nbsp;for the user such as setting weights for preferring parking slots with less costs or at a closer distance.&nbsp;To give you an impression this screenshot shows the hub screen of the app:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/7563.FreeParking.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/7563.FreeParking.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/7563.FreeParking.png" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/7563.FreeParking.png" alt="" border="0" /></a><br /><br /></p>
<p>A big kudos to the <a href="https://twitter.com/search?q=%23innojam%20%23parkit&amp;src=typd">#parkit</a> team - they rocked and became one of the two winner teams at the InnoJam. And to top all this, they placed 3rd in the overall DemoJam of the TechEd. Great job guys! You can watch a recording here: <a href="http://www.sapvirtualevents.com/teched/sessiondetails.aspx?sId=3842">http://www.sapvirtualevents.com/teched/sessiondetails.aspx?sId=3842</a>.</p>
<p>From what I learned and saw at the InnoJam I can only recommend everybody who is working on SAP solutions to visit one if you have the opportunity to do so. You will find a diverse crowd of cool people with a lot of drive and different backgrounds. Project managers, technical experts, devs&nbsp;and even sales&nbsp;people - all&nbsp;locked up in a room with lots of caffeine, good food and (late at night) beer :-) What could you wish more?</p>]]></content:encoded>
    </item>
    <item>
      <title>More Windows 8 Events in Germany</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/more-windows-8-events-in-germany</link>
      <pubDate>Tue, 20 Mar 2012 01:26:21 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2012/03/20/more-windows-8-events-in-germany/</guid>
      <description><![CDATA[As promised here is an update on the events for Windows 8 Metro app development in Germany. The team...]]></description>
      <content:encoded><![CDATA[<p>As promised here is an update on the events for Windows 8 Metro app development in Germany. The team around <a href="http://www.microsoft.com/germany/msdn/pingme/OliverScheffert/">Oliver Scheffert</a> has put up an <a href="http://www.microsoft.com/germany/msdn/events/windows8/default.mspx">MSDN site</a> that explains the <a href="http://www.microsoft.com/germany/msdn/events/windows8/Support.mspx">process</a> how to get your app in the Windows store before the final version of Windows is released. What a great opportunity for all app developers out there! Imagine having one of the first apps in the store...</p>
<p>The development events are vast: <a href="http://www.microsoft.com/germany/msdn/events/windows8/Introduction.mspx">introduction events</a>, <a href="http://www.microsoft.com/germany/msdn/events/windows8/Training.mspx">more in-depth events </a>and even <a href="http://www.microsoft.com/germany/msdn/events/windows8/Training.mspx">Hackathons</a>. </p>
<p>By the way - similar programs are also running outside of Germany. Just get in contact with your local Microsoft Developer Platform Evangelist - I am sure that they can help you.</p>]]></content:encoded>
    </item>
    <item>
      <title>CeBIT 2012 and Windows 8</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/cebit-2012-and-windows-8</link>
      <pubDate>Mon, 12 Mar 2012 04:42:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2012/03/12/cebit-2012-and-windows-8/</guid>
      <description><![CDATA[A colleague from the Developer and Platform Evangelism (DPE) division invited me to one of their...]]></description>
      <content:encoded><![CDATA[<p>A colleague from the Developer and Platform Evangelism (DPE) division invited me to one of their Windows 8 developer sessions at the CeBIT 2012. There, all the developers and IT professionals in the audience received a 2 hour overview about Windows 8, metro style design and metro app development. In addition to the talks DPE held a Q&amp;A session which was very interesting to follow. I took two observations from my visit:</p>
<ul>
<li>The vast majority of developers appreciates the possibilities that Windows 8 provides. There is a huge awareness that a completely new market becomes available and the reach of the platform is growing from Desktop to mobile (slate/pad) devices.</li>
<li>Obviously, there are many questions around WinRT, application architecture, metro style design and also a few around hardware extensibility or custom driver support.</li>
</ul>
<p><br />The first observation makes me happy since this will be one important driver for getting a lot of high quality apps into our market place. The second observation underlines how important these sessions are to clarify misunderstandings and also to receive first-hand feedback.</p>
<p><strong>Hint:</strong> A very good resource on getting started with metro style app design is <a href="http://design.windows.com">http://design.windows.com</a>. It does not solely discuss pixels or grids but also the philosophy of information architecture that will be central to most Metro apps.</p>
<p><br />In the next weeks and months DPE will host more sessions on Windows 8 in various places. <a href="http://www.microsoft.com/germany/msdn/techtalk/EntwickelnFuerWindows8.mspx">In Germany there will be sessions in Karlsruhe, Munich, Hamburg, Cologne and Berlin</a>. Unfortunately, all sessions are already fully booked. But there is a waiting list! And as soon as I receive information on more events I&rsquo;ll post it!</p>
<p>Apart from the Windows 8 sessions in the Microsoft &ldquo;Entwickler-Forum&rdquo; (developer panel) in exhibition hall 8, visiting the main stand in hall 4 was really impressive. Not only in terms of size but also in terms of the crowd that gathered around the stage:<br /><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/7571.CeBIT2012-MS-Hall4.jpg" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/7571.CeBIT2012_2D00_MS_2D00_Hall4.jpg"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/51/62/7571.CeBIT2012-MS-Hall4.jpg" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-51-62/7571.CeBIT2012_2D00_MS_2D00_Hall4.jpg" alt="" border="0" /></a></p>
<p>Due to my schedule I wasn&rsquo;t able to attend most of the talks at the center stage but a personal highlight was definitely the AntMe session. I am a big fan of getting kids early into programming so if you don&rsquo;t know it and your kids are interested in the area - go and give it a <a href="http://www.antme.net/">try</a>!</p>]]></content:encoded>
    </item>
    <item>
      <title>Hello World</title>
      <link>https://docs.microsoft.com/archive/blogs/argumentnullexceptionblogpost/hello-world</link>
      <pubDate>Mon, 12 Mar 2012 04:29:00 GMT</pubDate>
      <dc:creator><![CDATA[Oliver Lohmann - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2012/03/12/hello-world/</guid>
      <description><![CDATA[This is my first MSDN blog post and my first blog post as an MS employee. I joined Microsoft in...]]></description>
      <content:encoded><![CDATA[<p>This is my first MSDN blog post and my first blog post as an MS employee. I joined Microsoft in February as Premier Field Engineer (PFE). The question I got most often from former colleagues, friends and family is &ldquo;What is a PFE and what do you do as a PFE?&rdquo;. Good question! Here is my elevator pitch:</p>
<p>As a PFE I am part of the strategy, consulting, and support division of Microsoft and thus I am working directly with our customers.The <a title="http://www.microsoft.com/microsoftservices/en/us/premier_support.aspx" href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/Premier support">Premier support</a> is a special support contract that offers a 24x7 subject-matter-expert support to our customers. Premier service is further structured into areas like Premier Support for Proactive Services or Premier Support for Global and Enterprise Customers. My area is the Premier Support for Developers which offers our customers services around software engineering and software development, for example on-site workshops for specific Microsoft products or frameworks, troubleshooting and developer coaching.</p>
<p>What contents will you find in this blog? It is hard to predict the future but plans are that I&rsquo;ll talk a lot about software engineering, .NET and Windows 8 Metro development. Enjoy your stay!<br /><br /></p>]]></content:encoded>
    </item>
  </channel>
</rss>