<?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>LegHumped.com &#187; WPF &#187; Leghumped</title>
	<atom:link href="http://www.leghumped.com/blog/category/programming/wpf-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.leghumped.com/blog</link>
	<description>Coding snippets, tutorials and procrastinations about C#, Java and Linux.</description>
	<lastBuildDate>Wed, 11 Jan 2012 20:15:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>WPF: Radio Toggle Button</title>
		<link>http://www.leghumped.com/blog/2011/04/24/441/</link>
		<comments>http://www.leghumped.com/blog/2011/04/24/441/#comments</comments>
		<pubDate>Sun, 24 Apr 2011 20:29:42 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://www.leghumped.com/blog/?p=441</guid>
		<description><![CDATA[There are times when you might want to display a toggle switch to let the user pick between one or more views. The easiest way to implement this in WPF is with RadioButtons and custom templates. Persistence can easily be added by binding to an application settings resource, as in the example below. The first [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when you might want to display a toggle switch to let the user pick between one or more views. The easiest way to implement this in WPF is with RadioButtons and custom templates. Persistence can easily be added by binding to an application settings resource, as in the example below.</p>
<p>The first thing you need is a custom control. This just needs to inherit from RadioButton, I added a few extra properties to make adding images easier.</p>
<p><span id="more-441"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> Lime49<span style="color: #008000;">.</span><span style="color: #0000FF;">UI</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// A button which can be assigned an image</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> RadioImageButton <span style="color: #008000;">:</span> RadioButton <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty ImageLocationProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ImageLocation&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>PlacementMode<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>RadioImageButton<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> UIPropertyMetadata<span style="color: #008000;">&#40;</span>PlacementMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets or sets the position of the image in relative to the text.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;value&gt;The image.&lt;/value&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> PlacementMode ImageLocation <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>PlacementMode<span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>ImageLocationProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>ImageLocationProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty ImageProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Image&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ImageSource<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>RadioImageButton<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets or sets the image to display.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;value&gt;The image.&lt;/value&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> ImageSource Image <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>ImageProperty<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> ImageSource<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>ImageProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty TextProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Text&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>RadioImageButton<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets or sets the text.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;value&gt;The text.&lt;/value&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Text <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>TextProperty<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>TextProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty ShowTextProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ShowText&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>RadioImageButton<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> UIPropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets or sets whether to show the text.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;value&gt;&lt;c&gt;true&lt;/c&gt; if the text should be shown, otherwise &lt;c&gt;false&lt;/c&gt;.&lt;/value&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> ShowText <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>ShowTextProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>ShowTextProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty AlwaysShowBorderProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;AlwaysShowBorder&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>RadioImageButton<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets or sets whether to always show the border.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;value&gt;&lt;c&gt;true&lt;/c&gt; if the border should be shown, otherwise &lt;c&gt;false&lt;/c&gt;.&lt;/value&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> AlwaysShowBorder <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>AlwaysShowBorderProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>AlwaysShowBorderProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The control template just needs to take these properties and display them. Ie (Visual Statemanager states omitted for brevity):</p>

<div class="wp_syntax"><div class="code"><pre class="xaml" style="font-family:monospace;">&lt;Style TargetType=&quot;{x:Type loc:RadioImageButton}&quot; x:Key=&quot;{x:Type loc:RadioImageButton}&quot;&gt;
        &lt;Setter Property=&quot;Margin&quot; Value=&quot;4,2&quot; /&gt;
        &lt;Setter Property=&quot;ToolTipService.ShowOnDisabled&quot; Value=&quot;True&quot; /&gt;
        &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Stretch&quot; /&gt;
        &lt;Setter Property=&quot;VerticalContentAlignment&quot; Value=&quot;Top&quot; /&gt;
        &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Center&quot; /&gt;
        &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Center&quot; /&gt;
        &lt;Setter Property=&quot;SnapsToDevicePixels&quot; Value=&quot;True&quot; /&gt;
        &lt;Setter Property=&quot;ShowText&quot; Value=&quot;True&quot; /&gt;
        &lt;Setter Property=&quot;ImageLocation&quot; Value=&quot;Top&quot; /&gt;
        &lt;Setter Property=&quot;Foreground&quot; Value=&quot;{DynamicResource NormalForeground}&quot;/&gt;
        &lt;Setter Property=&quot;Background&quot; Value=&quot;{DynamicResource NormalBrush}&quot;/&gt;
        &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;{DynamicResource NormalBorderBrush}&quot;/&gt;
        &lt;Setter Property=&quot;Template&quot;&gt;
            &lt;Setter.Value&gt;
                &lt;ControlTemplate TargetType=&quot;{x:Type loc:RadioImageButton}&quot;&gt;
                    &lt;Border x:Name=&quot;Border&quot; Padding=&quot;3&quot; BorderBrush=&quot;{TemplateBinding BorderBrush}&quot; Background=&quot;{TemplateBinding Background}&quot;
                        CornerRadius=&quot;1&quot; BorderThickness=&quot;1&quot; ToolTip=&quot;{TemplateBinding ToolTip}&quot; ToolTipService.ShowOnDisabled=&quot;True&quot;&gt;
                        &lt;DockPanel x:Name=&quot;Container&quot; HorizontalAlignment=&quot;{TemplateBinding HorizontalAlignment}&quot; VerticalAlignment=&quot;Center&quot;&gt;
                            &lt;Image x:Name=&quot;PART_img&quot; Source=&quot;{TemplateBinding Image}&quot; Stretch=&quot;None&quot; SnapsToDevicePixels=&quot;True&quot;
                               IsEnabled=&quot;{TemplateBinding IsEnabled}&quot; IsHitTestVisible=&quot;False&quot; /&gt;
                            &lt;TextBlock x:Name=&quot;PART_text&quot; Text=&quot;{TemplateBinding Text}&quot; IsEnabled=&quot;{TemplateBinding IsEnabled}&quot; VerticalAlignment=&quot;{TemplateBinding VerticalContentAlignment}&quot;
                                   TextTrimming=&quot;CharacterEllipsis&quot; TextWrapping=&quot;Wrap&quot; MaxWidth=&quot;{TemplateBinding MaxWidth}&quot; MaxHeight=&quot;{TemplateBinding MaxWidth}&quot;
                                   TextAlignment=&quot;{TemplateBinding HorizontalAlignment}&quot; /&gt;
                            &lt;Grid x:Name=&quot;ValidationErrorElement&quot; Visibility=&quot;Collapsed&quot; ToolTipService.PlacementTarget=&quot;{Binding RelativeSource={RelativeSource TemplatedParent}}&quot;&gt;
                                &lt;ToolTipService.ToolTip&gt;
                                    &lt;ToolTip x:Name=&quot;validationTooltip&quot; 
                                       Template=&quot;{StaticResource ValidationToolTipTemplate}&quot;
                                       DataContext=&quot;{Binding RelativeSource={RelativeSource TemplatedParent}}&quot;
                                       Placement=&quot;Right&quot; 
                                       PlacementTarget=&quot;{Binding RelativeSource={RelativeSource TemplatedParent}}&quot;&gt;
                                        &lt;ToolTip.Triggers&gt;
                                            &lt;EventTrigger RoutedEvent=&quot;Canvas.Loaded&quot;&gt;
                                                &lt;EventTrigger.Actions&gt;
                                                    &lt;BeginStoryboard&gt;
                                                        &lt;Storyboard&gt;
                                                            &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName=&quot;validationTooltip&quot; Storyboard.TargetProperty=&quot;IsHitTestVisible&quot;&gt;
                                                                &lt;DiscreteObjectKeyFrame KeyTime=&quot;0&quot; &gt;
                                                                    &lt;DiscreteObjectKeyFrame.Value&gt;
                                                                        &lt;sys:Boolean&gt;true&lt;/sys:Boolean&gt;
                                                                    &lt;/DiscreteObjectKeyFrame.Value&gt;
                                                                &lt;/DiscreteObjectKeyFrame&gt;
                                                            &lt;/ObjectAnimationUsingKeyFrames&gt;
                                                        &lt;/Storyboard&gt;
                                                    &lt;/BeginStoryboard&gt;
                                                &lt;/EventTrigger.Actions&gt;
                                            &lt;/EventTrigger&gt;
                                        &lt;/ToolTip.Triggers&gt;
                                    &lt;/ToolTip&gt;
                                &lt;/ToolTipService.ToolTip&gt;
                            &lt;/Grid&gt;
                        &lt;/DockPanel&gt;
                    &lt;/Border&gt;
                    &lt;ControlTemplate.Triggers&gt;
                        &lt;Trigger Property=&quot;ImageLocation&quot;&gt;
                            &lt;Trigger.Value&gt;
                                &lt;PlacementMode&gt;Left&lt;/PlacementMode&gt;
                            &lt;/Trigger.Value&gt;
                            &lt;Setter TargetName=&quot;PART_img&quot; Property=&quot;DockPanel.Dock&quot; Value=&quot;Left&quot; /&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;DockPanel.Dock&quot; Value=&quot;Right&quot; /&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;Margin&quot; Value=&quot;2,0,0,0&quot; /&gt;
                        &lt;/Trigger&gt;
                        &lt;Trigger Property=&quot;ImageLocation&quot;&gt;
                            &lt;Trigger.Value&gt;
                                &lt;PlacementMode&gt;Top&lt;/PlacementMode&gt;
                            &lt;/Trigger.Value&gt;
                            &lt;Setter TargetName=&quot;PART_img&quot; Property=&quot;DockPanel.Dock&quot; Value=&quot;Top&quot; /&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;DockPanel.Dock&quot; Value=&quot;Bottom&quot; /&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;Margin&quot; Value=&quot;0,2,0,0&quot; /&gt;
                        &lt;/Trigger&gt;
                        &lt;Trigger Property=&quot;ImageLocation&quot;&gt;
                            &lt;Trigger.Value&gt;
                                &lt;PlacementMode&gt;Right&lt;/PlacementMode&gt;
                            &lt;/Trigger.Value&gt;
                            &lt;Setter TargetName=&quot;PART_img&quot; Property=&quot;DockPanel.Dock&quot; Value=&quot;Right&quot; /&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;DockPanel.Dock&quot; Value=&quot;Left&quot; /&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;Margin&quot; Value=&quot;0,0,2,0&quot; /&gt;
                        &lt;/Trigger&gt;
                        &lt;Trigger Property=&quot;ImageLocation&quot;&gt;
                            &lt;Trigger.Value&gt;
                                &lt;PlacementMode&gt;Bottom&lt;/PlacementMode&gt;
                            &lt;/Trigger.Value&gt;
                            &lt;Setter TargetName=&quot;PART_img&quot; Property=&quot;DockPanel.Dock&quot; Value=&quot;Bottom&quot; /&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;DockPanel.Dock&quot; Value=&quot;Top&quot; /&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;Margin&quot; Value=&quot;0,0,0,2&quot; /&gt;
                        &lt;/Trigger&gt;
                        &lt;Trigger Property=&quot;ShowText&quot; Value=&quot;false&quot;&gt;
                            &lt;Setter TargetName=&quot;PART_text&quot; Property=&quot;Visibility&quot; Value=&quot;Collapsed&quot; /&gt;
                        &lt;/Trigger&gt;
                    &lt;/ControlTemplate.Triggers&gt;
                &lt;/ControlTemplate&gt;
            &lt;/Setter.Value&gt;
        &lt;/Setter&gt;
    &lt;/Style&gt;</pre></div></div>

<p>Then in your Window/Page you can use the following to quickly insert a RadioButton which is bound to the AppSettings collection and will persist it&#8217;s state:</p>

<div class="wp_syntax"><div class="code"><pre class="xaml" style="font-family:monospace;">&lt;Window...&gt;
   &lt;Window.Resources&gt;
      &lt;props:Settings x:Key=&quot;Settings&quot; /&gt;
   &lt;/Window.Resources&gt;
   &lt;Grid x:Name=&quot;grdContainer&quot; DataContext=&quot;{StaticResource Settings}&quot;&gt;
       &lt;l49ui:RadioImageButton x:Name=&quot;btnTglRecipes&quot; IsChecked=&quot;{Binding Default.MainWindowDisplayMode,Converter={StaticResource MainWindowDisplayModeConverter},ConverterParameter=Recipes}&quot;
           Image=&quot;pack://application:,,,/Resources/types/burger_24.png&quot; Text=&quot;{lex:LocText ChickenPing.Shared:Strings:Recipes}&quot; Checked=&quot;btnChangeView_Checked&quot; GroupName=&quot;windowdisplay&quot; /&gt;
       &lt;l49ui:RadioImageButton x:Name=&quot;btnTglWines&quot; IsChecked=&quot;{Binding Default.MainWindowDisplayMode,Converter={StaticResource MainWindowDisplayModeConverter},ConverterParameter=Wines}&quot;
           Image=&quot;pack://application:,,,/Resources/images/wine_24.png&quot; Text=&quot;{lex:LocText ChickenPing.Shared:Strings:Wines}&quot; Checked=&quot;btnChangeView_Checked&quot; GroupName=&quot;windowdisplay&quot; /&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> btnChangeView_Checked<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>IsLoaded<span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2011/04/24/441/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checkbox to Select in WPF Datagrid</title>
		<link>http://www.leghumped.com/blog/2011/03/17/checkbox-to-select-in-wpf-datagrid/</link>
		<comments>http://www.leghumped.com/blog/2011/03/17/checkbox-to-select-in-wpf-datagrid/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 13:16:40 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[datagrid]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://www.leghumped.com/blog/?p=433</guid>
		<description><![CDATA[There are several datagrids available for WPF, but in my experience the best free one is the one included in .NET 4 (formerly the WPF toolkit). A fairly common task which I use in dialogs is using a checkbox to allow selection of items. This isn&#8217;t as trivial as it appears with the WPF datagrid, [...]]]></description>
			<content:encoded><![CDATA[<p>There are several datagrids available for WPF, but in my experience the best free one is the one included in .NET 4 (formerly the WPF toolkit). A fairly common task which I use in dialogs is using a checkbox to allow selection of items. This isn&#8217;t as trivial as it appears with the WPF datagrid, but it is possible.</p>
<p>The first thing you need is an extended DataGrid control which includes a handler for the OnPreviewMouseLeftButtonDown event which listens for mouse clicks, then if the clicked cell contains a checkbox, places the cell in edit mode <strong>and</strong> toggles the checkbox.</p>
<p><span id="more-433"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CheckboxDataGrid <span style="color: #008000;">:</span> DataGrid
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> CheckboxDataGrid<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        EventManager<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterClassHandler</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DataGridCell<span style="color: #008000;">&#41;</span>,
            DataGridCell<span style="color: #008000;">.</span><span style="color: #0000FF;">PreviewMouseLeftButtonDownEvent</span>,
            <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPreviewMouseLeftButtonDown</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnPreviewMouseLeftButtonDown<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        DataGridCell cell <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> DataGridCell<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>cell <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>cell<span style="color: #008000;">.</span><span style="color: #0000FF;">IsEditing</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>cell<span style="color: #008000;">.</span><span style="color: #0000FF;">IsReadOnly</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
&nbsp;
                var parentRow <span style="color: #008000;">=</span> cell<span style="color: #008000;">.</span><span style="color: #0000FF;">Parent</span> <span style="color: #0600FF; font-weight: bold;">as</span> DataGridRow<span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>parentRow <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    SelectedIndex <span style="color: #008000;">=</span> parentRow<span style="color: #008000;">.</span><span style="color: #0000FF;">GetIndex</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                CurrentCell <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataGridCellInfo<span style="color: #008000;">&#40;</span>cell<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                BeginEdit<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
         DependencyObject obj <span style="color: #008000;">=</span> FindVisualChild<span style="color: #008000;">&lt;</span>CheckBox<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>cell<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>obj <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Controls</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CheckBox</span> cb <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Controls</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CheckBox</span><span style="color: #008000;">&#41;</span>obj<span style="color: #008000;">;</span>
                cb<span style="color: #008000;">.</span><span style="color: #0000FF;">Focus</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                cb<span style="color: #008000;">.</span><span style="color: #0000FF;">IsChecked</span> <span style="color: #008000;">=</span> <span style="color: #008000;">!</span>cb<span style="color: #008000;">.</span><span style="color: #0000FF;">IsChecked</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> TChild FindVisualChild<span style="color: #008000;">&lt;</span>TChild<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>DependencyObject obj<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> TChild <span style="color: #008000;">:</span> DependencyObject <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> VisualTreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">GetChildrenCount</span><span style="color: #008000;">&#40;</span>obj<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            DependencyObject child <span style="color: #008000;">=</span> VisualTreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">GetChild</span><span style="color: #008000;">&#40;</span>obj, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>child <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> child <span style="color: #008000;">is</span> TChild<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>TChild<span style="color: #008000;">&#41;</span>child<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
                TChild childOfChild <span style="color: #008000;">=</span> FindVisualChild<span style="color: #008000;">&lt;</span>TChild<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>child<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>childOfChild <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> childOfChild<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;locui:CheckboxDataGrid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;dgCategories&quot;</span> <span style="color: #000066;">DockPanel.Dock</span>=<span style="color: #ff0000;">&quot;Bottom&quot;</span> <span style="color: #000066;">AutoGenerateColumns</span>=<span style="color: #ff0000;">&quot;False&quot;</span> <span style="color: #000066;">CanUserAddRows</span>=<span style="color: #ff0000;">&quot;False&quot;</span> <span style="color: #000066;">CanUserDeleteRows</span>=<span style="color: #ff0000;">&quot;False&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000066;">SelectionMode</span>=<span style="color: #ff0000;">&quot;Single&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">SelectionUnit</span>=<span style="color: #ff0000;">&quot;FullRow&quot;</span> <span style="color: #000066;">CanUserSortColumns</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000066;">EnableRowVirtualization</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000066;">GridLinesVisibility</span>=<span style="color: #ff0000;">&quot;Horizontal&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Path=Categories,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}&quot;</span> <span style="color: #000066;">HeadersVisibility</span>=<span style="color: #ff0000;">&quot;Column&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">ScrollViewer.VerticalScrollBarVisibility</span>=<span style="color: #ff0000;">&quot;Auto&quot;</span> <span style="color: #000066;">ScrollViewer.CanContentScroll</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000066;">SelectedValuePath</span>=<span style="color: #ff0000;">&quot;CategoryID&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataGrid.Columns<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataGridTemplateColumn</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;18&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataGridTemplateColumn.CellTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CheckBox</span> <span style="color: #000066;">IsChecked</span>=<span style="color: #ff0000;">&quot;{Binding IsSelected}&quot;</span> <span style="color: #000066;">IsEnabled</span>=<span style="color: #ff0000;">&quot;False&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataGridTemplateColumn.CellTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataGridTemplateColumn.CellEditingTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CheckBox</span> <span style="color: #000066;">IsChecked</span>=<span style="color: #ff0000;">&quot;{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}&quot;</span> <span style="color: #000066;">Checked</span>=<span style="color: #ff0000;">&quot;chkCategories_CheckedStateChanged&quot;</span> <span style="color: #000066;">Unchecked</span>=<span style="color: #ff0000;">&quot;chkCategories_CheckedStateChanged&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataGridTemplateColumn.CellEditingTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataGridTemplateColumn<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><em>IsSelected</em> is a property of the objects to which the grid is bound.
<p>While this gets you 99% of the way there, the only situation it doesn&#8217;t work in is uopdating the last checked item. Eg: If a row is checked, then a button is clicked which closes the dialog, the item won&#8217;t be updated because the cell is still in edit mode. To update the item instantly, the handler for the checkbox&#8217;s Checked and Unchecked event also toggles the selection.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> chkCategories_CheckedStateChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var cat <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>dgCategories<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentItem</span> <span style="color: #0600FF; font-weight: bold;">as</span> SelectableCategory<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var checkBox <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> CheckBox<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>cat <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> checkBox <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        cat<span style="color: #008000;">.</span><span style="color: #0000FF;">IsSelected</span> <span style="color: #008000;">=</span> checkBox<span style="color: #008000;">.</span><span style="color: #0000FF;">IsChecked</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2011/03/17/checkbox-to-select-in-wpf-datagrid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Watermarked TextBox with an AttachedProperty</title>
		<link>http://www.leghumped.com/blog/2010/02/07/watermarked-textbox-with-an-attachedproperty/</link>
		<comments>http://www.leghumped.com/blog/2010/02/07/watermarked-textbox-with-an-attachedproperty/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 12:11:16 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[styles]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://www.leghumped.com/blog/?p=377</guid>
		<description><![CDATA[Ever wanted to create a watermark effect for a TextBox in WPF? Most of the tutorials scattered around have either used static text or created a subclass of TextBox. I wanted to use an AttachedProperty instead, and though it took a while to try every possible combination of brackets and bindings I came up with [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to create a watermark effect for a TextBox in WPF? Most of the tutorials scattered around have either used static text or created a subclass of TextBox. I wanted to use an AttachedProperty instead, and though it took a while to try every possible combination of brackets and bindings I came up with the following to create an AttachedProperty which you can apply in any ResourceDictionary or wherever you keep your Styles.</p>
<p><a href="http://www.leghumped.com/blog/2010/02/07/watermarked-textbox-with-an-attachedproperty/" title="Watermarks with attached properties for WPF"><br />
    <img src="http://images.leghumped.com/wpf_watermarktextbox.png" alt="Watermarks with Attached Properties"/><br />
    <small>Get the code after the jump.</small><br />
</a><br />
<span id="more-377"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> WatermarkedTextBox
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty WatermarkProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAttached</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Watermark&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>WatermarkedTextBox<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetWatermark<span style="color: #008000;">&#40;</span>TextBox element, <span style="color: #6666cc; font-weight: bold;">string</span> watermark<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    element<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>WatermarkProperty, watermark<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetWatermark<span style="color: #008000;">&#40;</span>UIElement element<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> element<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>WatermarkProperty<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>You need to include the namespace for whichever assemly contains the AttachedProperty (<em>l49ui</em> in my case).</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type TextBoxBase}&quot;</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;{x:Type TextBox}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type TextBoxBase}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ScrollViewer</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;PART_ContentHost&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;Transparent&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;watermark&quot;</span> <span style="color: #000066;">Opacity</span>=<span style="color: #ff0000;">&quot;0.3&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding (l49ui:WatermarkedTextBox.Watermark),RelativeSource={RelativeSource TemplatedParent}}&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">TextWrapping</span>=<span style="color: #ff0000;">&quot;Wrap&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">Visibility</span>=<span style="color: #ff0000;">&quot;Collapsed&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate.Triggers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;MultiTrigger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;MultiTrigger.Conditions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Condition</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;IsFocused&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;False&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Condition</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;TextBox.Text&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/MultiTrigger.Conditions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Visibility&quot;</span> <span style="color: #000066;">TargetName</span>=<span style="color: #ff0000;">&quot;watermark&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Visible&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/MultiTrigger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate.Triggers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2010/02/07/watermarked-textbox-with-an-attachedproperty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Minimize To Tray with WPF</title>
		<link>http://www.leghumped.com/blog/2009/12/19/minimize-to-tray-with-wpf/</link>
		<comments>http://www.leghumped.com/blog/2009/12/19/minimize-to-tray-with-wpf/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 12:09:43 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[winforms]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://www.leghumped.com/blog/?p=367</guid>
		<description><![CDATA[I&#8217;ve been working on porting LockCrypt to WPF from Java and was pretty shocked to find there was no no built-in support. It&#8217;s entirely possible, but you have to use WinForms. I based this on Delay&#8216;s sample, feel free to use it however you wish. namespace Lime49.WPF &#123; /// &#60;summary&#62; /// Tray minimization utility, based [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on porting <a href="http://www.lockcrypt.com">LockCrypt</a> to WPF from Java and was pretty shocked to find there was no no built-in support. It&#8217;s entirely possible, but you have to use WinForms. I based this on <em>Delay</em>&#8216;s sample, feel free to use it however you wish.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> Lime49<span style="color: #008000;">.</span><span style="color: #0000FF;">WPF</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Tray minimization utility, based on Delay's code - http://blogs.msdn.com/delay/.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> TrayMinimizer <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Enables &quot;minimize to tray&quot; behavior for the specified Window.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;window&quot;&gt;Window to enable the behavior for.&lt;/param&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> EnableMinimizeToTray<span style="color: #008000;">&#40;</span>Window window<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>MinimizeInstances<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>window<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Minimization already enabled for '{0}'&quot;</span>, window<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
                var instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MinimizeToTrayInstance<span style="color: #008000;">&#40;</span>window<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                instance<span style="color: #008000;">.</span><span style="color: #0000FF;">Enable</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                MinimizeInstances<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>window, instance<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p><span id="more-367"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Disables &quot;minimize to tray&quot; behavior for the specified Window.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;window&quot;&gt;Window to enable the behavior for.&lt;/param&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> DisableMinimizeToTray<span style="color: #008000;">&#40;</span>Window window<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>MinimizeInstances<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>window<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Minimization not enabled for '{0}'&quot;</span>, window<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
                var instance <span style="color: #008000;">=</span> MinimizeInstances<span style="color: #008000;">&#91;</span>window<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
                instance<span style="color: #008000;">.</span><span style="color: #0000FF;">Disable</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                MinimizeInstances<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>window<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Dictionary<span style="color: #008000;">&lt;</span>Window, MinimizeToTrayInstance<span style="color: #008000;">&gt;</span> _minimizeInstances<span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets or sets the windows for which tray minimization is currently enabled.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;value&gt;The windows for which tray minimization is currently enabled.&lt;/value&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Dictionary<span style="color: #008000;">&lt;</span>Window, MinimizeToTrayInstance<span style="color: #008000;">&gt;</span> MinimizeInstances <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>_minimizeInstances <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    _minimizeInstances <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span>Window, MinimizeToTrayInstance<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> _minimizeInstances<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _minimizeInstances <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Allows minimization of a window to the tray.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">class</span> MinimizeToTrayInstance <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">private</span> Window _window<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">private</span> NotifyIcon _notifyIcon<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
            <span style="color: #008080; font-style: italic;">/// Initializes a new instance of the MinimizeToTrayInstance class.</span>
            <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
            <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;window&quot;&gt;Window instance to attach to.&lt;/param&gt;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> MinimizeToTrayInstance<span style="color: #008000;">&#40;</span>Window window<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>window <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;window parameter is null.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                _window <span style="color: #008000;">=</span> window<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
            <span style="color: #008080; font-style: italic;">/// Enables minimization for this Window.</span>
            <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Enable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                _window<span style="color: #008000;">.</span><span style="color: #0000FF;">StateChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>HandleStateChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
            <span style="color: #008080; font-style: italic;">/// Disables minimization for this Window.</span>
            <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Disable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                _window<span style="color: #008000;">.</span><span style="color: #0000FF;">StateChanged</span> <span style="color: #008000;">-=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>HandleStateChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
            <span style="color: #008080; font-style: italic;">/// Handles the Window's StateChanged event.</span>
            <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
            <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;sender&quot;&gt;The sender.&lt;/param&gt;</span>
            <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;e&quot;&gt;The &lt;see cref=&quot;System.EventArgs&quot;/&gt; instance containing the event data.&lt;/param&gt;</span>
            <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> HandleStateChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>_notifyIcon <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    _notifyIcon <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NotifyIcon<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    _notifyIcon<span style="color: #008000;">.</span><span style="color: #0000FF;">Icon</span> <span style="color: #008000;">=</span> Icon<span style="color: #008000;">.</span><span style="color: #0000FF;">ExtractAssociatedIcon</span><span style="color: #008000;">&#40;</span>Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEntryAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Location</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    _notifyIcon<span style="color: #008000;">.</span><span style="color: #0000FF;">MouseClick</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Forms</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MouseEventHandler</span><span style="color: #008000;">&#40;</span>HandleNotifyIconOrBalloonClicked<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    _notifyIcon<span style="color: #008000;">.</span><span style="color: #0000FF;">BalloonTipClicked</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>HandleNotifyIconOrBalloonClicked<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                _notifyIcon<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> _window<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Show/hide Window and NotifyIcon</span>
                var minimized <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>_window<span style="color: #008000;">.</span><span style="color: #0000FF;">WindowState</span> <span style="color: #008000;">==</span> WindowState<span style="color: #008000;">.</span><span style="color: #0000FF;">Minimized</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                _window<span style="color: #008000;">.</span><span style="color: #0000FF;">ShowInTaskbar</span> <span style="color: #008000;">=</span> <span style="color: #008000;">!</span>minimized<span style="color: #008000;">;</span>
                _notifyIcon<span style="color: #008000;">.</span><span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> minimized<span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>minimized <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>Settings<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MinimizeBalloonShown</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">// If this is the first time minimizing to the tray, show the user what happened</span>
                    _notifyIcon<span style="color: #008000;">.</span><span style="color: #0000FF;">ShowBalloonTip</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1000</span>, _window<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span>, <span style="color: #666666;">&quot;I'm still running!&quot;</span>, ToolTipIcon<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    Settings<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MinimizeBalloonShown</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
                    Settings<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
            <span style="color: #008080; font-style: italic;">/// Restores a window when the notify icon or its balloon are clicked.</span>
            <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
            <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;sender&quot;&gt;The sender.&lt;/param&gt;</span>
            <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;e&quot;&gt;The &lt;see cref=&quot;System.EventArgs&quot;/&gt; instance containing the event data.&lt;/param&gt;</span>
            <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> HandleNotifyIconOrBalloonClicked<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                _window<span style="color: #008000;">.</span><span style="color: #0000FF;">WindowState</span> <span style="color: #008000;">=</span> WindowState<span style="color: #008000;">.</span><span style="color: #0000FF;">Normal</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/12/19/minimize-to-tray-with-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPF: TextBox Error Notification Through Style</title>
		<link>http://www.leghumped.com/blog/2009/09/07/wpf-textbox-error-notification-through-style/</link>
		<comments>http://www.leghumped.com/blog/2009/09/07/wpf-textbox-error-notification-through-style/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 08:40:36 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=365</guid>
		<description><![CDATA[By default, WPF doesn&#8217;t provide any visual indication when ValidationRules aren&#8217;t met for TextBoxes. When I was learning WPF, I read a post about providing error notification for TextBoxes through the user of a style. I can&#8217;t find the article/post, but it never worked properly for me so I&#8217;ve modified and fixed what I had. [...]]]></description>
			<content:encoded><![CDATA[<p>By default, WPF doesn&#8217;t provide any visual indication when ValidationRules aren&#8217;t met for TextBoxes. When I was learning WPF, I read a post about providing error notification for TextBoxes through the user of a style. I can&#8217;t find the article/post, but it never worked properly for me so I&#8217;ve modified and fixed what I had. This only works if there are ValidationRules bound to the TextBox, and it only displays the first failed rule.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type TextBoxBase}&quot;</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;{x:Type TextBox}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Margin&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;SnapsToDevicePixels&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;True&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;OverridesDefaultStyle&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;KeyboardNavigation.TabNavigation&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;None&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;FocusVisualStyle&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{x:Null}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;MinWidth&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;80&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;MinHeight&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;AllowDrop&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Background&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{StaticResource WindowBackgroundBrush}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;BorderBrush&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{StaticResource SolidBorderBrush}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
...</pre></div></div>

<p>The rest is after the jump.</p>
<p><span id="more-365"></span></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type TextBoxBase}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Border&quot;</span> <span style="color: #000066;">CornerRadius</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Padding</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Background}&quot;</span> <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;{TemplateBinding BorderBrush}&quot;</span> <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;ErrorContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ScrollViewer</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;PART_ContentHost&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;Transparent&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate.Triggers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Trigger</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;IsEnabled&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;False&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">TargetName</span>=<span style="color: #ff0000;">&quot;Border&quot;</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Background&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{StaticResource DisabledBackgroundBrush}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Foreground&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{StaticResource DisabledForegroundBrush}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Trigger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Trigger</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Validation.HasError&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">TargetName</span>=<span style="color: #ff0000;">&quot;PART_ContentHost&quot;</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;ToolTip&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">TargetName</span>=<span style="color: #ff0000;">&quot;ErrorContainer&quot;</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Padding&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;0,0,18,0&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">TargetName</span>=<span style="color: #ff0000;">&quot;ErrorContainer&quot;</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Background&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ImageBrush</span> <span style="color: #000066;">ImageSource</span>=<span style="color: #ff0000;">&quot;pack://application:,,,/Resources/images/error_16.png&quot;</span> <span style="color: #000066;">Stretch</span>=<span style="color: #ff0000;">&quot;None&quot;</span> <span style="color: #000066;">AlignmentX</span>=<span style="color: #ff0000;">&quot;Right&quot;</span> <span style="color: #000066;">AlignmentY</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Trigger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate.Triggers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/09/07/wpf-textbox-error-notification-through-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPF: Changing Input Focus to Hidden Elements</title>
		<link>http://www.leghumped.com/blog/2009/07/31/wpf-changing-input-focus-to-hidden-elements/</link>
		<comments>http://www.leghumped.com/blog/2009/07/31/wpf-changing-input-focus-to-hidden-elements/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 07:39:13 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=363</guid>
		<description><![CDATA[When trying to change the focused element in the WPF recipe management app I develop, ChickenPing, I recently ran into a problem. I wanted a shortcut key (Ctrl + something) to change the selected tab in a TabControl, then focus an element in that tab. There are a few ways you might try doing this, [...]]]></description>
			<content:encoded><![CDATA[<p>When trying to change the focused element in the WPF recipe management app I develop, ChickenPing, I recently ran into a problem. I wanted a shortcut key (Ctrl + something) to change the  selected tab in a TabControl, then focus an element in that tab. There are a few ways you might try doing this, both do more or less the same thing and both will fail unless the tab in question is already selected. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"> tabControl<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedItem</span> <span style="color: #008000;">=</span> someTab<span style="color: #008000;">;</span>
 someTextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Focus</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 FocusManager<span style="color: #008000;">.</span><span style="color: #0000FF;">SetFocusedElement</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, someTextBox<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008080; font-style: italic;">// both only work unless the tab which contains them is visible.</span></pre></div></div>

<p>The problem is that an element can only be focused if it is visible. Making the item visible just before trying to focus it fails because the change hasn&#8217;t had time to take effect.</p>
<p><span id="more-363"></span></p>
<p>After trying different combinations and trawling, I found a method suggested by <a href="http://www.julmar.com/blog/mark/CommentView,guid,03722678-882a-4bb4-928f-4fe0d35f051e.asp">Mark Smith</a> which used a delegate to wait for an element to load if it was not yet loaded. Taking this idea and using it instead for the IsVisibleChanged event did exactly when I needed.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">tabControl<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedItem</span> <span style="color: #008000;">=</span> someTab<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>someTextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">IsVisible</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    DependencyPropertyChangedEventHandler deferredFocus <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    deferredFocus <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #008000;">&#123;</span>
        someTextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Focus</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        someTextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">IsVisibleChanged</span> <span style="color: #008000;">-=</span> deferredFocus<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    someTextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">IsVisibleChanged</span> <span style="color: #008000;">+=</span> deferredFocus<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
    someTextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Focus</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>No need to call Dispatcher.Invoke and run into complications with cross-thread access, in my tests it works perfectly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/07/31/wpf-changing-input-focus-to-hidden-elements/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TopMost Window In XAML</title>
		<link>http://www.leghumped.com/blog/2009/07/29/topmost-window-in-xaml/</link>
		<comments>http://www.leghumped.com/blog/2009/07/29/topmost-window-in-xaml/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 12:41:48 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[wpf]]></category>
		<category><![CDATA[xaml]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=361</guid>
		<description><![CDATA[I was kind of looking forward to delving into some native call to make a Window always on top in WPF, but it was actually surprisingly easy, a single binding in the Window&#8217;s declaration: &#60;Window ... Topmost=&#34;{Binding ElementName=itmTopMost,Path=IsChecked}&#34; ... &#60;MenuItem Header=&#34;_Always on Top&#34; x:Name=&#34;itmTopMost&#34; IsCheckable=&#34;True&#34; /&#62; No code involved!]]></description>
			<content:encoded><![CDATA[<p>I was kind of looking forward to delving into some native call to make a Window always on top in WPF, but it was actually surprisingly easy, a single binding in the Window&#8217;s declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> ...</span>
<span style="color: #009900;">	<span style="color: #000066;">Topmost</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=itmTopMost,Path=IsChecked}&quot;</span></span>
<span style="color: #009900;">...</span>
<span style="color: #009900;">	<span style="color: #000000; font-weight: bold;">&lt;MenuItem</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;_Always on Top&quot;</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;itmTopMost&quot;</span> <span style="color: #000066;">IsCheckable</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>No code involved!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/07/29/topmost-window-in-xaml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Editable ComboBox with XCeed DataGrid</title>
		<link>http://www.leghumped.com/blog/2009/06/13/editable-combobox-with-xceed-datagrid/</link>
		<comments>http://www.leghumped.com/blog/2009/06/13/editable-combobox-with-xceed-datagrid/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 05:39:49 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[combobox]]></category>
		<category><![CDATA[datagrid]]></category>
		<category><![CDATA[wpf]]></category>
		<category><![CDATA[xceed]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=352</guid>
		<description><![CDATA[I&#8217;ve recently started using XCeed&#8217;s DataGrid for WPF instead of he WPF toolkit DataGrid because overall it seems to have more features. One of the problems which I couldn&#8217;t find any reference to was using editable comboboxes when editing an item. I&#8217;m using an ObjectDataProvider as the ItemsSource for a ComboBox, but I wanted to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently started using <a href="http://xceed.com/Grid_WPF_Intro.html">XCeed&#8217;s DataGrid</a> for WPF instead of he WPF toolkit DataGrid because overall it seems to have more features. One of the problems which I couldn&#8217;t find any reference to was using editable comboboxes when editing an item. I&#8217;m using an ObjectDataProvider as the ItemsSource for a ComboBox, but I wanted to let a user type a value as well as select one from the list.</p>
<p>The solution I eventually used lets a user add an item to the list of items which can then be selected when editing other items in the DataGrid.</p>
<p>Once you&#8217;ve declared the ObjectDataProvider in XAML, you need a CellEditor template for the column which will use the ComboBox (code below).</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xcdg:Column.CellEditor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xcdg:CellEditor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xcdg:CellEditor.EditTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ComboBox</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;cboMeasurement&quot;</span> <span style="color: #000066;">IsVisibleChanged</span>=<span style="color: #ff0000;">&quot;cbo_IsVisibleChanged&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Collection,Source={StaticResource measurementProvider}}&quot;</span> <span style="color: #000066;">SelectedValue</span>=<span style="color: #ff0000;">&quot;{xcdg:CellEditorBinding}&quot;</span> <span style="color: #000066;">IsEditable</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;Black&quot;</span> <span style="color: #000066;">IsSynchronizedWithCurrentItem</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xcdg:CellEditor.EditTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xcdg:CellEditor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xcdg:Column.CellEditor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><span id="more-352"></span><br />
In my example, <em>measurementProvider </em>is an ObjectDataProvider bound to a wrapper class containing a list of objects (of class <em>Ingredient</em>) in the Collection property. The event which adds a new item to the data source fires when the ComboBox loses visibility (because the row is exiting edit mode), then the new item is added with:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Adds a new measurement to the list of available measurements when a new value is typed.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;sender&quot;&gt;The source of the event.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;e&quot;&gt;The &lt;see cref=&quot;System.Windows.DependencyPropertyChangedEventArgs&quot;/&gt; instance containing the event data.&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> cbo_IsVisibleChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    ComboBox cbo <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> ComboBox<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>cbo <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>Measurements<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span>cbo<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> itm <span style="color: #008000;">=</span> cbo<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Measurements<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>itm<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Ingredient currentIngredient <span style="color: #008000;">=</span> dgIngredients<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentItem</span> <span style="color: #0600FF; font-weight: bold;">as</span> Ingredient<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>currentIngredient <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            currentIngredient<span style="color: #008000;">.</span><span style="color: #0000FF;">Measurement</span> <span style="color: #008000;">=</span> itm<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Update 27th September 2009</strong>: This method introduces a bug with XCeed&#8217;s control which means that the first time a row is edited, the change from the ComboBox is not saved. For some reason, the ComboBox is not null, but it&#8217;s Text property is null.</p>
<p>After spending hours trying to start, then cancel an edit, cancelling edits and virtually everything else I used the following (hacky) solution.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> firstEdit <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> InsertionRow_Loaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    insertionRow <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> BetterInsertionRow<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Adds a new measurement to the list of available measurements when a new value is typed.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> cbo_IsVisibleChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>firstEdit<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        firstEdit <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    ComboBox cbo <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> ComboBox<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>cbo <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>Measurements<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span>cbo<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> itm <span style="color: #008000;">=</span> cbo<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Measurements<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>itm<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Ingredient currentIngredient<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>dgIngredients<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentItem</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">// row being edited</span>
            currentIngredient <span style="color: #008000;">=</span> dgIngredients<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentItem</span> <span style="color: #0600FF; font-weight: bold;">as</span> Ingredient<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">// insertionrow is in focus (new item being added)</span>
            currentIngredient <span style="color: #008000;">=</span> insertionRow<span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #0600FF; font-weight: bold;">as</span> Ingredient<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>currentIngredient <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            currentIngredient<span style="color: #008000;">.</span><span style="color: #0000FF;">Measurement</span> <span style="color: #008000;">=</span> itm<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This still doesn&#8217;t fix the problem, and seems to work intermittently, though it&#8217;s better than the default behaviour. The consequence is that the first time a row is edited, if a new item is added to the list, it&#8217;s not saved due to the text still being empty. Subsequent edits <em>seem</em> to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/06/13/editable-combobox-with-xceed-datagrid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPF: DataGrid And The Return Key</title>
		<link>http://www.leghumped.com/blog/2009/05/01/wpf-datagrid-and-the-return-key/</link>
		<comments>http://www.leghumped.com/blog/2009/05/01/wpf-datagrid-and-the-return-key/#comments</comments>
		<pubDate>Fri, 01 May 2009 17:57:47 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[datagrid]]></category>
		<category><![CDATA[toolkit]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=345</guid>
		<description><![CDATA[A decent DataGrid is really the only thing WPF doesn&#8217;t have. The WPF Toolkit DataGrid is a great addition but it&#8217;s still lacking in a few places. To me, it would seem natural for the Tab key to move to the next column and Enter to move to the next row, but there isn&#8217;t really [...]]]></description>
			<content:encoded><![CDATA[<p>A decent DataGrid is really the only thing WPF doesn&#8217;t have. The <a href="http://www.codeplex.com/wpf">WPF Toolkit DataGrid</a> is a great addition but it&#8217;s still lacking in a few places.</p>
<p>To me, it would seem natural for the Tab key to move to the next column and Enter to move to the next row, but there isn&#8217;t really support for this.</p>
<p>After experimenting with KeyUp and KeyDown events, I came up with the following method, based on a similar technique by <a href="http://blogs.msdn.com/vinsibal/archive/2009/04/14/5-more-random-gotchas-with-the-wpf-datagrid.aspx">Vincent Sibal</a>. It makes the DataGrid more usable in my opinion.</p>
<p><span id="more-345"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Focuses the next row when enter or return is pressed</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;sender&quot;&gt;The source of the event.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;e&quot;&gt;The &lt;see cref=&quot;Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs&quot;/&gt; instance containing the event data.&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> dgResources_RowEditEnding<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DataGridRowEditEndingEventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">EditAction</span> <span style="color: #008000;">==</span> DataGridEditAction<span style="color: #008000;">.</span><span style="color: #0000FF;">Commit</span> <span style="color: #008000;">&amp;&amp;</span> dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectionUnit</span> <span style="color: #008000;">==</span> DataGridSelectionUnit<span style="color: #008000;">.</span><span style="color: #0000FF;">Cell</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>Keyboard<span style="color: #008000;">.</span><span style="color: #0000FF;">IsKeyDown</span><span style="color: #008000;">&#40;</span>Key<span style="color: #008000;">.</span><span style="color: #0000FF;">Enter</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> Keyboard<span style="color: #008000;">.</span><span style="color: #0000FF;">IsKeyDown</span><span style="color: #008000;">&#40;</span>Key<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Return</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// get the row and column indexes of the currently selected cell</span>
            <span style="color: #6666cc; font-weight: bold;">int</span> currentColIndex <span style="color: #008000;">=</span> dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span>dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentColumn</span><span style="color: #008000;">&#41;</span>,
                currentRowIndex <span style="color: #008000;">=</span> dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Row</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            var rowToSelect <span style="color: #008000;">=</span> dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">&#91;</span>currentRowIndex <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// get the next row</span>
            var colToSelect <span style="color: #008000;">=</span> dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">&#91;</span>currentColIndex<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">int</span> rowIndex <span style="color: #008000;">=</span> dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span>rowToSelect<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// select the new cell</span>
            dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedCells</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedCells</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> DataGridCellInfo<span style="color: #008000;">&#40;</span>rowToSelect, colToSelect<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Dispatcher</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BeginInvoke</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> DispatcherOperationCallback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>param<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// get the new cell, set focus, then open for edit</span>
                var cell <span style="color: #008000;">=</span> DataGridUtils<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCell</span><span style="color: #008000;">&#40;</span>dgResources, rowIndex, currentColIndex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                cell<span style="color: #008000;">.</span><span style="color: #0000FF;">Focus</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                dgResources<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginEdit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span>, DispatcherPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">Background</span>, <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I also found it difficult to select a control when a Cell enters edit mode, but after vastly overthinking the problem used the Initialized event of the TextBox in the CellEditingTemplate:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Focuses and selects all text in the TextBox used for editing when it is selected.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;sender&quot;&gt;The source of the event.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;e&quot;&gt;The &lt;see cref=&quot;System.EventArgs&quot;/&gt; instance containing the event data.&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> txt_Initialized<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    TextBox tb <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> TextBox<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>tb <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        tb<span style="color: #008000;">.</span><span style="color: #0000FF;">Focus</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        tb<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectAll</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/05/01/wpf-datagrid-and-the-return-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: AutoSave with WPF</title>
		<link>http://www.leghumped.com/blog/2009/04/27/c-autosave-with-wpf/</link>
		<comments>http://www.leghumped.com/blog/2009/04/27/c-autosave-with-wpf/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 07:22:32 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[autosave]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=344</guid>
		<description><![CDATA[Creating an automatic save for ResourceBlender Express was actually a lot easier than I thought it would be but I&#8217;ll post the source here for anyone else wondering how to do it. My .xaml.cs file has four class variables to make it work, and the save method executes in a separate thread to prevent it [...]]]></description>
			<content:encoded><![CDATA[<p>Creating an automatic save for <a href="http://www.resourceblender.com/">ResourceBlender Express</a> was actually a lot easier than I thought it would be but I&#8217;ll post the source here for anyone else wondering how to do it.</p>
<p>My .xaml.cs file has four class variables to make it work, and the save method executes in a separate thread to prevent it locking the UI during saving (actually a BackgroundWorker).</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">BackgroundWorker worker<span style="color: #008000;">;</span>
DispatcherTimer timer<span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">int</span> autoSaveInterval <span style="color: #008000;">=</span> <span style="color: #FF0000;">3000</span><span style="color: #008000;">;</span>
DateTime lastSave<span style="color: #008000;">;</span></pre></div></div>

<p>The DispatcherTimer times the save at a specified interval and the BackgroundWorker executes the operation. In the class constructor I setup the DispatchTimer:</p>
<p><span id="more-344"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> WinEditRbt<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #008000;">...</span>
	<span style="color: #0000FF;">timer</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DispatcherTimer<span style="color: #008000;">&#40;</span>TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromMilliseconds</span><span style="color: #008000;">&#40;</span>autoSaveInterval<span style="color: #008000;">&#41;</span>, DispatcherPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">Background</span>, <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>DoAutoSave<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Dispatcher</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I wanted to allow the user to save manually regardless of whether an autosave had just executed, but prevent autosaving if the user had just recently saved, so the DoWork delegate checks to see when the save method last executed before saving.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Save<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ExecutedRoutedEventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>worker <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> worker<span style="color: #008000;">.</span><span style="color: #0000FF;">IsBusy</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>worker<span style="color: #008000;">.</span><span style="color: #0000FF;">IsBusy</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            Dispatcher<span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span>DispatcherPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">Background</span>, <span style="color: #008000;">new</span> ThreadStart<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">...</span>
    <span style="color: #0000FF;">worker</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DoWork</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, dwe<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span>
        DateTime dtStart <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span> <span style="color: #008000;">-</span> dtStart<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TotalSeconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">// give the user chance to see the message</span>
            worker<span style="color: #008000;">.</span><span style="color: #0000FF;">ReportProgress</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #666666;">&quot;Save Complete&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Lambdas help minimize the amount of method garbage in the class by reducing the number of methods to one per worker task instead of three (ProgressChanged, DoWork and RunWorkerCompleted). To manually save, you just need to trigger the ApplicationCommands.Save command.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> CanExecuteSave<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, CanExecuteRoutedEventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">CanExecute</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsLoaded</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>worker <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> <span style="color: #008000;">!</span>worker<span style="color: #008000;">.</span><span style="color: #0000FF;">IsBusy</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoAutoSave<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>worker <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span>worker <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>worker<span style="color: #008000;">.</span><span style="color: #0000FF;">IsBusy</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Subtract</span><span style="color: #008000;">&#40;</span>lastSave<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TotalMilliseconds</span> <span style="color: #008000;">&gt;=</span> autoSaveInterval<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        ApplicationCommands<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/04/27/c-autosave-with-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

