<?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;  &#187; Leghumped</title>
	<atom:link href="http://www.leghumped.com/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>Mon, 08 Mar 2010 20:33:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Entity Framework in Mednium Trust</title>
		<link>http://www.leghumped.com/blog/2010/03/08/entity-framework-in-mednium-trust/</link>
		<comments>http://www.leghumped.com/blog/2010/03/08/entity-framework-in-mednium-trust/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 18:47:56 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[entity framework]]></category>

		<guid isPermaLink="false">http://www.leghumped.com/blog/?p=403</guid>
		<description><![CDATA[ASP.NET and medium trust typically don&#8217;t get along well once you start using third party libraries. It really should, however, be easier to get the Entity Framework working on medium trust, and this had me baffled for a few hours trying to split the EDMX file and other fruitless solutions.
To get this working I needed [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET and medium trust typically don&#8217;t get along well once you start using third party libraries. It really should, however, be easier to get the Entity Framework working on medium trust, and this had me baffled for a few hours trying to split the EDMX file and other fruitless solutions.</p>
<p>To get this working I needed to:</p>
<ol>
<li>Move the EDMX to a separate assembly.</li>
<li>Set the EDMX metadata artifact processing to <em>&#8216;Embed in output assembly&#8217;</em>.</li>
<li>Add a reference in the site.</li>
<li>Change the connection string to

<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;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ShopEntities&quot;</span> <span style="color: #000066;">connectionString</span>=<span style="color: #ff0000;">&quot;metadata=res://*/;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=servername;Initial Catalog=dbname;User Id=username;Password=P@55word;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #000066;">providerName</span>=<span style="color: #ff0000;">&quot;System.Data.EntityClient&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

</li>
</ol>
<p>Which every guide claimed would work. There&#8217;s one more step if you have custom partial entity classes (in App_Code). If so, these also need to be moved to your output assembly, as you can&#8217;t split partial classes across assemblies. Lastly, change the namespace in your partial classes to the same as your output assembly, and everything should work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2010/03/08/entity-framework-in-mednium-trust/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Watermarked TextBox with an AttachedProperty</title>
		<link>http://www.leghumped.com/blog/2010/02/07/watermarked-textbox-with-an-attachedproperty/</link>
		<comments>http://www.leghumped.com/blog/2010/02/07/watermarked-textbox-with-an-attachedproperty/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 12:11:16 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[styles]]></category>
		<category><![CDATA[wpf]]></category>

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

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">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>Creating query strings with LINQ</title>
		<link>http://www.leghumped.com/blog/2010/01/30/creating-query-strings-with-linq/</link>
		<comments>http://www.leghumped.com/blog/2010/01/30/creating-query-strings-with-linq/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 06:32:54 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://www.leghumped.com/blog/?p=372</guid>
		<description><![CDATA[While implementing a recipe search API for ChickenPing, I&#8217;ve just spotted the following hilariously verbose piece of code which concatenates parameters as part of a GET request.

StringBuilder buffer = new StringBuilder&#40;&#41;;
bool first = true;
foreach&#40;KeyValuePair&#60;string, string&#62; parameter in parameters&#41; &#123;
    if&#40;first&#41; &#123;
        first = false;
  [...]]]></description>
			<content:encoded><![CDATA[<p>While implementing a recipe search API for ChickenPing, I&#8217;ve just spotted the following hilariously verbose piece of code which concatenates parameters as part of a GET request.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">StringBuilder buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">bool</span> first <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>KeyValuePair<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> parameter <span style="color: #0600FF;">in</span> parameters<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>first<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        first <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
        buffer.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'&amp;'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>encodeParams<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        buffer.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0}={1}&quot;</span>, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Uri</span>.<span style="color: #0000FF;">EscapeDataString</span><span style="color: #000000;">&#40;</span>parameter.<span style="color: #0000FF;">Key</span><span style="color: #000000;">&#41;</span>, Uri.<span style="color: #0000FF;">EscapeDataString</span><span style="color: #000000;">&#40;</span>parameter.<span style="color: #0000FF;">Value</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>
        buffer.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0}={1}&quot;</span>, parameter.<span style="color: #0000FF;">Key</span>, parameter.<span style="color: #0000FF;">Value</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: #FF0000;">string</span> queryString <span style="color: #008000;">=</span> buffer.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The worst thing is I only wrote this about six months ago. I simplified it to the following with LINQ:</p>
<p><span id="more-372"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> queryString <span style="color: #008000;">=</span> parameters.<span style="color: #0000FF;">Aggregate</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span>, <span style="color: #000000;">&#40;</span>allParameters, param<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> allParameters <span style="color: #008000;">+</span> encodeParams <span style="color: #008000;">?</span> 
                <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0}={1}&quot;</span>, Uri.<span style="color: #0000FF;">EscapeDataString</span><span style="color: #000000;">&#40;</span>param.<span style="color: #0000FF;">Key</span><span style="color: #000000;">&#41;</span>, Uri.<span style="color: #0000FF;">EscapeDataString</span><span style="color: #000000;">&#40;</span>param.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span>
                <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0}={1}&quot;</span>, param.<span style="color: #0000FF;">Key</span>, param.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>One less skeleton in ChickenPing&#8217;s closet and 12 fewer lines of source code!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2010/01/30/creating-query-strings-with-linq/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&#8217;s sample, feel free to use it however you wish.

namespace Lime49.WPF &#123;
    /// &#60;summary&#62;
    [...]]]></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>&#8217;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 [...]]]></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>ASP.NET: Remove from the cache using</title>
		<link>http://www.leghumped.com/blog/2009/07/25/asp-net-remove-from-the-cache-using/</link>
		<comments>http://www.leghumped.com/blog/2009/07/25/asp-net-remove-from-the-cache-using/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 10:09:50 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=360</guid>
		<description><![CDATA[<p>The Cache object in ASP.NET isn't like other collections, probably because it needs to be specialized so it doesn't make use of generics. Unfortunately, that makes it more difficult to work with. This small utility method removes an object from the cache by key using a predictate. You could easily modify it to compare either of the IDictionaryEnumerator's Key or Value properties.</p>
<pre lang="csharp">
/// <summary>
/// Removes an item from the cache using a Predictate to match the key.
/// </summary>
/// <param name="keyCriteria">The Predictate to use for each key to determine whether the entry should be removed.</param>
/// <summary>
/// Removes an item from the cache using a Predictate to match the key.
/// </summary>
/// <param name="keyCriteria">The Predictate to use for each key to determine whether the entry should be removed.</param>
</pre>]]></description>
			<content:encoded><![CDATA[<p>The Cache object in ASP.NET isn&#8217;t like other collections, probably because it needs to be specialized so it doesn&#8217;t make use of generics. Unfortunately, that makes it more difficult to work with. This small utility method removes an object from the cache by key using a predictate. You could easily modify it to compare either of the IDictionaryEnumerator&#8217;s Key or Value properties.</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;">/// Removes an item from the cache using a Predictate to match the key.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;keyCriteria&quot;&gt;The Predictate to use for each key to determine whether the entry should be removed.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Removes an item from the cache using a Predictate to match the key.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;keyCriteria&quot;&gt;The Predictate to use for each key to determine whether the entry should be removed.&lt;/param&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RemoveFromCache<span style="color: #000000;">&#40;</span>Predicate<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> keyCriteria<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	IDictionaryEnumerator enuma <span style="color: #008000;">=</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>enuma.<span style="color: #0000FF;">MoveNext</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</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>keyCriteria<span style="color: #000000;">&#40;</span>enuma.<span style="color: #0000FF;">Key</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>enuma.<span style="color: #0000FF;">Key</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</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: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/07/25/asp-net-remove-from-the-cache-using/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET: Data Binding with ObjectDataSource</title>
		<link>http://www.leghumped.com/blog/2009/07/09/asp-net-data-binding-with-objectdatasource/</link>
		<comments>http://www.leghumped.com/blog/2009/07/09/asp-net-data-binding-with-objectdatasource/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 19:38:57 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[databinding]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=358</guid>
		<description><![CDATA[In the past when writing forms with ASP.NET I&#8217;ve usually used an SqlDataSource when binding to controls, but I&#8217;ve been doing a lot of WPF lately. Being a desktop platform, WPF has excellent support for DataBinding, but obviously since HTTP is a stateless protocol (apart from the ViewState in ASP.NET which isn&#8217;t ideal), it&#8217;s not [...]]]></description>
			<content:encoded><![CDATA[<p>In the past when writing forms with ASP.NET I&#8217;ve usually used an SqlDataSource when binding to controls, but I&#8217;ve been doing a lot of WPF lately. Being a desktop platform, WPF has excellent support for DataBinding, but obviously since HTTP is a stateless protocol (apart from the ViewState in ASP.NET which isn&#8217;t ideal), it&#8217;s not so well suited to data binding.</p>
<p>One thing I&#8217;ve never really tried is binding an object to a form to display a single item (eg: a report). This leads to writing lots of tedious code like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	Order theOrder <span style="color: #008000;">=</span> GetOrder<span style="color: #000000;">&#40;</span>orderId<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	litName.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> theOrder.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
	litID.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> theOrder.<span style="color: #0000FF;">ID</span><span style="color: #008000;">;</span>
	litCustomer.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> theOrder.<span style="color: #0000FF;">BillingCustomer</span>.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
	...</pre></div></div>

<p><span id="more-358"></span></p>
<p>It never occured to me to use an ObjectDataSource with a Repeater, just returning one row. Not only does it keep all the code in one place instead of superflous bits in the code-behind, it makes it easier to see which properties on the object are being used.</p>
<p>So instead of the above, you end up with:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;">&lt;asp:ObjectDataSource ID=&quot;dsOrderDetails&quot; runat=&quot;server&quot; TypeName=&quot;DatabaseHelper&quot; SelectMethod=&quot;GetOrder&quot;&gt;
	&lt;SelectParameters&gt;
		&lt;asp:Parameter Name=&quot;orderId&quot; Type=&quot;Int32&quot; DefaultValue=&quot;-1&quot; /&gt;
	&lt;/SelectParameters&gt;
&lt;/asp:ObjectDataSource&gt;
&lt;asp:Repeater ID=&quot;rptOrder&quot; runat=&quot;server&quot; DataSourceID=&quot;dsOrderDetails&quot;&gt;
	&lt;ItemTemplate&gt;
		&lt;strong&gt;Order ID: &lt;/strong&gt;&lt;asp:Literal ID=&quot;lblOrderID&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Eval<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;ID&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>' /&gt;
		&lt;strong&gt;Checkout Complete: &lt;/strong&gt;&lt;asp:Literal ID=&quot;lblPaid&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Convert.<span style="color: #9900cc;">ToBoolean</span><span style="color: #006600; font-weight:bold;">&#40;</span>Eval<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Paid&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">??</span> <span style="color: #cc0000;">&quot;false&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">?</span> <span style="color: #cc0000;">&quot;Yes&quot;</span> <span style="color: #006600; font-weight: bold;">:</span> <span style="color: #cc0000;">&quot;No&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>' /&gt;
		&lt;strong&gt;Customer Name: &lt;/strong&gt;&lt;asp:Literal ID=&quot;lblCustomerName&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Eval<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;BillingCustomer.Name&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>' /&gt;
                            &lt;/div&gt;
		...
		&lt;asp:GridView ID=&quot;grdItems&quot; runat=&quot;server&quot; DataSource='<span style="color: #000000; font-weight: bold;">&lt;%</span># Eval<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Items&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'...</pre></div></div>

<p>Just to put those properties into context, the object might have a structure like this:</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> Order
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> ID <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Paid <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> Customer BillingCustomer <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>OrderItem<span style="color: #008000;">&gt;</span> Items <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	...</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/07/09/asp-net-data-binding-with-objectdatasource/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Faster Image Loading With jQuery</title>
		<link>http://www.leghumped.com/blog/2009/07/05/faster-image-loading-with-jquery/</link>
		<comments>http://www.leghumped.com/blog/2009/07/05/faster-image-loading-with-jquery/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 06:35:13 +0000</pubDate>
		<dc:creator>Harry Jennerway</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://leghumped.com/blog/?p=357</guid>
		<description><![CDATA[Normally, if there are images on a webpage they&#8217;re there to support the content and not excessive, so you don&#8217;t have to worry about loading times. What if you need to have an undefined number though, and they happen to be high resolution like a gallery?
For the last site I worked on, the client wanted [...]]]></description>
			<content:encoded><![CDATA[<p>Normally, if there are images on a webpage they&#8217;re there to support the content and not excessive, so you don&#8217;t have to worry about loading times. What if you need to have an undefined number though, and they happen to be high resolution like a gallery?</p>
<p>For the last site I worked on, the client wanted a gallery of 400&#215;400 pixel images to cycle in a gallery on the front page of the site. They&#8217;re about 70KB each so if you&#8217;re loading 10 or more of them in the middle of the page, they&#8217;ll slow the page load down until they&#8217;ve all totally loaded.</p>
<p>The solution I used was to add the images to the DOM <em>after</em> the rest of the content loads.</p>
<p><span id="more-357"></span></p>
<p>The site was written in ASP.NET, but I used jQuery so it applies to any project.</p>
<p>First, you need to render a list of the images to load into a variable which JavaScript can access. Ie:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">...
<span style="color: #0000FF;">cmd</span>.<span style="color: #0000FF;">CommandText</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;SELECT Filename, Description FROM tblGallery WHERE Visible = '1'&quot;</span><span style="color: #008000;">;</span>
StringBuilder jqueryBuffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;$().ready(function() {<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
jqueryBuffer.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;var galleryImages = [<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> galleryPics <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
SqlDataReader reader <span style="color: #008000;">=</span> cmd.<span style="color: #0000FF;">ExecuteReader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
JavaScriptSerializer js <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> JavaScriptSerializer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>reader.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    galleryPics.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>js.<span style="color: #0000FF;">Serialize</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span> 
        filename <span style="color: #008000;">=</span> Server.<span style="color: #0000FF;">HtmlDecode</span><span style="color: #000000;">&#40;</span>reader<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, 
        alt <span style="color: #008000;">=</span> Server.<span style="color: #0000FF;">HtmlDecode</span><span style="color: #000000;">&#40;</span>reader<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</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>
reader.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
jqueryBuffer.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>Utils.<span style="color: #0000FF;">Implode</span><span style="color: #000000;">&#40;</span>galleryPics, <span style="color: #666666;">&quot;,<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;];<span style="color: #008080; font-weight: bold;">\n</span> cycleGalleryPics(galleryImages);<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ScriptManager.<span style="color: #0000FF;">RegisterStartupScript</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Page<span style="color: #000000;">&#41;</span>, <span style="color: #666666;">&quot;jqueryListeners&quot;</span>, jqueryBuffer.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
...</pre></div></div>

<p>Which results in the following JavaScript on the page:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> galleryImages <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;filename&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;alt&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Picture 1&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;filename&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;alt&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Caption 2&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;filename&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;3&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;alt&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Something 3&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;filename&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;4&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;alt&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;etc&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;filename&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;5&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;alt&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;filename&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;6&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;alt&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  cycleGalleryPics<span style="color: #009900;">&#40;</span>galleryImages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <em>cycleGalleryPics</em> method just iterates over the array and creates an image for each element, then adds it to a named container on the document.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> cycleGalleryPics<span style="color: #009900;">&#40;</span>galleryImages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> galleryDiv <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#Gallery'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    galleryDiv.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #009900;">&#40;</span>imagePath<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        path <span style="color: #339933;">=</span> <span style="color: #3366CC;">'UserImages/Gallery/'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        path <span style="color: #339933;">=</span> imagePath <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/Gallery/'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    jQuery.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>galleryImages<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img/&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            width<span style="color: #339933;">:</span> <span style="color: #CC0000;">368</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">370</span><span style="color: #339933;">,</span>
            alt<span style="color: #339933;">:</span> unescape<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">alt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            src<span style="color: #339933;">:</span> path <span style="color: #339933;">+</span> unescape<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">filename</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'.jpg'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#Gallery'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    galleryDiv.<span style="color: #660066;">cycle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> fx<span style="color: #339933;">:</span> <span style="color: #3366CC;">'fade'</span><span style="color: #339933;">,</span> timeout<span style="color: #339933;">:</span> <span style="color: #CC0000;">3000</span><span style="color: #339933;">,</span> random<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> fit<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> containerResize<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The reason the contents of the container are cleared before adding the images is to provide support for users with JavaScript disabled. One image is added by an ASP.NET <em>Image</em> control so there won&#8217;t be a block of empty space.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.leghumped.com/blog/2009/07/05/faster-image-loading-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.346 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-03-11 03:22:52 -->
