<?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>Sun, 30 May 2010 14:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>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;">public</span> <span style="color: #FF0000;">class</span> WatermarkedTextBox
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> DependencyProperty WatermarkProperty <span style="color: #008000;">=</span> DependencyProperty.<span style="color: #0000FF;">RegisterAttached</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Watermark&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>WatermarkedTextBox<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> SetWatermark<span style="color: #000000;">&#40;</span>TextBox element, <span style="color: #FF0000;">string</span> watermark<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    element.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>WatermarkProperty, watermark<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> GetWatermark<span style="color: #000000;">&#40;</span>UIElement element<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> element.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>WatermarkProperty<span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> string<span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">namespace</span> Lime49.<span style="color: #0000FF;">WPF</span> <span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> TrayMinimizer <span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> EnableMinimizeToTray<span style="color: #000000;">&#40;</span>Window window<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>MinimizeInstances.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span>window<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Minimization already enabled for '{0}'&quot;</span>, window.<span style="color: #0000FF;">Title</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
                var instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MinimizeToTrayInstance<span style="color: #000000;">&#40;</span>window<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                instance.<span style="color: #0000FF;">Enable</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                MinimizeInstances.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>window, instance<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> DisableMinimizeToTray<span style="color: #000000;">&#40;</span>Window window<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>MinimizeInstances.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span>window<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Minimization not enabled for '{0}'&quot;</span>, window.<span style="color: #0000FF;">Title</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
                var instance <span style="color: #008000;">=</span> MinimizeInstances<span style="color: #000000;">&#91;</span>window<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                instance.<span style="color: #0000FF;">Disable</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                MinimizeInstances.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>window<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">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;">private</span> <span style="color: #0600FF;">static</span> Dictionary<span style="color: #008000;">&lt;</span>Window, MinimizeToTrayInstance<span style="color: #008000;">&gt;</span> MinimizeInstances <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>_minimizeInstances <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#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: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">return</span> _minimizeInstances<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _minimizeInstances <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#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;">private</span> <span style="color: #FF0000;">class</span> MinimizeToTrayInstance <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">private</span> Window _window<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">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;">public</span> MinimizeToTrayInstance<span style="color: #000000;">&#40;</span>Window window<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>window <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;window parameter is null.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                _window <span style="color: #008000;">=</span> window<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">void</span> Enable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                _window.<span style="color: #0000FF;">StateChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #000000;">&#40;</span>HandleStateChanged<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">void</span> Disable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                _window.<span style="color: #0000FF;">StateChanged</span> <span style="color: #008000;">-=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #000000;">&#40;</span>HandleStateChanged<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#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;">private</span> <span style="color: #0600FF;">void</span> HandleStateChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>_notifyIcon <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                    _notifyIcon <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NotifyIcon<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    _notifyIcon.<span style="color: #0000FF;">Icon</span> <span style="color: #008000;">=</span> Icon.<span style="color: #0000FF;">ExtractAssociatedIcon</span><span style="color: #000000;">&#40;</span>Assembly.<span style="color: #0000FF;">GetEntryAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Location</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    _notifyIcon.<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: #0000FF;">Windows</span>.<span style="color: #0000FF;">Forms</span>.<span style="color: #0000FF;">MouseEventHandler</span><span style="color: #000000;">&#40;</span>HandleNotifyIconOrBalloonClicked<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    _notifyIcon.<span style="color: #0000FF;">BalloonTipClicked</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #000000;">&#40;</span>HandleNotifyIconOrBalloonClicked<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                _notifyIcon.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> _window.<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: #000000;">&#40;</span>_window.<span style="color: #0000FF;">WindowState</span> <span style="color: #008000;">==</span> WindowState.<span style="color: #0000FF;">Minimized</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                _window.<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: #0000FF;">Visible</span> <span style="color: #008000;">=</span> minimized<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>minimized <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>Settings.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">MinimizeBalloonShown</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#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: #0000FF;">ShowBalloonTip</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1000</span>, _window.<span style="color: #0000FF;">Title</span>, <span style="color: #666666;">&quot;I'm still running!&quot;</span>, ToolTipIcon.<span style="color: #0000FF;">None</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    Settings.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">MinimizeBalloonShown</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                    Settings.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">Save</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#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;">private</span> <span style="color: #0600FF;">void</span> HandleNotifyIconOrBalloonClicked<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                _window.<span style="color: #0000FF;">WindowState</span> <span style="color: #008000;">=</span> WindowState.<span style="color: #0000FF;">Normal</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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: #0000FF;">SelectedItem</span> <span style="color: #008000;">=</span> someTab<span style="color: #008000;">;</span>
 someTextBox.<span style="color: #0000FF;">Focus</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 FocusManager.<span style="color: #0000FF;">SetFocusedElement</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, someTextBox<span style="color: #000000;">&#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: #0000FF;">SelectedItem</span> <span style="color: #008000;">=</span> someTab<span style="color: #008000;">;</span>
<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>someTextBox.<span style="color: #0000FF;">IsVisible</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    DependencyPropertyChangedEventHandler deferredFocus <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
    deferredFocus <span style="color: #008000;">=</span> <span style="color: #FF0000;">delegate</span> <span style="color: #000000;">&#123;</span>
        someTextBox.<span style="color: #0000FF;">Focus</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        someTextBox.<span style="color: #0000FF;">IsVisibleChanged</span> <span style="color: #008000;">-=</span> deferredFocus<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    someTextBox.<span style="color: #0000FF;">IsVisibleChanged</span> <span style="color: #008000;">+=</span> deferredFocus<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
    someTextBox.<span style="color: #0000FF;">Focus</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#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;">private</span> <span style="color: #0600FF;">void</span> cbo_IsVisibleChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, DependencyPropertyChangedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    ComboBox cbo <span style="color: #008000;">=</span> sender <span style="color: #0600FF;">as</span> ComboBox<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>cbo <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>Measurements.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>cbo.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> itm <span style="color: #008000;">=</span> cbo.<span style="color: #0000FF;">Text</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Measurements.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>itm<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Ingredient currentIngredient <span style="color: #008000;">=</span> dgIngredients.<span style="color: #0000FF;">CurrentItem</span> <span style="color: #0600FF;">as</span> Ingredient<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>currentIngredient <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            currentIngredient.<span style="color: #0000FF;">Measurement</span> <span style="color: #008000;">=</span> itm<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">private</span> <span style="color: #FF0000;">bool</span> firstEdit <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> InsertionRow_Loaded<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, RoutedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    insertionRow <span style="color: #008000;">=</span> sender <span style="color: #0600FF;">as</span> BetterInsertionRow<span style="color: #008000;">;</span>
<span style="color: #000000;">&#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;">private</span> <span style="color: #0600FF;">void</span> cbo_IsVisibleChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, DependencyPropertyChangedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>firstEdit<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        firstEdit <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
        return<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    ComboBox cbo <span style="color: #008000;">=</span> sender <span style="color: #0600FF;">as</span> ComboBox<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>cbo <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>Measurements.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>cbo.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> itm <span style="color: #008000;">=</span> cbo.<span style="color: #0000FF;">Text</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Measurements.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>itm<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Ingredient currentIngredient<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>dgIngredients.<span style="color: #0000FF;">CurrentItem</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// row being edited</span>
            currentIngredient <span style="color: #008000;">=</span> dgIngredients.<span style="color: #0000FF;">CurrentItem</span> <span style="color: #0600FF;">as</span> Ingredient<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#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: #0000FF;">DataContext</span> <span style="color: #0600FF;">as</span> Ingredient<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>currentIngredient <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            currentIngredient.<span style="color: #0000FF;">Measurement</span> <span style="color: #008000;">=</span> itm<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">private</span> <span style="color: #0600FF;">void</span> dgResources_RowEditEnding<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, DataGridRowEditEndingEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">EditAction</span> <span style="color: #008000;">==</span> DataGridEditAction.<span style="color: #0000FF;">Commit</span> <span style="color: #008000;">&amp;&amp;</span> dgResources.<span style="color: #0000FF;">SelectionUnit</span> <span style="color: #008000;">==</span> DataGridSelectionUnit.<span style="color: #0000FF;">Cell</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>Keyboard.<span style="color: #0000FF;">IsKeyDown</span><span style="color: #000000;">&#40;</span>Key.<span style="color: #0000FF;">Enter</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span> Keyboard.<span style="color: #0000FF;">IsKeyDown</span><span style="color: #000000;">&#40;</span>Key.<span style="color: #0600FF;">Return</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// get the row and column indexes of the currently selected cell</span>
            <span style="color: #FF0000;">int</span> currentColIndex <span style="color: #008000;">=</span> dgResources.<span style="color: #0000FF;">Columns</span>.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span>dgResources.<span style="color: #0000FF;">CurrentColumn</span><span style="color: #000000;">&#41;</span>,
                currentRowIndex <span style="color: #008000;">=</span> dgResources.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Row</span>.<span style="color: #0000FF;">Item</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            var rowToSelect <span style="color: #008000;">=</span> dgResources.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#91;</span>currentRowIndex <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#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: #0000FF;">Columns</span><span style="color: #000000;">&#91;</span>currentColIndex<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> rowIndex <span style="color: #008000;">=</span> dgResources.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span>rowToSelect<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// select the new cell</span>
            dgResources.<span style="color: #0000FF;">SelectedCells</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            dgResources.<span style="color: #0000FF;">SelectedCells</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> DataGridCellInfo<span style="color: #000000;">&#40;</span>rowToSelect, colToSelect<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Dispatcher</span>.<span style="color: #0000FF;">BeginInvoke</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> DispatcherOperationCallback<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>param<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#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: #0000FF;">GetCell</span><span style="color: #000000;">&#40;</span>dgResources, rowIndex, currentColIndex<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                cell.<span style="color: #0000FF;">Focus</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                dgResources.<span style="color: #0000FF;">BeginEdit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>, DispatcherPriority.<span style="color: #0000FF;">Background</span>, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">null</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">private</span> <span style="color: #0600FF;">void</span> txt_Initialized<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    TextBox tb <span style="color: #008000;">=</span> sender <span style="color: #0600FF;">as</span> TextBox<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>tb <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        tb.<span style="color: #0000FF;">Focus</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        tb.<span style="color: #0000FF;">SelectAll</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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: #FF0000;">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;">public</span> WinEditRbt<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	...
	<span style="color: #0000FF;">timer</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DispatcherTimer<span style="color: #000000;">&#40;</span>TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>autoSaveInterval<span style="color: #000000;">&#41;</span>, DispatcherPriority.<span style="color: #0000FF;">Background</span>, <span style="color: #008000;">new</span> EventHandler<span style="color: #000000;">&#40;</span>DoAutoSave<span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Dispatcher</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#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;">private</span> <span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, ExecutedRoutedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>worker <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> worker.<span style="color: #0000FF;">IsBusy</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>worker.<span style="color: #0000FF;">IsBusy</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            Dispatcher.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>DispatcherPriority.<span style="color: #0000FF;">Background</span>, <span style="color: #008000;">new</span> ThreadStart<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    ...
    <span style="color: #0000FF;">worker</span>.<span style="color: #0000FF;">DoWork</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span>s, dwe<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span>
        DateTime dtStart <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DateTime.<span style="color: #0000FF;">Now</span> <span style="color: #008000;">-</span> dtStart<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">TotalSeconds</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// give the user chance to see the message</span>
            worker.<span style="color: #0000FF;">ReportProgress</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #666666;">&quot;Save Complete&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#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;">private</span> <span style="color: #0600FF;">void</span> CanExecuteSave<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, CanExecuteRoutedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    e.<span style="color: #0000FF;">CanExecute</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">IsLoaded</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span>worker <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">||</span> <span style="color: #008000;">!</span>worker.<span style="color: #0000FF;">IsBusy</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> DoAutoSave<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>worker <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">||</span> <span style="color: #000000;">&#40;</span>worker <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>worker.<span style="color: #0000FF;">IsBusy</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">Subtract</span><span style="color: #000000;">&#40;</span>lastSave<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">TotalMilliseconds</span> <span style="color: #008000;">&gt;=</span> autoSaveInterval<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        ApplicationCommands.<span style="color: #0000FF;">Save</span>.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span>, <span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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>
		<item>
		<title>WPF: GridView Column Width Calculator</title>
		<link>http://www.leghumped.com/blog/2009/03/11/wpf-gridview-column-width-calculator/</link>
		<comments>http://www.leghumped.com/blog/2009/03/11/wpf-gridview-column-width-calculator/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 12:43:52 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[listview]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=340</guid>
		<description><![CDATA[The GridView in WPF isn&#8217;t bad, but is pretty basic compared to the one in ASP.NET. There&#8217;s isn&#8217;t really a decent method for setting column width, so I wrote this converter to calculate the width needed to fill the parent ListView. If you call it with no parameters, it&#8217;ll take up the remaining width in [...]]]></description>
			<content:encoded><![CDATA[<p>The GridView in WPF isn&#8217;t bad, but is pretty basic compared to the one in ASP.NET. There&#8217;s isn&#8217;t really a decent method for setting column width, so I wrote this converter to calculate the width needed to fill the parent ListView.</p>
<p>If you call it with no parameters, it&#8217;ll take up the remaining width in the ListView:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// fills remaining width in the ListView</span>
<span style="color: #008000;">&lt;</span>GridViewColumn Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter}}&quot;</span><span style="color: #008000;">&gt;</span></pre></div></div>

<p>If you use an integer as a parameter, the value will act as the minimum width</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// fills remaining width in the ListView, unless the remaining width is less than the parameter</span>
<span style="color: #008000;">&lt;</span>GridViewColumn Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter},ConverterParameter=200}&quot;</span><span style="color: #008000;">&gt;</span></pre></div></div>

<p>Or, you can specify a GridView type width with an asterisk, and the percentage width of the ListView will be returned</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// calculates 30% of the ListView width</span>
<span style="color: #008000;">&lt;</span>GridViewColumn Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter},ConverterParameter=0.3*}&quot;</span><span style="color: #008000;">&gt;</span></pre></div></div>

<p><span id="more-340"></span><br />
<a href="http://www.leghumped.com/WidthConverter.cs" class="rmore">Download Source</a></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;">/// Calculates the column width required to fill the view in a GridView</span>
<span style="color: #008080; font-style: italic;">/// For usage examples, see http://leghumped.com/blog/2009/03/11/wpf-gridview-column-width-calculator/</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WidthConverter <span style="color: #008000;">:</span> IValueConverter <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Converts the specified value.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;value&quot;&gt;The parent Listview.&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;type&quot;&gt;The type.&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;parameter&quot;&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If no parameter is given, the remaning with will be returned.</span>
    <span style="color: #008080; font-style: italic;">/// If the parameter is an integer acts as MinimumWidth, the remaining with will be returned only if it's greater than the parameter</span>
    <span style="color: #008080; font-style: italic;">/// If the parameter is anything else, it's taken to be a percentage. Eg: 0.3* = 30%, 0.15* = 15%</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;culture&quot;&gt;The culture.&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;The width, as calculated by the parameter given&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> Convert<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> value, Type type, <span style="color: #FF0000;">object</span> parameter, CultureInfo culture<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
        ListView listView <span style="color: #008000;">=</span> value <span style="color: #0600FF;">as</span> ListView<span style="color: #008000;">;</span>
        GridView grdView <span style="color: #008000;">=</span> listView.<span style="color: #0000FF;">View</span> <span style="color: #0600FF;">as</span> GridView<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> minWidth <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">bool</span> widthIsPercentage <span style="color: #008000;">=</span> parameter <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span><span style="color: #FF0000;">int</span>.<span style="color: #0000FF;">TryParse</span><span style="color: #000000;">&#40;</span>parameter.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">out</span> minWidth<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>widthIsPercentage<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> widthParam <span style="color: #008000;">=</span> parameter.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">double</span> percentage <span style="color: #008000;">=</span> <span style="color: #FF0000;">double</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>widthParam.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, widthParam.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> listView.<span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">*</span> percentage<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">double</span> total <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">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> grdView.<span style="color: #0000FF;">Columns</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                total <span style="color: #008000;">+=</span> grdView.<span style="color: #0000FF;">Columns</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ActualWidth</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #FF0000;">double</span> remainingWidth <span style="color: #008000;">=</span> listView.<span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">-</span> total<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>remainingWidth <span style="color: #008000;">&gt;</span> minWidth<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// fill the remaining width in the ListView</span>
                <span style="color: #0600FF;">return</span> remainingWidth<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// fill remaining space with MinWidth</span>
                <span style="color: #0600FF;">return</span> minWidth<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>            
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> ConvertBack<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> o, Type type, <span style="color: #FF0000;">object</span> parameter, CultureInfo culture<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/03/11/wpf-gridview-column-width-calculator/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
