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

<channel>
	<title>Bryan&#039;s Coding Corner &#187; Code Samples</title>
	<atom:link href="http://bryanmitchellanderson.com/category/code-samples/feed/" rel="self" type="application/rss+xml" />
	<link>http://bryanmitchellanderson.com</link>
	<description>Chronicling my advancement as a software developer</description>
	<lastBuildDate>Thu, 26 Aug 2010 01:00:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Rx &#8211; Calling a long running function asynchronously</title>
		<link>http://bryanmitchellanderson.com/2010/08/rx-calling-a-long-running-function-asynchronously/</link>
		<comments>http://bryanmitchellanderson.com/2010/08/rx-calling-a-long-running-function-asynchronously/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 01:00:54 +0000</pubDate>
		<dc:creator>Bryan Anderson</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Rx]]></category>

		<guid isPermaLink="false">http://bryanmitchellanderson.com/?p=224</guid>
		<description><![CDATA[Today we're going to call a long running function asynchronously and handle the return on the Dispatcher thread almost as simply as calling the function directly.]]></description>
			<content:encoded><![CDATA[<p>Time for a little more Rx love. Today we&#8217;re going to call a long running function asynchronously and handle the return on the Dispatcher thread almost as simply as calling the function directly.</p>
<pre class="brush: csharp;">void CallSomeLongFunctionAsync()
{
 Observable
 .ToAsync&lt;int&gt;(LifeTheUniverseEverything)()
 .ObserveOnDispatcher()
 .Subscribe(theAnswer =&gt; MessageBox.Show(string.Format(&quot;The answer was {0}. What was the question anyway?&quot;, theAnswer)));
}

private int LifeTheUniverseEverything()
{
 Thread.Sleep(600000);
 return 42;
}</pre>
<p>I may have used my new-found powers to calculate the answer to life, the universe, and everything but imagine a service call or long running file operation. Making those UIs completely non-blocking just got a lot easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanmitchellanderson.com/2010/08/rx-calling-a-long-running-function-asynchronously/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rx &#8211; Running a function when the user stops typing</title>
		<link>http://bryanmitchellanderson.com/2010/08/rx-running-a-function-when-the-user-stops-typing/</link>
		<comments>http://bryanmitchellanderson.com/2010/08/rx-running-a-function-when-the-user-stops-typing/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 03:22:21 +0000</pubDate>
		<dc:creator>Bryan Anderson</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Rx]]></category>

		<guid isPermaLink="false">http://bryanmitchellanderson.com/?p=211</guid>
		<description><![CDATA[One common task when creating UIs is to call some function, like a filter or search, when the user stops typing. Anyone who has set this up before knows how painful it is but with Rx it's dead simple.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing with <a title="Rx Devlabs Site" href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" target="_blank">Rx (Reactive Extensions)</a> quite a bit lately and I have to say it&#8217;s one of the most powerful features added to .Net since LINQ. That probably isn&#8217;t too surprising since a lot of people call Rx &#8220;LINQ to Events&#8221;.</p>
<p>One common task when creating UIs is to call some function, like a filter or search, when the user stops typing. Anyone who has set this up before knows how painful it is but with Rx it&#8217;s dead simple.</p>
<pre class="brush: csharp;">private IDisposable textBoxObserver;

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    textBoxObserver =
        Observable
        .FromEvent&lt;TextChangedEventArgs&gt;(SomeTextBox, &quot;TextChanged&quot;)
        .Throttle(TimeSpan.FromMilliseconds(500))
        .ObserveOnDispatcher()
        .Subscribe(HandleSomeTextBoxTextChanged);
}

void MainWindow_Unloaded(object sender, RoutedEventArgs e)
{
    textBoxObserver.Dispose();
}

private void HandleSomeTextBoxTextChanged(IEvent&lt;TextChangedEventArgs&gt; args)
{
    var tb = ((TextBox)args.Sender);
    var text = tb.Text;

    SomeFunctionYouWantToCallWhenTheUserStopsTyping(text);
}</pre>
<p>There are a lot of other really cool uses for Rx so I&#8217;ll be posting more soon. Until then you should check out <a title="101 Rx Samples" href="http://rxwiki.wikidot.com/101samples" target="_blank">101 Rx Samples</a> or this <a href="http://jasonrowe.com/2010/04/02/silverlight-drag-and-drop-with-rx/" target="_blank">simple implementation</a> of drag &amp; drop in Silverlight.</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanmitchellanderson.com/2010/08/rx-running-a-function-when-the-user-stops-typing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use a Grid as an ItemsHost</title>
		<link>http://bryanmitchellanderson.com/2009/04/how-to-use-a-grid-as-an-itemshost/</link>
		<comments>http://bryanmitchellanderson.com/2009/04/how-to-use-a-grid-as-an-itemshost/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 03:18:44 +0000</pubDate>
		<dc:creator>Bryan Anderson</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Grid]]></category>
		<category><![CDATA[ItemsHost]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[xaml]]></category>

		<guid isPermaLink="false">http://bryanmitchellanderson.com/?p=55</guid>
		<description><![CDATA[You know how sometimes you run into problems that you know should be really simple but you just can&#8217;t manage to solve them? Well one of those problems for me was making a grid an ItemsHost and actually having it work. Sure you can always set IsItemsHost=&#8221;True&#8221; but you&#8217;ll just end up with every item [...]]]></description>
			<content:encoded><![CDATA[<p>You know how sometimes you run into problems that you know should be really simple but you just can&#8217;t manage to solve them? Well one of those problems for me was making a grid an ItemsHost and actually having it work. Sure you can always set IsItemsHost=&#8221;True&#8221; but you&#8217;ll just end up with every item in your ItemsSource all stacked on top of each other. A few weeks ago I ran into this same problem again and couldn&#8217;t find a way around it. So after beating my head against the wall for a few hours I finally gave up and <a href="http://stackoverflow.com/questions/691379/" target="_blank">posted on StackOverflow</a>, within a half hour I had the answer I&#8217;d been trying to figure out for the better part of a day.</p>
<p>It turns out that setting IsItemsHost to True on a panel creates the required ContentPresenters and nothing you can do will let you get at them directly (believe me, I tried). The solution is to create a keyless style that targets ContentPresenter and let this style take care of the dirty work.</p>
<p>Without furthur ado, here&#8217;s an example.</p>
<p>The Data</p>
<pre class="brush: vb;">Class DataPoint
	Private myRow As Integer
	Public Property Row() As Integer
		Get
			Return myRow
		End Get
		Set(ByVal value As Integer)
			myRow = value
		End Set
	End Property

	Private myCol As Integer
	Public Property Col() As Integer
		Get
			Return myCol
		End Get
		Set(ByVal value As Integer)
			myCol = value
		End Set
	End Property

	Private myText As String
	Public ReadOnly Property Text() As String
		Get
			Return myText
		End Get
	End Property

	Public Sub New(ByVal x As Integer, ByVal y As Integer)
		Me.Row = y
		Me.Col = x
		myText = &quot;Column &quot; + x.ToString + &quot;, Row &quot; + y.ToString
	End Sub
End Class</pre>
<p>Load the test data and set it as the data context for the window</p>
<pre class="brush: vb;">Class GridAsItemsHostExample
	Private myTestData As TestData

	Public Sub New()
		InitializeComponent()

		myTestData = New TestData()
		Me.DataContext = myTestData
	End Sub
End Class

Class TestData
	Private myDataSet As List(Of DataPoint)
	Public Property DataSet() As List(Of DataPoint)
		Get
			Return myDataSet
		End Get
		Set(ByVal value As List(Of DataPoint))
			myDataSet = value
		End Set
	End Property

	Public Sub New()
		Me.DataSet = New List(Of DataPoint)
		For x As Integer = 0 To 1
			For y As Integer = 0 To 1
				Me.DataSet.Add(New DataPoint(x, y))
			Next
		Next
	End Sub
End Class</pre>
<p>And finally the window, note the ContentPresenter style.</p>
<pre class="brush: xml;">&lt;Window x:Class=&quot;GridAsItemsHostExample&quot;
		xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
		xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
		Title=&quot;Grid As ItemsHost Example&quot;
		SizeToContent=&quot;WidthAndHeight&quot;&gt;

	&lt;ItemsControl ItemsSource=&quot;{Binding DataSet}&quot;&gt;
		&lt;ItemsControl.ItemContainerStyle&gt;
			&lt;Style TargetType=&quot;ContentPresenter&quot;&gt;
				&lt;Setter Property=&quot;Grid.Column&quot;	Value=&quot;{Binding Col}&quot;	/&gt;
				&lt;Setter Property=&quot;Grid.Row&quot;		Value=&quot;{Binding Row}&quot;	/&gt;
				&lt;Setter Property=&quot;Margin&quot;		Value=&quot;5&quot;				/&gt;
			&lt;/Style&gt;
		&lt;/ItemsControl.ItemContainerStyle&gt;
		&lt;ItemsControl.ItemTemplate&gt;
			&lt;DataTemplate&gt;
				&lt;TextBlock Text=&quot;{Binding Text}&quot;   /&gt;
			&lt;/DataTemplate&gt;
		&lt;/ItemsControl.ItemTemplate&gt;
		&lt;ItemsControl.Style&gt;
			&lt;Style TargetType=&quot;{x:Type ItemsControl}&quot;&gt;
				&lt;Setter Property=&quot;Template&quot;&gt;
					&lt;Setter.Value&gt;
						&lt;ControlTemplate TargetType=&quot;{x:Type ItemsControl}&quot;&gt;
							&lt;Grid HorizontalAlignment=&quot;Stretch&quot;
								  IsItemsHost=&quot;True&quot;
								  ShowGridLines=&quot;True&quot;&gt;
								&lt;Grid.ColumnDefinitions&gt;
									&lt;ColumnDefinition /&gt;
									&lt;ColumnDefinition /&gt;
								&lt;/Grid.ColumnDefinitions&gt;
								&lt;Grid.RowDefinitions&gt;
									&lt;RowDefinition /&gt;
									&lt;RowDefinition /&gt;
								&lt;/Grid.RowDefinitions&gt;
							&lt;/Grid&gt;
						&lt;/ControlTemplate&gt;
					&lt;/Setter.Value&gt;
				&lt;/Setter&gt;
			&lt;/Style&gt;
		&lt;/ItemsControl.Style&gt;
	&lt;/ItemsControl&gt;
&lt;/Window&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://bryanmitchellanderson.com/2009/04/how-to-use-a-grid-as-an-itemshost/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
