<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Casey @ Microsoft</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/cstein/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/cstein/feed.xml</link>
    <description>Pro developer and rock dude</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:48:10 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Normal Properties vs. Dependency Properties</title>
      <link>https://docs.microsoft.com/archive/blogs/cstein/normal-properties-vs-dependency-properties</link>
      <pubDate>Wed, 29 Oct 2008 16:16:00 GMT</pubDate>
      <dc:creator><![CDATA[caseydarwinstein]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cstein/2008/10/29/normal-properties-vs-dependency-properties/</guid>
      <description><![CDATA[In the Silverlight development world there are two kinds of properties that you can have on a...]]></description>
      <content:encoded><![CDATA[<P>In the Silverlight development world there are two kinds of properties that you can have on a Silverlight control: normal C# properties and dependency properties.&nbsp; The normal C# properties are added to a Silverlight control the same way as they would be added to any C# class:</P>
<BLOCKQUOTE><PRE class=csharpcode><SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>string</SPAN> MyProperty { get; set; }</PRE></BLOCKQUOTE>
<STYLE type=text/css>


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</STYLE>

<P>And then in XAML, you could set the value of the property like this:</P>
<BLOCKQUOTE><PRE class=csharpcode>&lt;MyControl MyProperty=<SPAN class=str>"someValue"</SPAN> /&gt;</PRE></BLOCKQUOTE>
<STYLE type=text/css>


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</STYLE>

<P>This works fine for setting specific values in templates that are unlikely to change or if they do change then the change is always manually made to the XAML file.&nbsp; But what happens if you want to have the value of the property bound to some other value that is retrieved from a database, web service, etc?&nbsp; You might try Binding to the property like so:</P>
<BLOCKQUOTE><PRE class=csharpcode>&lt;MyControl MyProperty=<SPAN class=str>"{Binding DynamicPropertyValue}"</SPAN> /&gt;</PRE></BLOCKQUOTE>
<STYLE type=text/css>


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</STYLE>

<P>Where <STRONG>DynamicPropertyValue</STRONG> was a property on the parent element that you wanted to to have your property get data from.&nbsp; But when you run it, you get and error similar to the following:</P>
<BLOCKQUOTE>
<P>Silverlight error <BR>ErrorCode: 2260 <BR>ErrorType: ParserError <BR>Message: AG_E_PARSER_BAD_PROPERTY_VALUE <BR>XAMLFile: <BR>Line: 1 <BR>Position: 6030</P></BLOCKQUOTE>
<P>Enter the <STRONG>DependencyProperty</STRONG>.&nbsp; The <STRONG>DependencyProperty </STRONG>is a property that <STRONG>can</STRONG> be bound dynamically.&nbsp; The downside is that there is a bit more overhead.&nbsp; Here is the same property from above expressed as a <STRONG>DependencyProperty</STRONG>:</P>
<BLOCKQUOTE><PRE class=csharpcode><SPAN class=rem>/// &lt;summary&gt;</SPAN>
<SPAN class=rem>/// Gets or sets the MyProperty value.</SPAN>
<SPAN class=rem>/// &lt;/summary&gt;</SPAN>
<SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>string</SPAN> MyProperty
{
    get { <SPAN class=kwrd>return</SPAN> (<SPAN class=kwrd>string</SPAN>)GetValue(MyProperty); }
    set { SetValue(MyProperty, <SPAN class=kwrd>value</SPAN>); }
}

<SPAN class=rem>/// &lt;summary&gt;</SPAN>
<SPAN class=rem>/// Dependency field of MyProperty property.</SPAN>
<SPAN class=rem>/// &lt;/summary&gt;</SPAN>
<SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>static</SPAN> <SPAN class=kwrd>readonly</SPAN> DependencyProperty MyPropertyProperty =
        DependencyProperty.Register(<SPAN class=str>"MyProperty"</SPAN>, 
                                    <SPAN class=kwrd>typeof</SPAN>(<SPAN class=kwrd>string</SPAN>), 
                                    <SPAN class=kwrd>typeof</SPAN>(MyControl),
                                    <SPAN class=kwrd>null</SPAN>);</PRE></BLOCKQUOTE>
<STYLE type=text/css>

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</STYLE>

<P>Now if you want to add some logic that happens when the property value is changed you can set up your <STRONG>DependencyProperty</STRONG> like this:</P>
<BLOCKQUOTE><PRE class=csharpcode><SPAN class=rem>/// &lt;summary&gt;</SPAN>
<SPAN class=rem>/// Dependency field of MyProperty property.</SPAN>
<SPAN class=rem>/// &lt;/summary&gt;</SPAN>
<SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>static</SPAN> <SPAN class=kwrd>readonly</SPAN> DependencyProperty MyPropertyProperty =
        DependencyProperty.Register(<SPAN class=str>"MyProperty"</SPAN>, 
                 <SPAN class=kwrd>typeof</SPAN>(<SPAN class=kwrd>string</SPAN>), 
                 <SPAN class=kwrd>typeof</SPAN>(MyControl),
                 <SPAN class=kwrd>new</SPAN> PropertyMetadata(</PRE><PRE class=csharpcode><SPAN class=kwrd>new</SPAN> PropertyChangedCallback(OnMyPropertyPropertyChanged)));

<SPAN class=kwrd>private</SPAN> <SPAN class=kwrd>static</SPAN> <SPAN class=kwrd>void</SPAN> OnMyPropertyPropertyChanged(DependencyObject sender, 
            DependencyPropertyChangedEventArgs args)
{
    MyControl myControl = (MyControl)sender;
    myControl.OnMyPropertyChanged((<SPAN class=kwrd>string</SPAN>)args.NewValue);
}

<SPAN class=rem>/// &lt;summary&gt;</SPAN>
<SPAN class=rem>/// Event handler for the MyProperty property changed event.</SPAN>
<SPAN class=rem>/// &lt;/summary&gt;</SPAN>
<SPAN class=rem>/// &lt;param name="newMyPropertyValue"&gt;The new MyProperty value to set.&lt;/param&gt;</SPAN>
<SPAN class=kwrd>protected</SPAN> <SPAN class=kwrd>virtual</SPAN> <SPAN class=kwrd>void</SPAN> OnCurrentRatingValueChanged(<SPAN class=kwrd>string</SPAN> newMyPropertyValue)
{
    <SPAN class=kwrd>if</SPAN> (newMyPropertyValue.Length == 0)
    {
        <SPAN class=kwrd>throw</SPAN> <SPAN class=kwrd>new</SPAN> ArgumentException(<SPAN class=str>"MyProperty cannot be null."</SPAN>);
    }
}</PRE>
<STYLE type=text/css>

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</STYLE>

<STYLE type=text/css>

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</STYLE>
</BLOCKQUOTE>
<P>Anyway, there you have it, you can now bind data to properties in XAML.&nbsp; As usual, should you find this information to have been helpful, or less than helpful, please feel free to leave comments.</P>]]></content:encoded>
    </item>
    <item>
      <title>How do I get the height/width of a Silverlight UIElement if its not explicitly set?</title>
      <link>https://docs.microsoft.com/archive/blogs/cstein/how-do-i-get-the-heightwidth-of-a-silverlight-uielement-if-its-not-explicitly-set</link>
      <pubDate>Wed, 22 Oct 2008 11:18:37 GMT</pubDate>
      <dc:creator><![CDATA[caseydarwinstein]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cstein/2008/10/22/how-do-i-get-the-heightwidth-of-a-silverlight-uielement-if-its-not-explicitly-set/</guid>
      <description><![CDATA[The answer is, of course, to use the ActualHeight and ActualWidth properties. But wait, ActualHeight...]]></description>
      <content:encoded><![CDATA[<p>The answer is, of course, to use the <strong>ActualHeight</strong> and <strong>ActualWidth</strong> properties. But wait, <strong>ActualHeight</strong> and <strong>ActualWidth</strong> return <strong>0.0</strong>, what gives?&#160; Well it turns out that if you just check the <strong>ActualHeight</strong> and <strong>ActualWidth</strong> right away, you are getting the measurements of the element BEFORE it has been measured and sized.&#160; The way to accurately use the <strong>ActualHeight</strong> and <strong>ActualWidth</strong> properties is to first subscribe to the element’s <strong>SizeChanged</strong> event.&#160; </p>  <blockquote>   <pre class="csharpcode">myElement.SizeChanged += <span class="kwrd">new</span> SizeChangedEventHandler(OnSizeChanged);</pre>
</blockquote>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Now, in the event handler you can use the <strong>NewSize</strong> property to get the newly measured <strong>ActualHeight </strong>and <strong>ActualWidth</strong>.</p>

<blockquote>
  <pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> OnSizeChanged(<span class="kwrd">object</span> sender, 
                           SizeChangedEventArgs e)
{
    actualHeight = e.NewSize.Height;
    actualWidth = e.NewSize.Width;
}</pre>
</blockquote>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Thanks everybody!&#160; I’ll be here all week!&#160; Be sure to tip your waitresses…</p>

<p></p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>]]></content:encoded>
    </item>
    <item>
      <title>“The property ‘_UnknownContent’ does not exist…” What?</title>
      <link>https://docs.microsoft.com/archive/blogs/cstein/the-property-_unknowncontent-does-not-exist-what</link>
      <pubDate>Thu, 02 Oct 2008 16:41:28 GMT</pubDate>
      <dc:creator><![CDATA[caseydarwinstein]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cstein/2008/10/02/the-property-_unknowncontent-does-not-exist-what/</guid>
      <description><![CDATA[I was working on a custom Control the other day and that had its own XAML file.&#160; I had it all...]]></description>
      <content:encoded><![CDATA[<p>I was working on a custom Control the other day and that had its own XAML file.&#160; I had it all coded up, but when I went to build it, it gave me the following two errors:</p>  <blockquote>   <p>Error&#160;&#160;&#160; 6&#160;&#160;&#160; The property '_UnknownContent' does not exist on the type 'ResourceDictionary' in the XML namespace '<a href="https://schemas.microsoft.com/winfx/2006/xaml/presentation'">https://schemas.microsoft.com/winfx/2006/xaml/presentation'</a>.&#160;&#160;&#160; C:\Source\Controls\MyControl.xaml</p> </blockquote>  <blockquote>   <p>Error&#160;&#160;&#160; 7&#160;&#160;&#160; The type 'List`1' is inside a ResourceDictionary and does not have a key.&#160;&#160;&#160; C:\Source\Controls\MyControl.xaml </p> </blockquote>  <p>Unfortunately this in not a very helpful error message.&#160; I’m embarrassed to say how long it took me to figure out the problem, so I’m not going to tell you.&#160; But I will tell you how to solve this if you run into it.&#160; Its surprisingly simple:</p>  <blockquote>   <p>1. Right-click the file in “Solution Explorer”.</p>    <p>2. Change the “Build Action” to “Resource”.</p>    <p>3. Save the project and rebuild.</p> </blockquote>  <p>So there you have it, an easy solution to an obnoxious, uninformative error.</p>]]></content:encoded>
    </item>
    <item>
      <title>“Why didn’t you use a HyperlinkButton?”</title>
      <link>https://docs.microsoft.com/archive/blogs/cstein/why-didnt-you-use-a-hyperlinkbutton</link>
      <pubDate>Fri, 26 Sep 2008 12:39:00 GMT</pubDate>
      <dc:creator><![CDATA[caseydarwinstein]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cstein/2008/09/26/why-didnt-you-use-a-hyperlinkbutton/</guid>
      <description><![CDATA[The other day I was working on a Silverlight control that would display a taxonomy of...]]></description>
      <content:encoded><![CDATA[<p>The other day I was working on a <strong>Silverlight</strong> control that would display a taxonomy of categories.&#160; Something along the lines of:</p>  <blockquote>   <p>One &gt; Two &gt; Three &gt; Four &gt; Five</p> </blockquote>  <p>where “Five” is a child of the “Four” category, which, in turn, is a child of the “Three” category, and so on.&#160; The way I initially though about it was that I would use a <strong>TextBlock</strong> for each of the categories and the “&gt;” separators.&#160; This actually worked fine.&#160; Here’s a simple version of the <strong>XAML</strong> that I used:</p>  <pre class="csharpcode">    &lt;StackPanel x:Name=<span class="str">&quot;LayoutRoot&quot;</span> Background=<span class="str">&quot;Black&quot;</span> 
                Orientation=<span class="str">&quot;Horizontal&quot;</span> Width=<span class="str">&quot;185&quot;</span> Height=<span class="str">&quot;15&quot;</span>&gt;
        &lt;StackPanel.Resources&gt;
            &lt;Style x:Name=<span class="str">&quot;CategoryStyle&quot;</span> TargetType=<span class="str">&quot;TextBlock&quot;</span>&gt;
                &lt;Setter Property=<span class="str">&quot;Foreground&quot;</span> Value=<span class="str">&quot;White&quot;</span> /&gt;
                &lt;Setter Property=<span class="str">&quot;FontSize&quot;</span> Value=<span class="str">&quot;11&quot;</span> /&gt;
                &lt;Setter Property=<span class="str">&quot;FontFamily&quot;</span> Value=<span class="str">&quot;Verdana&quot;</span> /&gt;
            &lt;/Style&gt;
            &lt;Style x:Name=<span class="str">&quot;SeparatorStyle&quot;</span> TargetType=<span class="str">&quot;TextBlock&quot;</span>&gt;
                &lt;Setter Property=<span class="str">&quot;Foreground&quot;</span> Value=<span class="str">&quot;White&quot;</span> /&gt;
                &lt;Setter Property=<span class="str">&quot;FontSize&quot;</span> Value=<span class="str">&quot;11&quot;</span> /&gt;
                &lt;Setter Property=<span class="str">&quot;FontFamily&quot;</span> Value=<span class="str">&quot;Verdana&quot;</span> /&gt;
                &lt;Setter Property=<span class="str">&quot;Margin&quot;</span> Value=<span class="str">&quot;3 0 3 0&quot;</span> /&gt;
            &lt;/Style&gt;
        &lt;/StackPanel.Resources&gt;
    &lt;/StackPanel&gt;</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style><style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>And here’s the code:</p>

<pre class="csharpcode">    <span class="kwrd">public</span> <span class="kwrd">void</span> AddCategory(<span class="kwrd">string</span> categoryName)
    {
        TextBlock tb = <span class="kwrd">new</span> TextBlock();
        tb.Text = categoryName;
        tb.Style = <span class="kwrd">this</span>.CategoryStyle;
        tb.MouseEnter += <span class="kwrd">new</span> MouseEventHandler(OnMouseEnter);
        tb.MouseLeave += <span class="kwrd">new</span> MouseEventHandler(OnMouseLeave);
        <span class="kwrd">this</span>.LayoutRoot.Children.Add(tb);
    }

    <span class="kwrd">private</span> <span class="kwrd">void</span> OnMouseEnter(<span class="kwrd">object</span> sender, MouseEventArgs e)
    {
        TextBlock tb = (TextBlock)sender;
        tb.TextDecorations = TextDecorations.Underline;
    }

    <span class="kwrd">private</span> <span class="kwrd">void</span> OnMouseLeave(<span class="kwrd">object</span> sender, MouseEventArgs e)
    {
        TextBlock tb = (TextBlock)sender;
        tb.TextDecorations = <span class="kwrd">null</span>;
    }</pre>

<pre class="csharpcode">    <span class="kwrd">public</span> <span class="kwrd">void</span> AddSeparator()
    {
        TextBlock separator = <span class="kwrd">new</span> TextBlock();
        separator.Text = <span class="str">&quot;&gt;&quot;</span>;
        separator.Style = <span class="kwrd">this</span>.SeparatorStyle;
        <span class="kwrd">this</span>.LayoutRoot.Children.Add(separator);
    }</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>When I ran this it looked liked the following:</p>

<p>&#160;&#160;&#160;&#160;&#160; <a href="https://blogs.msdn.com/blogfiles/cstein/WindowsLiveWriter/WhydidntyouuseaHyperlinkButton_83C3/taxonomy_2.jpg"><img title="taxonomy" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="taxonomy" src="https://blogs.msdn.com/blogfiles/cstein/WindowsLiveWriter/WhydidntyouuseaHyperlinkButton_83C3/taxonomy_thumb.jpg" width="198" border="0" /></a> </p>

<p>And when you mouse over one of the categories, it underlines.&#160; So I sent out my project for code review and one of the first comments to come back was: “Why didn’t you use a <strong>HyperlinkButton</strong>?”</p>

<p>Having never heard of the <strong>HyperlinkButton</strong>, I said, “There’s a <strong>HyperlinkButton</strong>?”&#160; But I looked it up and indeed, not only was there a <strong>HyperlinkButton</strong>, but it had automatic underlining, so I no longer had to handle that myself.</p>

<p>So I changed the code to add <strong>HyperlinkButtons</strong> instead of <strong>TextBlocks</strong>.&#160; This had the extra benefit of removing 12 lines of unnecessary code.&#160; My new method looked like the following:</p>

<pre class="csharpcode">    <span class="kwrd">public</span> <span class="kwrd">void</span> AddCategory(<span class="kwrd">string</span> categoryName)
    {
        HyperlinkButton hb = <span class="kwrd">new</span> HyperlinkButton();
        hb.Content = categoryName;
        hb.Style = <span class="kwrd">this</span>.CategoryStyle;
        <span class="kwrd">this</span>.LayoutRoot.Children.Add(hb);
    }</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>and I was able to completely remove the <strong>OnMouseEnter()</strong> and <strong>OnMouseLeave()</strong> methods.&#160; I left the seperators in as <strong>TextBlocks</strong> because they have no actions associated with them.&#160; </p>

<p>Among other things, since <strong>HyperlinkButtons</strong> are <strong>Buttons</strong>, they expose the <strong>Click</strong> event so there is no need to handle the related Mouse and Keyboard events.</p>]]></content:encoded>
    </item>
    <item>
      <title>But wait… who is this guy?</title>
      <link>https://docs.microsoft.com/archive/blogs/cstein/but-wait-who-is-this-guy</link>
      <pubDate>Thu, 25 Sep 2008 11:42:59 GMT</pubDate>
      <dc:creator><![CDATA[caseydarwinstein]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cstein/2008/09/25/but-wait-who-is-this-guy/</guid>
      <description><![CDATA[I realized after writing the last post that I had neglected to say who I was and why I was writing...]]></description>
      <content:encoded><![CDATA[<p>I realized after writing the last post that I had neglected to say who I was and why I was writing about Silverlight development.&#160; My name is Casey Stein and I’m a software developer working at Microsoft.&#160; I’ve been using Silverlight on the job for the past few months and find that many times it doesn’t seem to work in the way I expect it to.&#160; So I figured that if I’m running into these issues, other people probably are as well.&#160; I find that explaining a problem out loud to someone can really clarify it to yourself and can often lead to the solution.&#160; In an effort to help myself and others learn from my mistakes and solved problems, I’ve decided to write this blog.&#160; If you find that a solution I’ve suggested isn’t the best way to go, please feel free to leave a comment suggesting a better way to go about it.&#160; </p>]]></content:encoded>
    </item>
    <item>
      <title>Styles Are Forever – But ControlTemplates Are a Developer’s Best Friend</title>
      <link>https://docs.microsoft.com/archive/blogs/cstein/styles-are-forever-but-controltemplates-are-a-developers-best-friend</link>
      <pubDate>Wed, 24 Sep 2008 23:21:00 GMT</pubDate>
      <dc:creator><![CDATA[caseydarwinstein]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cstein/2008/09/24/styles-are-forever-but-controltemplates-are-a-developers-best-friend/</guid>
      <description><![CDATA[I’ve been working on a project where I have a Button with a default Style that I wanted to change...]]></description>
      <content:encoded><![CDATA[<p></p>  <p>I’ve been working on a project where I have a <strong>Button</strong> with a default <strong>Style</strong> that I wanted to change when the <strong>Button</strong> was clicked.&#160; So let’s say you have a <strong>Button </strong>and a couple of <strong>Styles</strong> as shown below:</p>  <blockquote>   <pre class="csharpcode">&lt;UserControl x:Class=<span class="str">&quot;SilverlightApplication1.Page&quot;</span>
    xmlns=<span class="str">&quot;https://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span> 
    xmlns:x=<span class="str">&quot;https://schemas.microsoft.com/winfx/2006/xaml&quot;</span> 
    Width=<span class="str">&quot;400&quot;</span> Height=<span class="str">&quot;300&quot;</span>&gt;
    &lt;UserControl.Resources&gt;
        &lt;Style x:Key=<span class="str">&quot;RedStyle&quot;</span> TargetType=<span class="str">&quot;Button&quot;</span>&gt;
            &lt;Setter Property=<span class="str">&quot;FontFamily&quot;</span> Value=<span class="str">&quot;Verdana&quot;</span>/&gt;
            &lt;Setter Property=<span class="str">&quot;Foreground&quot;</span> Value=<span class="str">&quot;Red&quot;</span>/&gt;
            &lt;Setter Property=<span class="str">&quot;FontSize&quot;</span> Value=<span class="str">&quot;11&quot;</span>/&gt;
        &lt;/Style&gt;
        &lt;Style x:Name=<span class="str">&quot;BlueStyle&quot;</span> TargetType=<span class="str">&quot;Button&quot;</span>&gt;
            &lt;Setter Property=<span class="str">&quot;FontFamily&quot;</span> Value=<span class="str">&quot;Courier New&quot;</span>/&gt;
            &lt;Setter Property=<span class="str">&quot;Foreground&quot;</span> Value=<span class="str">&quot;Blue&quot;</span>/&gt;
            &lt;Setter Property=<span class="str">&quot;FontSize&quot;</span> Value=<span class="str">&quot;11&quot;</span>/&gt;
        &lt;/Style&gt;
    &lt;/UserControl.Resources&gt;
    &lt;Grid x:Name=<span class="str">&quot;LayoutRoot&quot;</span> Background=<span class="str">&quot;White&quot;</span>&gt;
        &lt;Button x:Name=<span class="str">&quot;MyButton&quot;</span> Content=<span class="str">&quot;Style Me&quot;</span> Style=<span class="str">&quot;{StaticResource RedStyle}&quot;</span>/&gt;
    &lt;/Grid&gt;
&lt;/UserControl&gt;</pre>
</blockquote>

<p>I want the default style for this <strong>Button </strong>to be <strong>RedStyle</strong> so I have the <strong>Style</strong> set to:</p>

<blockquote>
  <pre class="csharpcode">Style=<span class="str">&quot;{StaticResource RedStyle}&quot;</span></pre>
</blockquote>

<p><style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>Now, in the code, I want to change the <strong>Style</strong> when the <strong>UserControl</strong> is clicked:</p>

<blockquote>
  <pre class="csharpcode"><span class="kwrd">void</span> MyButton_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e)
{
    Style blueStyle = <span class="kwrd">this</span>.Resources[<span class="str">&quot;BlueStyle&quot;</span>] <span class="kwrd">as</span> Style;
    <span class="kwrd">if</span> (blueStyle != <span class="kwrd">null</span>)
    {
        <span class="kwrd">this</span>.MyButton.Style = blueStyle;
    }
}</pre>
  <style type="text/css">

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style></blockquote>
<style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>But the problem comes along when you actually click the the control and the following exception is thrown:</p>

<blockquote>
  <p>Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))</p>
</blockquote>

<p>Wait, why can’t I change my style?&#160; You might think that the reason is because assigning the <strong>Style</strong> as a <strong>StaticResource</strong> makes it permanent.&#160; That’s true.&#160; However, the whole truth is that once a <strong>Style </strong>is assigned to an object, it can never have another <strong>Style </strong>assigned to it.&#160; Let’s test this out by not assigning the <strong>Style </strong>as a <strong>StaticResource</strong> in the <strong>XAML</strong>.&#160; Instead, let’s assign it in the constructor:</p>

<blockquote>
  <pre class="csharpcode"><span class="kwrd">public</span> Page()
{
    InitializeComponent();
    <span class="kwrd">this</span>.MyTextBlock.Style = <span class="kwrd">this</span>.Resources[<span class="str">&quot;RedStyle&quot;</span>] <span class="kwrd">as</span> Style;
    <span class="kwrd">this</span>.MouseLeftButtonUp += <span class="kwrd">new</span> MouseButtonEventHandler(Page_MouseLeftButtonUp);
}</pre>
</blockquote>
<style type="text/css">



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Now when we click the control what happens?</p>

<blockquote>
  <p>Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))</p>
</blockquote>

<p>But I really want to change the <strong>Style</strong> of the <strong>Button</strong> when I click it.&#160; Enter the <strong>ControlTemplate</strong>.&#160; First, let’s change the default style to include a <strong>ControlTemplate</strong>:</p>

<blockquote>
  <pre class="csharpcode">&lt;Style x:Key=<span class="str">&quot;BaseStyle&quot;</span> TargetType=<span class="str">&quot;Button&quot;</span>&gt;
    &lt;Setter Property=<span class="str">&quot;Template&quot;</span>&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate TargetType=<span class="str">&quot;Button&quot;</span>&gt;
                &lt;Grid x:Name=<span class="str">&quot;Content&quot;</span> Background=<span class="str">&quot;Black&quot;</span> Width=<span class="str">&quot;200&quot;</span> Height=<span class="str">&quot;100&quot;</span>&gt;
                    &lt;Grid.Resources&gt;
                        &lt;Style x:Key=<span class="str">&quot;RedStyle&quot;</span> TargetType=<span class="str">&quot;TextBlock&quot;</span>&gt;
                            &lt;Setter Property=<span class="str">&quot;FontFamily&quot;</span> Value=<span class="str">&quot;Verdana&quot;</span>/&gt;
                            &lt;Setter Property=<span class="str">&quot;Foreground&quot;</span> Value=<span class="str">&quot;Red&quot;</span>/&gt;
                            &lt;Setter Property=<span class="str">&quot;FontSize&quot;</span> Value=<span class="str">&quot;11&quot;</span>/&gt;
                        &lt;/Style&gt;
                    &lt;/Grid.Resources&gt;
                    &lt;TextBlock Text=<span class="str">&quot;{TemplateBinding Content}&quot;</span> Style=<span class="str">&quot;{StaticResource RedStyle}&quot;</span>/&gt;
                &lt;/Grid&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;</pre>
</blockquote>
<style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Here I’m saying that I want my <strong>Button</strong> to be a <strong>Grid</strong> that contains a <strong>TextBlock</strong>.&#160; It looks like this:</p>

<p>&#160;<a href="https://blogs.msdn.com/blogfiles/cstein/WindowsLiveWriter/StaticResourcePermanentResource_A955/RedStyle.jpg"><img title="RedStyle" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="148" alt="RedStyle" src="https://blogs.msdn.com/blogfiles/cstein/WindowsLiveWriter/StaticResourcePermanentResource_A955/RedStyle_thumb.jpg" width="240" border="0" /></a> </p>

<p>Now I’m going to add another <strong>ControlTemplate </strong>that will turn my text blue and change the font:</p>

<blockquote>
  <pre class="csharpcode">&lt;ControlTemplate x:Key=<span class="str">&quot;BlueTemplate&quot;</span> TargetType=<span class="str">&quot;Button&quot;</span>&gt;
    &lt;Grid x:Name=<span class="str">&quot;Content&quot;</span> Background=<span class="str">&quot;Yellow&quot;</span> Width=<span class="str">&quot;100&quot;</span> Height=<span class="str">&quot;50&quot;</span>&gt;
        &lt;Grid.Resources&gt;
            &lt;Style x:Key=<span class="str">&quot;BlueStyle&quot;</span> TargetType=<span class="str">&quot;TextBlock&quot;</span>&gt;
                &lt;Setter Property=<span class="str">&quot;FontFamily&quot;</span> Value=<span class="str">&quot;Courier New&quot;</span>/&gt;
                &lt;Setter Property=<span class="str">&quot;Foreground&quot;</span> Value=<span class="str">&quot;Blue&quot;</span>/&gt;
                &lt;Setter Property=<span class="str">&quot;FontSize&quot;</span> Value=<span class="str">&quot;13&quot;</span>/&gt;
            &lt;/Style&gt;
        &lt;/Grid.Resources&gt;
        &lt;TextBlock Text=<span class="str">&quot;{TemplateBinding Content}&quot;</span> Style=<span class="str">&quot;{StaticResource BlueStyle}&quot;</span>/&gt;
    &lt;/Grid&gt;
&lt;/ControlTemplate&gt;</pre>
</blockquote>
<style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p></p>
<style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p></p>

<p>My <strong>MyButton_Click()</strong> method now changes to:</p>

<blockquote>
  <pre class="csharpcode"><span class="kwrd">void</span> MyButton_Click(<span class="kwrd">object</span> sender, RoutedEventArgs e)
{
    ControlTemplate blueTemplate = <span class="kwrd">this</span>.Resources[<span class="str">&quot;BlueTemplate&quot;</span>] <span class="kwrd">as</span> ControlTemplate;
    <span class="kwrd">if</span> (blueTemplate != <span class="kwrd">null</span>)
    {
        <span class="kwrd">this</span>.MyButton.Template = blueTemplate;
    }
}</pre>
</blockquote>
<style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Now, when I click the <strong>Style Me</strong> button, it looks like this:</p>

<p><a href="https://blogs.msdn.com/blogfiles/cstein/WindowsLiveWriter/StaticResourcePermanentResource_A955/BlueStyle.jpg"><img title="BlueStyle" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="110" alt="BlueStyle" src="https://blogs.msdn.com/blogfiles/cstein/WindowsLiveWriter/StaticResourcePermanentResource_A955/BlueStyle_thumb.jpg" width="161" border="0" /></a> </p>

<p>The reason this now works is that while <strong>Style</strong> is write-once, the <strong>Template</strong> property isn’t.&#160; It can be set any number of times.&#160; </p>]]></content:encoded>
    </item>
  </channel>
</rss>