<?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>Tigraine &#187; WPF</title>
	<atom:link href="http://www.tigraine.at/category/programmierung/net/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tigraine.at</link>
	<description>Daniel Hoelbling talks about programming</description>
	<lastBuildDate>Fri, 03 Feb 2012 00:02:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Displaying different screens with ContentPresenters in Silverlight</title>
		<link>http://www.tigraine.at/2010/11/22/displaying-different-screens-with-contentpresenters-silverlight/</link>
		<comments>http://www.tigraine.at/2010/11/22/displaying-different-screens-with-contentpresenters-silverlight/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 09:45:01 +0000</pubDate>
		<dc:creator>Daniel Hölbling</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.tigraine.at/?p=972</guid>
		<description><![CDATA[In WPF you can use a ContentPresenter with DataTemplate to display different Views for different ViewModels. And although writing those DataTemplates gets old pretty quickly (and you resort to frameworks or roll your own code to alleviate that problem), I do like to use them in small demo apps at times. Silverlight does not support [...]]]></description>
			<content:encoded><![CDATA[<p>In <span class="caps">WPF</span> you can use a <code>ContentPresenter</code> with <code>DataTemplate</code> to display different Views for different ViewModels. And although writing those DataTemplates gets old pretty quickly (and you resort to frameworks or roll your own code to alleviate that problem), I do like to use them in small demo apps at times.</p>
<p>Silverlight does not support DataTemplates with types on them, so you can&#8217;t use ContentPresenters like you would in <span class="caps">WPF</span>.</p>
<p>But in this case it helps to understand what a DataTemplate is doing in <span class="caps">WPF</span> to easily replicate that behavior in Silverlight. DataTemplates (in this case) are little more than Converters that take objects and supply a template that is then used to display that object to the screen.</p>
<p>Once you know that, it gets rather simple: Simply implement <code>IValueConverter</code> and return a Silverlight UserControl in the <code>Convert</code> method.</p>
<p>You can even write something as simple as this:</p>
<div>
<pre name="code" class="csharp">
public class ViewConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.GetType() == typeof(NewCustomerViewModel))
        {
            return new NewCustomer();
        }
        if (value.GetType() == typeof(CustomerListViewModel))
        {
            return new MasterDetail();
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
</pre>
</div>
<p>Once your Converter can translate ViewModels (or any object) into Views you can then bind your ContentPresenter to use that Converter:</p>
<div>
<pre name="code" class="csharp">

&lt;ContentPresenter Content="{Binding ActiveScreen, Converter={StaticResource ViewConverter}}" /&gt;
</pre>
</div>
<p>You would obviously not want to use the code above, but rather hook your ViewConverter into your Inversion of Control container or something similar (or just use Caliburn.Micro).</p>
<p>There are a number of UI frameworks out there that make writing complex <span class="caps">MVVM</span> applications a lot easier. Caliburn, Caliburn.Micro and Prism. I tried some of them and I really liked them, but I found that you should really learn how Silverlight/<span class="caps">WPF</span> works before you resort to these Frameworks, as most of their features only start making sense once you start understanding the problems those frameworks set out to solve.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tigraine.at/2010/11/22/displaying-different-screens-with-contentpresenters-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Theming controls in WPF</title>
		<link>http://www.tigraine.at/2010/10/20/theming-controls-in-wpf/</link>
		<comments>http://www.tigraine.at/2010/10/20/theming-controls-in-wpf/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 09:29:27 +0000</pubDate>
		<dc:creator>Daniel Hölbling</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.tigraine.at/?p=970</guid>
		<description><![CDATA[My biggest grief with WPF is the way how twisted XAML works. It takes a lot of time getting used to it, and it&#8217;s by no means obvious how to do stuff. So this is the story of how to change the appearance of a control from some common resource for controls. Styles and Setters [...]]]></description>
			<content:encoded><![CDATA[<p>My biggest grief with <span class="caps">WPF</span> is the way how twisted <span class="caps">XAML</span> works. It takes a lot of time getting used to it, and it&#8217;s by no means obvious how to do stuff.</p>
<p>So this is the story of how to change the appearance of a control from some common resource for controls.</p>
<h3>Styles and Setters</h3>
<p>Most of the things you&#8217;d consider styling (as known from <span class="caps">CSS</span>) can be done via Styles that contain setters.</p>
<p>Inside a <code>ResourceDictionary</code> you can define styles for all controls of a type, or have them be applicable by a key. This is pretty similar to <span class="caps">CSS</span> where you can apply settings either via class (by key in <span class="caps">WPF</span>) or by elementType (TargetType in <span class="caps">WPF</span>)</p>
<p>Let&#8217;s look at some code:</p>
<div>
<pre name="code" class="css">
.highlight {
	color: Red;
}
</pre>
</div>
<p>translates into the following <span class="caps">XAML</span>:</p>
<div>
<pre name="code" class="xml">

&lt;Style x:Key="highlight"&gt;
    &lt;Setter Property="Foreground" Value="Red" /&gt;
&lt;/Style&gt;
</pre>
</div>
<p>Inside a <code>&lt;Style&gt;</code> you can change any property on the target element, in our case the Foreground property that accepts a display brush. </p>
<p>Note that omitting the <code>TargetType</code> property limits you to only properties on <code>FrameworkElement</code> so you might want to define something more specific like <code>TargetType="TextBlock"</code> etc.</p>
<p>If you don&#8217;t specify a <code>x:Key</code> the style will be applied to all possible elements matching the <code>TargetType</code>.</p>
<p>Let&#8217;s look at <span class="caps">CSS</span> and <span class="caps">XAML</span> side-by-side:</p>
<div>
<pre name="code" class="css">

span {
    color: Red;
}
</pre>
</div>
<p>is roughly the same as:</p>
<div>
<pre name="code" class="xml">

&lt;Style TargetType="TextBlock"&gt;
    &lt;Setter Property="Foreground" Value="Red" /&gt;
&lt;/Style&gt;
</pre>
</div>
<h3>Templates</h3>
<p>Now we know how <span class="caps">CSS</span> (at least in a way) maps to <span class="caps">XAML</span>. Things are still pretty different and one of the things that you need to wrap your head around in <span class="caps">XAML</span> is templates.</p>
<p>Every control you drag to the surface is usually a collection of borders, shapes and other lower level framework stuff that makes up your control. </p>
<p>So if you want to modify the appearance of your control, you do that via the <code>ControlTemplate</code>. And this is where I am still not 100% sure what the common theme is, but I am getting better at it.</p>
<p>ControlTemplates can be applied either directly to the element, or via a Style inside a Setter.</p>
<div>
<pre name="code" class="xml">

&lt;Style TargetType="TextBox"&gt;
&lt;Setter Property="Template"&gt;
	&lt;Setter.Value&gt;
		&lt;ControlTemplate TargetType="TextBox"&gt;
			&lt;!-- Your Template goes here --&gt;
		&lt;/ControlTemplate&gt;
	&lt;/Setter.Value&gt;
&lt;/Setter&gt;
&lt;/Style&gt;
</pre>
</div>
<p>As you can see, all you do inside a <code>&lt;Setter&gt;</code> is pretty much the same as you would apply to the element itself in markup. If you have non-trivial values to set you can use the <Setter.Value> as you can do with almost all things in <span class="caps">XAML</span>. </p>
<p>This is actually an interesting concept that took some getting used to. In <span class="caps">XAML</span> you can specify each and any attribute of the element by using the <code>&lt;Element.AttributeName&gt;</code> syntax in case it&#8217;s not expressable by a simple literal value.</p>
<p>Now let&#8217;s assume I want to change the appearance of a TextBox to have a flat border. If you come from WinForms like me you&#8217;ll spend a fair amount of time hunting for a BorderStyle property that&#8217;s simply not existant in <span class="caps">WPF</span>. </p>
<p>The way to go here is: You guessed it, Control Templates.</p>
<p>The ControlTemplate allows you to replace the visual tree of your object and roll your own that suits your needs (makes it powerful, but rather hard to figure out). My naive approach to this was to simply define the border in <span class="caps">WPF</span> like this:</p>
<div>
<pre name="code" class="xml">
&lt;ControlTemplate TargetType="{x:Type TextBoxBase}"&gt;
&lt;Border Name="Border"
	BorderThickness="1"
	Padding="4,2"
	Height="Auto"
	BorderBrush="#FFCCCCCC"&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
</pre>
</div>
<p>What happened was that I turned my TextBox into a border. No text entering, nothing. I changed the visual tree of the object to only contain a border, and that&#8217;s what I got.</p>
<p>It then took me a little while to figure out that you can&#8217;t simply change the tree without putting back in an element to write the text to. Apparently for textboxes you have to put in a <code>ScrollViewer</code> named <code>PART_ContentHost</code>.</p>
<p>So the correct way here is:</p>
<div>
<pre name="code" class="xml">

&lt;ControlTemplate TargetType="{x:Type TextBoxBase}"&gt;
&lt;Border Name="Border"
		BorderThickness="1"
		Padding="4,2"
		Height="Auto"
		BorderBrush="#FFCCCCCC"&gt;
	&lt;ScrollViewer x:Name="PART_ContentHost" /&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
</pre>
</div>
<p>This then gives us a nice flat border with 1 pixel thickness and a nice shade of gray instead of the gradient brush <span class="caps">WPF</span> uses by default.</p>
<h3>Triggers</h3>
<p>Another very important concept (at least to me) was triggers. Assuming we made our control look pretty, we may want to change it&#8217;s appearance once you mouseover it or focus it. </p>
<p>If you try like me using the <code>VisualFocusStyle</code> to do that you&#8217;ll soon notice that VisualFocusStyles are not applied when a user clicks the element, only when he navigates to it by keyboard. It was meant to provide UI hints for keyboard navigation, not to indicate focusing in any way.</p>
<p>The correct way to do stuff like OnFocus and OnMouseOver is by using Triggers. With triggers you can do something whenever a property matches a certain value. So in our case we can create a trigger that changes the border color whenever the <code>IsFocused</code> property contains the value <code>True</code>.</p>
<p>I am applying the trigger to my ControlTemplate we defined earlier:</p>
<div>
<pre name="code" class="xml">

&lt;ControlTemplate.Triggers&gt;
&lt;Trigger Property="IsFocused" Value="True"&gt;
	&lt;Setter TargetName="Border" Property="BorderBrush" Value="#FF00A9DA" /&gt;
&lt;/Trigger&gt;
&lt;/ControlTemplate.Triggers&gt;
</pre>
</div>
<p>This trigger now only works with one Property/Value, IsFocused. But there is a way to verify that two conditions are met before the trigger fires using the <code>MultiTrigger</code>.</p>
<p>The concept is the same:</p>
<div>
<pre name="code" class="xml">

&lt;MultiTrigger&gt;
&lt;MultiTrigger.Conditions&gt;
	&lt;Condition Property="IsMouseOver" Value="True"&gt;&lt;/Condition&gt;
	&lt;Condition Property="IsFocused" Value="False" /&gt;
&lt;/MultiTrigger.Conditions&gt;
&lt;Setter TargetName="Border" Property="BorderBrush" Value="Black" /&gt;
&lt;/MultiTrigger&gt;
</pre>
</div>
<p>This MultiTrigger will only apply the black BorderBrush if IsFocused is false and IsMouseOver is true. Thus making sure the IsFocus style is not overridden and flickering whenever the user waves the mouse around.</p>
<h3>DataTriggers</h3>
<p>Triggers are only good for one thing: Firing if properties  on the Visual Tree change. If you want to do something when something in your DataContext changes you need DataTriggers. These work by defining a Binding and a Value, but in essence work pretty much like normal Triggers, only that they work on anything you can bind to. </p>
<p>DataTriggers also come along in <code>MultiDataTrigger</code> and <code>DataTrigger</code> flavors.</p>
<p>DataTriggers are also the answer to most questions concerning <span class="caps">MVVM</span> asking &#8220;How can I show X from my ViewModel.&#8221;. The answer here is usually: &quot;Define some boolean property (with INotifyPropertyChanged) and set up a DataTrigger to it. (This works for starting animations etc.. Everything.. This is crazy powerful!)</p>
<p>And that&#8217;s about all the stuff I spent most of my morning figuring out. I hope you get any useful information out of it. Because I quickly became frustrated with the content that&#8217;s out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tigraine.at/2010/10/20/theming-controls-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use different layout containers in WPF</title>
		<link>http://www.tigraine.at/2010/09/17/how-to-use-different-layout-containers-in-wpf/</link>
		<comments>http://www.tigraine.at/2010/09/17/how-to-use-different-layout-containers-in-wpf/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 17:00:00 +0000</pubDate>
		<dc:creator>Daniel Hölbling</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.tigraine.at/2010/09/17/how-to-use-different-layout-containers-in-wpf/</guid>
		<description><![CDATA[WPF is different, that much I knew before I started learning it. But understanding really how different it is really takes a lot of time and effort. Especially when used to Windows Forms and HTML+CSS, WPF feels very alien in it’s way how to do layout. Here is what I learned: StackPanel: StackPanels are used [...]]]></description>
			<content:encoded><![CDATA[<p>WPF is different, that much I knew before I started learning it. But understanding really how different it is really takes a lot of time and effort. Especially when used to Windows Forms and HTML+CSS, WPF feels very alien in it’s way how to do layout. Here is what I learned:</p>
<p><strong>StackPanel:</strong></p>
<p>StackPanels are used to layout elements horizontally stacked onto each other. </p>
<p><a href="http://www.tigraine.at/wp-content/uploads/2010/09/image7.png"><img style="background-image: none; border-right-width: 0px; margin: 0px 10px 3px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2010/09/image_thumb6.png" width="318" height="171" /></a></p>
<p>By default the elements inside a StackPanel will take up their set height and you can make them fill the whole width by setting their <em>VerticalAlignment</em> to <em>Stretch</em></p>
<p>They will however never fill the full height of the container when it is resized, no matter how your <em>HorizontalAlign </em>property is set. If the above container got resized in height the buttons would still stick to the top with their individual heights staying just the same.</p>
<p><strong>WrapPanel:</strong></p>
<p>They are the same as the StackPanel, just in vertical. They stack elements vertically and reorder them if there is no more space vertically. However they also don’t allow them to fill the vertical space available to the control.</p>
<p><a href="http://www.tigraine.at/wp-content/uploads/2010/09/image8.png"><img style="background-image: none; border-right-width: 0px; margin: 0px 10px 3px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2010/09/image_thumb7.png" width="216" height="166" /></a></p>
<p><strong>The Dockpanel:</strong></p>
<p>Where both WrapPanel and StackPanel don’t allow Elements to fill vertical space (even with HorizontalAlignment to Stretch) the DockPanel will do just that by default:</p>
<p><a href="http://www.tigraine.at/wp-content/uploads/2010/09/image9.png"><img style="background-image: none; border-right-width: 0px; margin: 0px 10px 3px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2010/09/image_thumb8.png" width="244" height="153" /></a></p>
<p>The trick here is that only the last child really fills the panel, so while resizing the height will resize all 3 buttons, a horizontal resize will only cause the third button to change size. This has to do with the LastChildFill property, where Button3 is considered the filling element. </p>
<p>Why am I telling you this? Well, turns out most UIs look something like this:</p>
<p>Some header, then one filling list that should resize and then some controls around the edges. Making the list resize with the parent container can’t be achieved by the other panels at all. You have to use a DockPanel and use the DockPanel.Dock attributes on your child controls:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:f5a0003e-560c-4e80-afc7-a0fa94da07e6" class="wlWriterEditableSmartContent">
<pre name="code" class="xml">
&lt;DockPanel Margin="0,0,0,0"&gt;
	&lt;TextBlock DockPanel.Dock="Top" Text="Hello World" /&gt;
	&lt;TextBlock DockPanel.Dock="Bottom" Text="Bottom" /&gt;
	&lt;Button Content="Button3"&gt;&lt;/Button&gt;
&lt;/DockPanel&gt;
</pre>
</div>
<p>This will make the last element fill all available space left by the elements that where docked to the sides like this:</p>
<p><a href="http://www.tigraine.at/wp-content/uploads/2010/09/image10.png"><img style="background-image: none; border-right-width: 0px; margin: 0px 10px 3px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2010/09/image_thumb9.png" width="248" height="155" /></a></p>
<p>Once you understand these three layout concepts, you can build pretty much everything by nesting panels within each other. There is however also the GridPanel that lends itself very well to creating forms and other stuff that has to be aligned on a grid. But I won’t go into that in this post (and I think it’s markup is just awful. HTML4 tables were more intuitive to write)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tigraine.at/2010/09/17/how-to-use-different-layout-containers-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beware of &lt;Button IsDefault=&#8221;True&#8221;&gt; in WPF/Silverlight</title>
		<link>http://www.tigraine.at/2010/09/13/beware-of-button-isdefaulttrue-in-wpfsilverlight/</link>
		<comments>http://www.tigraine.at/2010/09/13/beware-of-button-isdefaulttrue-in-wpfsilverlight/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 11:59:02 +0000</pubDate>
		<dc:creator>Daniel Hölbling</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.tigraine.at/2010/09/13/beware-of-button-isdefaulttrue-in-wpfsilverlight/</guid>
		<description><![CDATA[I’ve spent the better part of my weekend finding this bug and thought I’d share it in case someone else is having the problem. I’m building a WPF user interface with a NServiceBus backend and started noticing that sometimes changing data on the client didn’t generate UPDATE statements in my Database. I first assumed it [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve spent the better part of my weekend finding this bug and thought I’d share it in case someone else is having the problem. I’m building a WPF user interface with a NServiceBus backend and started noticing that sometimes changing data on the client didn’t generate UPDATE statements in my Database. I first assumed it had to do with my backend code, then I started digging through the various libraries I am using (NHibernate and NServiceBus) to find the problem. </p>
<p>After 2 days debugging through the backend code I finally found out that it’s the WPF application that sometimes sent wrong data. The scenario is quite simple:</p>
<p><a href="http://www.tigraine.at/wp-content/uploads/2010/09/image5.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2010/09/image_thumb5.png" width="328" height="241" /></a></p>
<p>This very simple dialog uses a <a href="http://www.tigraine.at/2010/09/10/wpf-formatstring-and-its-localization-bugs/">StringFormat</a> to display the Price field. This prevents me from using any other binding than a <em>UpdateSourceTrigger=”OnLostFocus”</em> because otherwise it would reformat the field on every key-press. Rendering it unusable due to a jumping focus caret.&#160; </p>
<p>The issue was that upon hitting Save, sometimes the Save method was executed before the data was bound to the model, sometimes the other way around. I genuinely thought I had a race condition until I noticed one little thing: Since it’s a decimal field I usually fill those in by using the number pad. That also led to me hitting the Enter quite often without thinking about it, thus invoking the Save method while still having the focus on the Price Textbox. </p>
<p>If you set a Button to<em> IsDefault=”True”</em> and hit Enter the Command will be executed without triggering the OnLostFocus Databindings in your Textbox, making your code act on stale model data. The only way around this is to either use <em>UpdateSourceTrigger=”OnPropertyChanged”</em> (not always applicable) or remove the IsDefault attribute from your form. </p>
<p>I still believe this is a bug in WPF, but at least now I know about it and can avoid it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tigraine.at/2010/09/13/beware-of-button-isdefaulttrue-in-wpfsilverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPF FormatString and it&#8217;s localization bugs</title>
		<link>http://www.tigraine.at/2010/09/10/wpf-formatstring-and-its-localization-bugs/</link>
		<comments>http://www.tigraine.at/2010/09/10/wpf-formatstring-and-its-localization-bugs/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 13:03:55 +0000</pubDate>
		<dc:creator>Daniel Hölbling</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.tigraine.at/2010/09/10/wpf-formatstring-and-its-localization-bugs/</guid>
		<description><![CDATA[As some of you may know, I am from Austria. That means that I get to pay my bills in Euro and we format our decimals with a comma instead of a period (yes I know I’m weird). So naturally my culture setting is de-AT and I really expect to see my decimals formatted this [...]]]></description>
			<content:encoded><![CDATA[<p>As some of you may know, I am from Austria. That means that I get to pay my bills in Euro and we format our decimals with a comma instead of a period (yes I know I’m weird). </p>
<p>So naturally my culture setting is de-AT and I really expect to see my decimals formatted this way: € 19,99   <br />Turns out, WPF doesn’t give a damn and if you use FormatString in a binding it will just go ahead and return en-US formats!</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:16a98245-b48d-4b23-9025-9e1fe611e766" class="wlWriterEditableSmartContent">
<pre name="code" class="xml">
&lt;TextBox Text="{Binding Path=Model.Price, StringFormat=\{0:c\}}"/&gt;
</pre>
</div>
<p>This is a bug in WPF and has been there for more than 2 years now from what I can gather. There is a <a href="http://www.nbdtech.com/Blog/archive/2009/02/22/wpf-data-binding-cheat-sheet-update-the-internationalization-fix.aspx">fix to it</a> as suggested by <a href="http://twitter.com/nirdobovizki">Nir Dobovizki</a>, who coincidentally also has a pretty cool <a href="http://go.nbdtech.com?94E138EA">CheatSheet on WPF Databinding</a> that I now have taped to the wall.</p>
<p>For the sake of completeness here is the code you have to put in your App startup code (I’ve thrown it in my App constructor):</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:543174bf-e84f-4f00-ba38-76b53eae9512" class="wlWriterEditableSmartContent">
<pre name="code" class="csharp">
FrameworkElement.LanguageProperty.OverrideMetadata(
&#160;&#160;&#160;&#160;typeof(FrameworkElement),
&#160;&#160;&#160;&#160;new FrameworkPropertyMetadata(
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
</pre>
</div>
<p>While at it I also found out another pretty cool thing that managed to save me a lot of markup in XAML: MultiBinding! You can apply StringFormat to single elements through the {Binding} blocks, but sometimes you want to show stuff like “Showing Page x of y” somewhere.<br />
  <br />My naïve approach was to just create 4 elements and bind 2 of them to the appropriate values. As <a href="http://elegantcode.com/author/blagunas/">Brian</a> points out in his post on <a href="http://elegantcode.com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/">WPF StringFormat</a> you can just do stuff like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:0523d3f4-907f-4a8e-845b-5e0ad668dd81" class="wlWriterEditableSmartContent">
<pre name="code" class="xml">
&lt;TextBlock VerticalAlignment="Center" Margin="0, 0, 0, 10"&gt;
&#160;&#160;&#160;&#160;&lt;TextBlock.Text&gt;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;MultiBinding StringFormat="Showing Page {0} of {1}"&gt;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;Binding Path="CurrentPage" /&gt;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;Binding Path="NumberOfPages" /&gt;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;/MultiBinding&gt;
&#160;&#160;&#160;&#160;&lt;/TextBlock.Text&gt;
&lt;/TextBlock&gt;
</pre>
</div>
<p>The StringFormat attribute works exactly like the String.Format method whereas the &lt;Binding&gt; children are the parameters passed into it. Pretty cool to say the least. </p>
<p>Now if only someone explained to me why WPF Grids have this hideous way of cluttering up my markup with Grid.Row=”0” Grid.Column=”1” I may finally make my peace with WPF (aka the display technology I stayed away until now because learning it seemed like an impossible task)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tigraine.at/2010/09/10/wpf-formatstring-and-its-localization-bugs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Untangling the dependency ball! Windsor + NServiceBus + Caliburn + Fluent Nhibernate in one package</title>
		<link>http://www.tigraine.at/2010/09/06/untangling-the-dependency-ball-windsor-nservicebus-caliburn-fluent-nhibernate-in-one-package/</link>
		<comments>http://www.tigraine.at/2010/09/06/untangling-the-dependency-ball-windsor-nservicebus-caliburn-fluent-nhibernate-in-one-package/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 20:47:41 +0000</pubDate>
		<dc:creator>Daniel Hölbling</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Castle]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.tigraine.at/2010/09/06/untangling-the-dependency-ball-windsor-nservicebus-caliburn-fluent-nhibernate-in-one-package/</guid>
		<description><![CDATA[Unfortunately nu is still falling short on one thing: Making sure that all the stuff you install is actually compatible with the other stuff you have already installed. There is a ticket for this and I’m fairly confident this will get resolved (please vote the ticket up), but for now I was back to figuring [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tigraine.at/wp-content/uploads/2010/09/confusingroadsignlargewebview.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 10px 3px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="confusing-road-sign-large-web-view" border="0" alt="confusing-road-sign-large-web-view" src="http://www.tigraine.at/wp-content/uploads/2010/09/confusingroadsignlargewebview_thumb.jpg" width="211" height="244" /></a></p>
<p>Unfortunately <a href="http://www.tigraine.at/2010/08/25/the-fastest-way-to-install-dotless-nubular-nu/">nu</a> is still falling short on one thing: Making sure that all the stuff you install is actually compatible with the other stuff you have already installed. There is a <a href="http://github.com/nu/nu/issues/issue/13">ticket for this</a> and I’m fairly confident this will get resolved (please vote the ticket up), but for now I was back to figuring out what version of what framework to use to make my app compile.</p>
<p>As always, the main problem was Castle.Core, being present in 3 different versions. (NSB used version 1.1, Caliburn 1.2 and the latest Windsor release targets 2.5) </p>
<p>I decided to back down and use 1.2 since there is a NHibernate gem for 1.2 and a Windsor gem for 1.2. I’m now using NHibernate 3.0 alpha so think about using this “stack”. </p>
<p>Anyway, this is a collection of:</p>
<ol>
<li>NServiceBus 2.0 .NET 4 (2.0.0.1219)</li>
<li>NServiceBus.ObjectBuilder.CastleWindsor</li>
<li>Castle.Windsor (2.1)</li>
<li>Caliburn 2.0 (still unreleased from the trunk)</li>
<li>NHibernate 3.0.0.1002</li>
<li>FluentNhibernate 1.1 (Updated to NHibernate 3.0)</li>
<li>AutoMapper</li>
</ol>
<p>Disclaimer: The whole thing is built for .NET 4.0 and works on my machine. Don’t blame me if it’s broken for you.</p>
<p>Anyway. You can download the whole package of libraries here: <a href="http://www.tigraine.at/wp-content/uploads/2010/09/castle-stack.rar">castle-stack.rar</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tigraine.at/2010/09/06/untangling-the-dependency-ball-windsor-nservicebus-caliburn-fluent-nhibernate-in-one-package/feed/</wfw:commentRss>
		<slash:comments>98</slash:comments>
		</item>
		<item>
		<title>Useful Resharper Live Template for WPF development</title>
		<link>http://www.tigraine.at/2010/04/29/useful-resharper-live-template-for-wpf-development/</link>
		<comments>http://www.tigraine.at/2010/04/29/useful-resharper-live-template-for-wpf-development/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 17:13:30 +0000</pubDate>
		<dc:creator>Daniel Hölbling</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.tigraine.at/2010/04/29/useful-resharper-live-template-for-wpf-development/</guid>
		<description><![CDATA[While doing some work in WPF for a customer project I found myself writing far too often code like this: private bool isSelected; public bool IsSelected { &#160;&#160;&#160;&#160;get { return isSelected; } &#160;&#160;&#160;&#160;set &#160;&#160;&#160;&#160;{ &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;isSelected = value; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;OnPropertyChanged("IsSelected"); &#160;&#160;&#160;&#160;} } Auto-properties simply don’t work with INotifyPropertyChanged so you have to do all the grunt work [...]]]></description>
			<content:encoded><![CDATA[<p>While doing some work in WPF for a customer project I found myself writing far too often code like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:b615157c-4b99-4860-ba0b-66974eecb1b5" class="wlWriterEditableSmartContent">
<pre name="code" class="csharp">
private bool isSelected;

public bool IsSelected
{
&#160;&#160;&#160;&#160;get { return isSelected; }
&#160;&#160;&#160;&#160;set
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;isSelected = value;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;OnPropertyChanged("IsSelected");
&#160;&#160;&#160;&#160;}
}
</pre>
</div>
<p>Auto-properties simply don’t work with INotifyPropertyChanged so you have to do all the grunt work over again .. Thank god there is Resharper!</p>
<p>It’s said lazyness is a virtue on a programmer, so I made this little Live Template that will create all that code with you only having to fill in type and name of your property:</p>
<p><a href="http://www.tigraine.at/wp-content/uploads/2010/04/image3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2010/04/image_thumb2.png" width="236" height="193" /></a> </p>
<p>Download it here: <a href="http://tigraine.at/wpfprop.xml">wpfprop.xml</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tigraine.at/2010/04/29/useful-resharper-live-template-for-wpf-development/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WebKit like focus indicator for WPF Windows</title>
		<link>http://www.tigraine.at/2009/06/22/webkit-like-focus-indicator-for-wpf-windows/</link>
		<comments>http://www.tigraine.at/2009/06/22/webkit-like-focus-indicator-for-wpf-windows/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 11:58:48 +0000</pubDate>
		<dc:creator>Daniel Hölbling</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.tigraine.at/?p=580</guid>
		<description><![CDATA[If you like Chrome/Safari or not, one thing that both have and all others lack is a good focus indicator that graphically shows me where my focus currently is. Jeff Atwood wrote something interesting on the topic of Where the Heck is My Focus: But even if developers do remember to test for basic keyboard [...]]]></description>
			<content:encoded><![CDATA[<p>If you like Chrome/Safari or not, one thing that both have and all others lack is a good focus indicator that graphically shows me where my focus currently is. Jeff Atwood wrote something interesting on the topic of <a href="http://www.codinghorror.com/blog/archives/001055.html">Where the Heck is My Focus</a>:</p>
<blockquote><p>But even if developers do remember to test for basic keyboard behavior, there&#8217;s a deeper problem here. <b>Keyboard navigation relies heavily on the focus.</b> In order to move from one area to the next, you have to be able to reliably know where you are. Unfortunately, <b>web browsers make it needlessly difficult to tell where the focus is</b>.</p>
</blockquote>
<p>I believe he not only has a valid point here, but also that his criticism this should not be limited to web browsers. Most if not all Windows applications do this thing badly, and it’s up to us developers to fix it.</p>
<p>So, I decided to try to put my recent WPF research to good use and tried to implement a small class that applies/removes a WebKit like focus caret:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2009/06/image4.png" width="300" height="145" /> </p>
<p>The whole implementation is completely encapsulated inside a class named <a href="http://bitbucket.org/Tigraine/tigraine-samples/src/tip/WebkitInputs/WebkitFocusEffect.cs">WebkitFocusEffect</a> that only needs to be initialized during your window construction:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:7d6cb718-92e9-4338-86a2-ae7276ea0b11" class="wlWriterEditableSmartContent">
<pre name="code" class="csharp">
public Window1()
{
&#160;&#160;&#160;&#160;InitializeComponent();

&#160;&#160;&#160;&#160;WebkitFocusEffect.Initialize(this);
}
</pre>
</div>
<h4>How does it work?</h4>
<p>First of all, <a href="http://bitbucket.org/Tigraine/tigraine-samples/src/tip/WebkitInputs/WebkitFocusEffect.cs">the source is available</a> on BitBucket in my <a href="http://bitbucket.org/Tigraine/tigraine-samples/">repository</a>, so you can just go ahead and look at it. But I’ll also try to shed some light about what goes on there.</p>
<h5>Routed Events</h5>
<p>WPF introduced a cool concept called RoutedEvents, meaning that an Event like GotFocus/LostFocus will travel through the object model to the point where it gets handled and stops there. This technique is important because, unlike Windows Forms, WPF controls can contain other UIElements. This gives you far more control over the look and feel of your application, allowing for crazy things like a button with a image instead of text:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:3bd97067-1206-4c74-9d66-c09fc94c1346" class="wlWriterEditableSmartContent">
<pre name="code" class="xml">
&lt;Button Width="100"&gt;
&#160;&#160;&#160;&#160;&lt;Image Source="http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/100px-Smiley.svg.png"&gt;&lt;/Image&gt;
&lt;/Button&gt;
</pre>
</div>
<p>Resulting in this:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2009/06/image5.png" width="159" height="163" /> </p>
<p>And that leads to the point of RoutedEvents: How do you know that the button was clicked? The event that was fired was the image’s click event, not the button. So, WPF introduced the concept of RoutedEvents that can traverse the object tree upwards and downwards. What means that the image’s ClickEvent gets passed on to it’s direct parent (our friend the button) to get handled there.</p>
<p>&#160;<img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.tigraine.at/wp-content/uploads/2009/06/image6.png" width="400" height="235" /> </p>
<p>If not handled at some level the event would travel up through the whole tree until reaching the root.</p>
<p>I used this technique to hook up the GotFocus/LostFocus events on our window (being the root element), relying on the fact that any GotFocus events by it’s children will bubble upwards the graph eventually reaching the window’s handler.</p>
<p>The handler then just unwraps the event’s source object (the source of the RoutedEvent gets passed along through the RoutedEventArgs parameter) and modifies it’s Effect property:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:53353ad9-5043-497d-ac84-588d9db99247" class="wlWriterEditableSmartContent">
<pre name="code" class="csharp">
protected virtual void WindowGotFocus(object sender, RoutedEventArgs e)
{
&#160;&#160;&#160;&#160;try
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if (!(e.Source is UIElement)) return;

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;var element = (UIElement) e.Source;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if (element.Effect == null)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;var effect = new DropShadowEffect {Color = Colors.Gold, ShadowDepth = 0, BlurRadius = 8};
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;element.Effect = effect;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;removeEffect = true;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;catch (Exception ex)
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Log(ex);
&#160;&#160;&#160;&#160;}
}
</pre>
</div>
<p>One problem still remained: RoutedEvents can stop bubbling if they get handled at a lower level. This is how the button stops the image’s click event from spreading to it’s parent element, therefore containing it. So if you set an RoutedEventArgs.Handled property to true, it will stop bubbling up the tree:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:63d90cb1-6644-4e6f-8b6a-e3a523361c63" class="wlWriterEditableSmartContent">
<pre name="code" class="csharp">
private void LoginButton_GotFocus(object sender, RoutedEventArgs e)
{
&#160;&#160;&#160;&#160;//This Event was handled and will not call Window's GotHandled eventhandler
&#160;&#160;&#160;&#160;e.Handled = true;
}
</pre>
</div>
<p>Cool though is that RoutedEvents can’t really be stopped from bubbling, they do so anyway. Handled events just don’t invoke any event handlers up the tree any more, and that can be overridden explicitly when subscribing to the event:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:2e6d557b-b705-4c34-b5ad-8606cf99c7de:a647969b-f8d9-404d-af20-0b63b8797ed8" class="wlWriterEditableSmartContent">
<pre name="code" class="csharp">
window.AddHandler(UIElement.GotFocusEvent, new RoutedEventHandler(WindowGotFocus), true);
</pre>
</div>
<p>The last parameter is “handledEventsToo” and if set to true the event handler will be fired even if the event was already handled at a lower level. Allowing us to still do our thing while not getting in your way when subscribing to GotFocus / LostFocus events.</p>
<h5>Effect</h5>
<p>One little limitation is there though. Currently there is no EffectGroup container similar to the <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.effects.bitmapeffectgroup.aspx">BitmapEffectGroup</a>, and therefore only one effect can be applied to any UIElement at one time. So WebkitFocusEffect will just skip elements that already have a Effect defined.</p>
<p>I consciously went with the <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.effects.dropshadoweffect.aspx">DropShadowEffect</a> and not with the <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.effects.outerglowbitmapeffect.aspx">OuterGlowBitmapEffect</a> because Effects are hardware accelerated (means they get processed by your idle graphics card instead of the CPU) and will not slow your application down as BitmapEffects would. </p>
</p>
<p>You can download the <a href="http://bitbucket.org/Tigraine/tigraine-samples/src/tip/WebkitInputs/WebkitFocusEffect.cs">WebkitFocusEffect.cs</a> through Bitbucket.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tigraine.at/2009/06/22/webkit-like-focus-indicator-for-wpf-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

