<?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>Sander van Vliet &#187; WPF</title>
	<atom:link href="http://barad-dur.nl/tag/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://barad-dur.nl</link>
	<description>Land Rovers, .Net en meer!</description>
	<lastBuildDate>Wed, 21 Apr 2010 09:47:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>TwitCast</title>
		<link>http://barad-dur.nl/net/twitcast/</link>
		<comments>http://barad-dur.nl/net/twitcast/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 16:28:22 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[LINQ To Twitter]]></category>
		<category><![CDATA[TwitCast]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://barad-dur.nl/?p=147</guid>
		<description><![CDATA[What started it Are you using Twitter? Do you like it or absolutely loathe it? I do like it but one thing I don’t like is constantly checking twitter.com to see if someone tweeted something interesting. So a new idea was born and the wasting of time began. The idea Actually it’s pretty simple, I <a href="http://barad-dur.nl/net/twitcast/" class="more-link">Meer &#62;</a>]]></description>
			<content:encoded><![CDATA[<h3>What started it</h3>
<p>Are you using Twitter? Do you  like it or absolutely loathe it? I do like it but one thing I don’t like is  constantly checking twitter.com to see if someone tweeted something interesting. So  a new idea was born and the wasting of time began.</p>
<h3>The idea</h3>
<p>Actually it’s pretty simple, I  wanted an application that showed a pop-up of some kind that shows me the latest  tweet for a minute or so and then disappear, similar to the new e-mail popup  of Outlook. Another feature I wanted to make is that when I mouse over on  the pop-up the app shows me the last 10 tweets from my timeline.</p>
<h3>Problem #1</h3>
<p>First things first, how to get  the last tweets from Twitter? As all programmers are lazy (see <a href="http://blogoscoped.com/archive/2005-08-24-n14.html">http://blogoscoped.com/archive/2005-08-24-n14.html</a>) I’ve started looking for a library that could do this  for me. Lo’ and behold: <a title="LINQ To Twitter" href="http://linqtotwitter.codeplex.com/" target="_blank">LINQ To Twitter</a>. A wrapper around the Twitter web services written by <a title="Joe Mayo's blog" href="http://geekswithblogs.net/WinAZ/Default.aspx" target="_blank">Joe Mayo</a> (Microsoft  MVP) in C#.</p>
<p>So, problem #1 was solved.</p>
<h3>Problem #2</h3>
<p>The next problem was one of UI.  How to display a nice pop-up that gives access to the rest of the application?  What I wanted to do is create a window that slides in from the top of the  screen when a new Tweet is posted and when the mouse is over it, expand to show the  last 10 tweets. Because a default window border doesn’t look all that nice I’ve  decided to use a transparent window:</p>
<table style="height: 316px;" border="0" cellspacing="0" cellpadding="0" width="700">
<tbody>
<tr>
<td width="307" valign="top"><a href="http://barad-dur.nl/wp-content/uploads/2010/03/twitcast-screen.png" rel="lightbox[147]"><img class="alignnone size-full wp-image-148" title="twitcast-screen" src="http://barad-dur.nl/wp-content/uploads/2010/03/twitcast-screen.png" alt="" width="304" height="191" /></a></td>
<td width="307" valign="top"><strong>XAML:</strong></p>
<pre>&lt;Window

 Background="Transparent"
 AllowsTransparency="True"&gt;
 &lt;Grid&gt;
  &lt;Border&gt;
   &lt;ScrollViewer&gt;
    &lt;ItemsControl /&gt;
   &lt;/ScrollViewer&gt;
  &lt;/Border&gt;
 &lt;/Grid&gt;
&lt;/Window&gt;
</pre>
</td>
</tr>
</tbody>
</table>
<p>As you can see it’s a pretty  simple UI but that’s exactly what I wanted. What you see is a transparent Window with a Border and ScrollViewer (the green bit) that hosts an ItemsControl,  nothing fancy so far. The next step was to set the initial position of the  window. A simple RenderTransform took care of that:</p>
<p><strong>XAML:</strong></p>
<pre>&lt;Border.RenderTransform&gt;
&lt;TranslateTransform Y="-360"  /&gt;
&lt;/Border.RenderTransform&gt;
</pre>
<p>The ItemTemplate of the tweets  is also quite straightforward:</p>
<p><strong>XAML:</strong></p>
<pre>&lt;DataTemplate&gt;
  &lt;Border&gt;
    &lt;StackPanel Orientation="Vertical"&gt;
      &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Left"&gt;
        &lt;TextBlock Text="{Binding Path=User.Name}" /&gt;
        &lt;TextBlock Text=" at " /&gt;
        &lt;TextBlock Text="{Binding Path=CreatedAt}" /&gt;
      &lt;/StackPanel&gt;
      &lt;TextBlock Text="{Binding Path=Text}" TextWrapping="Wrap" /&gt;
    &lt;/StackPanel&gt;
  &lt;/Border&gt;
&lt;/DataTemplate&gt;
</pre>
<p>The result looks like this:</p>
<p><a href="http://barad-dur.nl/wp-content/uploads/2010/03/image002.jpg" rel="lightbox[147]"><img class="alignnone size-full wp-image-152" title="image002" src="http://barad-dur.nl/wp-content/uploads/2010/03/image002.jpg" alt="" width="375" height="66" /></a></p>
<h3>Fancy UI</h3>
<p>The important part of the  application was to “popup” (or down actually) when a new tweet is available and when the  mouse enters the application so all tweets are shown. To do this three  storyboards are needed:</p>
<p>1.       Animate to the new tweet view</p>
<p>2.       Animate to the full view</p>
<p>3.        Animate to the hidden view</p>
<p>The storyboards all animate the RenderTransform of the Border that hosts the controls.</p>
<p><strong>XAML:</strong></p>
<pre>&lt;Storyboard  x:Key="Hidden"&gt;
  &lt;DoubleAnimation
    Storyboard.TargetProperty="Opacity"
    To="0.4"
    Duration="0:0:0.2" /&gt;
  &lt;DoubleAnimation Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
    To="-370"
    Duration="0:0:0.2"
    BeginTime="0:0:0.2" /&gt;
&lt;/Storyboard&gt;
&lt;Storyboard x:Key="NewTweet"&gt;
  &lt;DoubleAnimation Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
    To="-295"
    Duration="0:0:0.2" /&gt;
   &lt;DoubleAnimation
    Storyboard.TargetProperty="Opacity"
    To="1"
    Duration="0:0:0.2" /&gt;
&lt;/Storyboard&gt;
&lt;Storyboard x:Key"AllTweets"&gt;
  &lt;DoubleAnimation Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
    To="-20"
    Duration="0:0:0.2" /&gt;
  &lt;DoubleAnimation
    Storyboard.TargetProperty="Opacity"
    To="1"
    Duration="0:0:0.2" /&gt;
&lt;/Storyboard&gt;
</pre>
<p>These storyboards are started  by various events in the application. For example: MouseEnter and MouseLeave on the  Border trigger the AllTweets and Hidden Storyboards.</p>
<h3>Timeline</h3>
<p>So far so good, the UI is  basically finished and all that remains is to fill the application with the tweets  from my timeline. For this I’ve created a Timeline class that contains the TwitterContext and a simple timer for the refresh action. The grunt of  this class is the following LINQ statement:</p>
<p><strong>LINQ:</strong></p>
<pre>var statusTweets =
from tweet in  this.context.Status
where tweet.Type == statusType
            &amp;&amp; tweet.ScreenName == this.screenName
            &amp;&amp; tweet.Count == maximumTweetsPerRefresh
            &amp;&amp; tweet.CreatedAt &gt; lastTweetDate
select tweet;
</pre>
<p>This statement will retrieve all tweets from the timeline specified with  screenName that have been created  since the last refresh.  If this results in 1 or more tweets the Timeline class will raise the NewTweetsAvailable event to notify the application that new tweets are  available to display.</p>
<p>The event handler simply pushes the new tweets to the ItemsControl and starts the NewTweet storyboard to  pop-down the window so the latest tweet is visible.</p>
<h3>Wrap up</h3>
<p>As you can see it’s really  simple to build an application that shows the latest tweets in your Twitter timeline. Of  course there are a few features that are missing, for example the storyboards,  but I’ll blog about them a bit later. Be sure to check <a title="TwitCast on Twitter" href="http://twitter.com/#search?q=%23TwitCast" target="_blank">#TwitCast</a> for updates</p>
<p><strong>Update:</strong></p>
<p>TwitCast is now listed on Codeplex at <a title="TwitCast CodePlex project page" href="http://twitcast.codeplex.com/" target="_blank">http://twitcast.codeplex.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://barad-dur.nl/net/twitcast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binding a WPF DataGridColumn to a LINQ result</title>
		<link>http://barad-dur.nl/net/binding-a-wpf-datagridcolumn-to-a-linq-result/</link>
		<comments>http://barad-dur.nl/net/binding-a-wpf-datagridcolumn-to-a-linq-result/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 14:42:49 +0000</pubDate>
		<dc:creator>Sander</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://barad-dur.nl/?p=117</guid>
		<description><![CDATA[I&#8217;ve been tinkering with Visual Studio 2010 Beta1 and the .Net 4 framework. One of the things that I&#8217;ve tried is filling a DataGrid with the results of a LINQ query. Consider the following code: C# var voyages = from voyage in entities.Voyage select new { Number = voyage.Number, ETA = voyage.StartTime }; dataGridTest.ItemsSource = <a href="http://barad-dur.nl/net/binding-a-wpf-datagridcolumn-to-a-linq-result/" class="more-link">Meer &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been tinkering with Visual Studio 2010 Beta1 and the .Net 4 framework. One of the things that I&#8217;ve tried is filling a DataGrid with the results of a LINQ query.<br />
Consider the following code:<br />
<strong>C#</strong></p>
<pre>var voyages = from voyage in entities.Voyage
              select new { Number = voyage.Number, ETA = voyage.StartTime };

dataGridTest.ItemsSource = voyages;</pre>
<p><strong>XAML</strong></p>
<pre>&lt;DataGrid Name="dataGridTest"&gt;
    &lt;DataGrid.Columns&gt;
        &lt;DataGridTextColumn Header="Number" Binding="{Binding Path=Number}" /&gt;
        &lt;DataGridTextColumn Header="ETA" Binding="{Binding Path=ETA}" /&gt;
    &lt;/DataGrid.Columns&gt;
&lt;/DataGrid&gt;</pre>
<p>Seems straightforward enough, doesn&#8217;t it? Well it does but when you try to run it, it&#8217;ll crash and burn with the following exception:</p>
<blockquote><p><strong>InvalidOperationException:</strong> A TwoWay or OneWayToSource binding cannot work on the read-only property &#8216;Number&#8217;</p></blockquote>
<p>The reason that the InvalidOperationException gets thrown is that the Binding used to bind the column to a property is a TwoWay binding, <strong>by default!</strong></p>
<p>But why should (and is) this be a problem? Well, because the result from the LINQ is a new class that contains only get-properties. This is by design because an anonymous type can only contain get-only properties as is written on <a title="MSDN - Anonymous Types" href="http://msdn.microsoft.com/en-us/library/bb397696.aspx" target="_blank">MSDN</a>:</p>
<blockquote><p>Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type.</p></blockquote>
<p>There are two workarounds for this problem, one is simply to add <code>Mode=OneWay</code> to the Binding property of the DataGridTextColumn. This will tell the DataGrid (ultimately the Binding) that it should not attempt to write data back to the bound object.</p>
<p>The other workaround is not to use an anonymous type. If you need to be able to change data in the result set this is probably the way to go. By defining a type that contains the properties you want to bind on you can tell LINQ to return that specific type and not an anonymous one.</p>
<p>For example:</p>
<p><strong>C#</strong></p>
<pre>public class VoyageResult {
    public int Number { get; set; }
    public DateTime ETA { get; set; }
}
var voyages = from voyage in entities.Voyage
              select new VoyageResult()
              {
                   Number = voyage.Number,
                   ETA = voyage.StartTime
              };</pre>
<p>voyages will now contain a list of VoyageResults instead of a generated anonymous type (with exactly the same properties!) If you bind this list to the DataGrid it will use the default TwoWay binding without complaints.</p>
]]></content:encoded>
			<wfw:commentRss>http://barad-dur.nl/net/binding-a-wpf-datagridcolumn-to-a-linq-result/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
