<?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>Greg Does IT &#187; Styles</title>
	<atom:link href="http://gregdoesit.com/tag/styles/feed/" rel="self" type="application/rss+xml" />
	<link>http://gregdoesit.com</link>
	<description>Do it to get it</description>
	<lastBuildDate>Sun, 22 Apr 2012 16:48:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Manipulating Styles At Runtime in Silverlight</title>
		<link>http://gregdoesit.com/2010/11/manipulating-styles-at-runtime-in-silverlight/</link>
		<comments>http://gregdoesit.com/2010/11/manipulating-styles-at-runtime-in-silverlight/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 15:18:50 +0000</pubDate>
		<dc:creator>Gergely Orosz</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Styles]]></category>

		<guid isPermaLink="false">http://gregdoesit.com/?p=684</guid>
		<description><![CDATA[I&#8217;ve been writing a 4 part series on everything (I think) is worth knowing about styles in Silverlight. The previous parts were Silverlight Styles: an Introduction, Silverlight Styles: Advanced Topics and Silverlight Styles: Further Advanced Topics The final part of the series is ready and it covers the following topics: Modifying Styles Runtime: the IsSealed [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing a 4 part series on everything (I think) is worth knowing about styles in Silverlight. The previous parts were <a href="http://gregdoesit.com/2010/07/styles-in-silverlight-an-introduction/">Silverlight Styles: an Introduction</a>, <a href="http://gregdoesit.com/2010/07/styles-in-silverlight-advanced-topics-part-1/">Silverlight Styles: Advanced Topics</a> and <a href="http://gregdoesit.com/2010/07/styles-in-silverlight-advanced-topics-part-1/">Silverlight Styles: Further Advanced Topics</a></p>
<p>The final part of the series is ready and it covers the following topics:</p>
<ul>
<li><a href="http://www.visiblox.com/blog/2010/11/styles-in-silverlight-manipulating-in-runtime#isSealedProperty">Modifying Styles Runtime: the IsSealed Property</a> &#8211; modifying the Setters of a Style can only be done if the IsSealed property is set to false and this property is set to true as soon as the style gets applied to an element in the visual tree.
</li>
<li><a href="http://www.visiblox.com/blog/2010/11/styles-in-silverlight-manipulating-in-runtime#CloningStyles">Cloning Styles</a> &#8211; because sealed Styles can&#8217;t be modified, one needs to clone them to be able to manipulate them. I&#8217;m providing example code on how cloning of a Style can be done.</li>
<li><a href="http://www.visiblox.com/blog/2010/11/styles-in-silverlight-manipulating-in-runtime#mergingUsingBasedOn">Merging styles runtime</a> &#8211; I&#8217;m showing two ways of merging existing styles programatically, both using the BasedOn property of Style as well as just copying Style setters.</li>
</ul>
<p>You can read the detailed article and download source code for examples <a href="http://www.visiblox.com/blog/2010/11/styles-in-silverlight-manipulating-in-runtime">on my Visiblox blog</a> &#8211; I hope you&#8217;ll find it useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregdoesit.com/2010/11/manipulating-styles-at-runtime-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disappearing Style Setters in Silverlight 3 and WP7 &#8211; fixed in Silverlight 4</title>
		<link>http://gregdoesit.com/2010/09/disappearing-style-setters-in-silverlight-3-and-wp7-fixed-in-silverlight-4/</link>
		<comments>http://gregdoesit.com/2010/09/disappearing-style-setters-in-silverlight-3-and-wp7-fixed-in-silverlight-4/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 08:21:11 +0000</pubDate>
		<dc:creator>Gergely Orosz</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[Styles]]></category>

		<guid isPermaLink="false">http://gregdoesit.com/?p=578</guid>
		<description><![CDATA[I&#8217;ve come across a really annoying bug in Silverlight 3 that seems to be fixed in Silverlight 4, but still present in WP7. When creating a Style object in runtime, then setting that as the Style of an element in the visual tree, the Value properties in the Setters of the original Style are set [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve come across a really annoying bug in Silverlight 3 that seems to be fixed in Silverlight 4, but still present in WP7. When creating a Style object in runtime, then setting that as the Style of an element in the visual tree, the Value properties in the Setters of the original Style are set to null!</p>
<h3>The problem</h3>
<p>I&#8217;ve come across this issue because I was trying to clone styles with the following extension method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Style Clone<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Style style<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>style <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
    Style clonedStyle <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Style<span style="color: #000000;">&#40;</span>style.<span style="color: #0000FF;">TargetType</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    clonedStyle.<span style="color: #0000FF;">BasedOn</span> <span style="color: #008000;">=</span> style.<span style="color: #0000FF;">BasedOn</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Setter setterToCopy <span style="color: #0600FF;">in</span> style.<span style="color: #0000FF;">Setters</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        clonedStyle.<span style="color: #0000FF;">Setters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Setter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Property <span style="color: #008000;">=</span> setterToCopy.<span style="color: #0000FF;">Property</span>,
            Value <span style="color: #008000;">=</span> setterToCopy.<span style="color: #0000FF;">Value</span>
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> clonedStyle<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see, this clone method is not doing anything complicated: it is simply iterating through the Setters of the Style and creating new Setter instances with the Property and Value of the original Setters &#8211; basically deep cloning the Style itself.</p>
<h3>The issue in Silverlight 3</h3>
<p>When I did clone a Style that had been created on the fly and had been assigned a the Style of an element, the values of Setters magically were set to null in Silverlight 3. The problem is visualized using the following simple code:<br />
<span id="more-578"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Style dynamicallyCreatedStyle <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Style<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Rectangle<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
dynamicallyCreatedStyle.<span style="color: #0000FF;">Setters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Setter<span style="color: #000000;">&#40;</span>Rectangle.<span style="color: #0000FF;">StrokeThicknessProperty</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
dynamicallyCreatedStyle.<span style="color: #0000FF;">Setters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Setter<span style="color: #000000;">&#40;</span>Rectangle.<span style="color: #0000FF;">StrokeProperty</span>, <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #000000;">&#40;</span>Colors.<span style="color: #0000FF;">Red</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Rectangle1.<span style="color: #0000FF;">Style</span> <span style="color: #008000;">=</span> dynamicallyCreatedStyle.<span style="color: #0000FF;">Clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Works fine</span>
Rectangle2.<span style="color: #0000FF;">Style</span> <span style="color: #008000;">=</span> dynamicallyCreatedStyle<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// The Value of all Setters in dynamicallyCreatedStyle are set to null because of a Silverlight 3 bug</span>
Rectangle3.<span style="color: #0000FF;">Style</span> <span style="color: #008000;">=</span> dynamicallyCreatedStyle.<span style="color: #0000FF;">Clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// The Value of all Setters of dynamicallyCreatedStyle are null, thus Rectangle 3 won't be shown in Silverlight 3!</span></pre></div></div>

<p>See the example in Silverlight 3 below. As you can notice, the third rectangle isn&#8217;t showing as the Style that it&#8217;s assigned to it (the clone of dynamicallyCreatedStyle) has all of it&#8217;s Setter Values set to null:</p>
<p><object type="application/x-silverlight-2" data="data:application/x-silverlight,"  width="500" height="180"><param name="source" value="http://gregdoesit.com/wp-content/uploads/2010/09/StyleProblem-v3.xap"/></object> </p>
<h3>The issue in Windows Phone 7: exception being thrown</h3>
<p>In Windows Phone 7 the same code still results in incorrect behaviour with an exception being thrown.</p>
<p>The Value of the Setter is not set to null, instead it&#8217;s value can&#8217;t be determined runtime, in the debugger. Cloning of this Style is still possible, however when assigning this cloned style to in this line:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Rectangle3.<span style="color: #0000FF;">Style</span> <span style="color: #008000;">=</span> dynamicallyCreatedStyle.<span style="color: #0000FF;">Clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>and ArgumentException is thrown with the error message <em>The parameter is incorrect</em>.</p>
<h3>Silverlight 4: issue fixed</h3>
<p>When compiling the very same code in Silverlight 4, the Values of the Setters of the dynamically created Style are no longer set to null. Here is the same example targeted for Silverlight 4:</p>
<p><object type="application/x-silverlight-2" data="data:application/x-silverlight,"  width="500" height="180"><param name="source" value="http://gregdoesit.com/wp-content/uploads/2010/09/StyleProblem-v4.xap"/></object> </p>
<p>Using Silverlight 4, the third rectangle is showing in the application. Kudos for the Silverlight team for fixing this (as far as I was aware) undocumented issue with Styles.</p>
<p>(Note: you can download the source of the example here: <a href='http://gregdoesit.com/wp-content/uploads/2010/09/StyleProblem.zip'>Silverlight 3 Styling Issue.zip</a>)</p>
<h3>Conclusion</h3>
<p>It&#8217;s safe to say that due to this bug present in Silverlight 3 and Windows Phone 7, it&#8217;s definitely not recommended cloning Styles after they&#8217;ve been assigned to visual elements. If you&#8217;re forced to modify existing styles on visual elements, I&#8217;d suggest to constantly clone them before they&#8217;re applied to a visual element. Of course this is an expensive operation, so it&#8217;s definitely worth investigating if there are any other options available to avoid this happening in your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregdoesit.com/2010/09/disappearing-style-setters-in-silverlight-3-and-wp7-fixed-in-silverlight-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Styles in Silveright &#8211; Data Binding, Runtime Declaration and Further Advanced Topics</title>
		<link>http://gregdoesit.com/2010/08/styles-in-silveright-data-binding-runtime-declaration-and-further-advanced-topics/</link>
		<comments>http://gregdoesit.com/2010/08/styles-in-silveright-data-binding-runtime-declaration-and-further-advanced-topics/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 08:59:16 +0000</pubDate>
		<dc:creator>Gergely Orosz</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Styles]]></category>

		<guid isPermaLink="false">http://gregdoesit.com/?p=573</guid>
		<description><![CDATA[I&#8217;m writing a 4 part series on everything (I think) is worth knowing about styles in Silverlight. The previous parts were Silverlight Styles: an Introduction and Silverlight Styles: Advanced Topics. The third part of the series is ready and it covers the following topics: Style is a dependency property. Because of this, it supports the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing a 4 part series on everything (I think) is worth knowing about styles in Silverlight. The previous parts were <a href="http://gregdoesit.com/2010/07/styles-in-silverlight-an-introduction/">Silverlight Styles: an Introduction</a> and <a href="http://gregdoesit.com/2010/07/styles-in-silverlight-advanced-topics-part-1/">Silverlight Styles: Advanced Topics</a>.</p>
<p>The third part of the series is ready and it covers the following topics:</p>
<ul>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/08/styles-in-silverlight-further-advanced-topics#stylesAreDependencyProperties">Style is a dependency property</a>. Because of this, it supports the following features:
<ul>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/08/styles-in-silverlight-further-advanced-topics#dataBinding">Data binding</a> &#8211; it&#8217;s possible to bind a style to another (even though this is probably a very rare use case)</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/08/styles-in-silverlight-further-advanced-topics#propertyChangedNotifications">Property change notifications</a> &#8211; it is possible to get notified when the Style property changes, a little workaround is needed though</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/08/styles-in-silverlight-further-advanced-topics#clearValue">ClearValue</a> &#8211; when resetting the value of a Style, setting it to null may result in the element disappearing. It&#8217;s worth knowing what the difference between that and calling ClearValue are</li>
</ul>
</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/08/styles-in-silverlight-further-advanced-topics#declaringStylesInCodeBehind">Declaring styles in code behind</a>: creating styles programmatically is more complex that via XAML but at times this technique may be useful.</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/08/styles-in-silverlight-further-advanced-topics#onlyDependencyPropertiesCanBeStyled">Only dependency properties can be styled</a>, styling CLR properties will result in either a compile time error or a runtime exception.</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/08/styles-in-silverlight-further-advanced-topics#onlyFrameworkElementsHaveStyles">Only classes descending from FrameworkElement have the Style property</a> and thus can be styled.</li>
</ul>
<p>You can read the detailed article <a href="http://www.scottlogic.co.uk/blog/gergely/2010/08/styles-in-silverlight-further-advanced-topics/">on my Scott Logic blog</a> &#8211; I hope you&#8217;ll find it useful.</p>
<p>The final part of the series will explore manipulating styles in runtime.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregdoesit.com/2010/08/styles-in-silveright-data-binding-runtime-declaration-and-further-advanced-topics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Styles in Silverlight &#8211; Advanced Topics</title>
		<link>http://gregdoesit.com/2010/07/styles-in-silverlight-advanced-topics-part-1/</link>
		<comments>http://gregdoesit.com/2010/07/styles-in-silverlight-advanced-topics-part-1/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 09:55:12 +0000</pubDate>
		<dc:creator>Gergely Orosz</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Styles]]></category>

		<guid isPermaLink="false">http://gregdoesit.com/?p=567</guid>
		<description><![CDATA[I&#8217;m writing a 4 part series on everything (I think) it&#8217;s worth knowing about styles. In the second part of the series (following the previous one Styles in Silverlight &#8211; an Introduction) I&#8217;m covering the following topics: Re-Using Styles for Different Types: the TargetType of the Style can be a parent type of the target [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing a 4 part series on everything (I think) it&#8217;s worth knowing about styles. In the second part of the series (following the previous one <a href="http://gregdoesit.com/2010/07/styles-in-silverlight-an-introduction/">Styles in Silverlight &#8211; an Introduction</a>) I&#8217;m covering the following topics:</p>
<ul>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-inheritance-precedence-and-other-advanced-topics#reusingStyles">Re-Using Styles for Different Types</a>: the TargetType of the Style can be a parent type of the target object as long as it only sets properties of the parent type.<br/> So for example the Fill property of both an Ellipse and Rectangle can be styled by using a Style that&#8217;s TargetType is set to Shape (as both Ellipse and Rectangle inherit from Shape)</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-inheritance-precedence-and-other-advanced-topics/#styleBasedOn">Inheriting Styles using the BasedOn property</a>: Styles can be inherited from one another using the BasedOn property. Single inheritance is supported and the depth of inheritance is not limited.</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-inheritance-precedence-and-other-advanced-topics/#implicitStyling">Implicit styling</a>: Implicit styles allow defining a default style for all instances of a given type within the scope of the control. This feature has been introduced in Silverlight 4.</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-inheritance-precedence-and-other-advanced-topics/#stylePrecedence">Style Precedence</a>: Styles are only applied to a property if that property is not animated, has no local value set and has no value set in a template. This precedence is the same as the dependency property precedence.</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-inheritance-precedence-and-other-advanced-topics/#styleSetterPrecedence">Style Setter Precedence:</a> setters specifying the value of the same property may be declared within the same Style. The last one of them has the highest precedence and will be used.</li>
<p>You can read the (much more detailed) full article <a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-inheritance-precedence-and-other-advanced-topics/">on my Scott Logic blog</a>. The next part of the series will cover further advanced topics such as using Styles in code behind and exploring the relationship between styles and dependency properties from a practical perspective.
</ul>
]]></content:encoded>
			<wfw:commentRss>http://gregdoesit.com/2010/07/styles-in-silverlight-advanced-topics-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Styles in Silverlight: an Introduction</title>
		<link>http://gregdoesit.com/2010/07/styles-in-silverlight-an-introduction/</link>
		<comments>http://gregdoesit.com/2010/07/styles-in-silverlight-an-introduction/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 15:57:14 +0000</pubDate>
		<dc:creator>Gergely Orosz</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Styles]]></category>

		<guid isPermaLink="false">http://gregdoesit.com/?p=563</guid>
		<description><![CDATA[I&#8217;m writing a (planned) 4 parts series on everything I think it&#8217;s worth knowing about styles in Silverlight. The first part is ready and it covers the following topics: What are Styles?Styles are basically list of property-value pairs. Defining a style consists declaring the TargetType property and the list of Setters. Defining Styles in XAMLDefining [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing a (planned) 4 parts series on everything I think it&#8217;s worth knowing about styles in Silverlight. The first part is ready and it covers the following topics:</p>
<ul>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-an-introduction/#whatAreStyles">What are Styles?</a><br/>Styles are basically list of property-value pairs. Defining a style consists declaring the <a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-an-introduction/#targetType">TargetType</a> property and the list of <a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-an-introduction/#setters">Setters</a>.</li>
<li><a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-an-introduction/#definingStylesInXAML">Defining Styles in XAML</a><br/>Defining styles in XAML is the easiest and most common method. They can either be defined in the Resources section or inline.</li>
<li>Styles defined in XAML can be <a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-an-introduction/#referencing">referenced in code-behind</a>.</li>
<li>Altering visual templates of an object doesn&#8217;t always end at styling. More powerful way of customizing the look and feel of objects involves <a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-an-introduction/#templates">using Control Templates and Themes</a>.</li>
</ul>
<p>You can read the (much more detailed) full article <a href="http://www.scottlogic.co.uk/blog/gergely/2010/07/styles-in-silverlight-an-introduction">on my Scott Logic blog</a>. This part has meant to be an introduction, the next parts will dig into more advanced Style usage scenarios.</p>
]]></content:encoded>
			<wfw:commentRss>http://gregdoesit.com/2010/07/styles-in-silverlight-an-introduction/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

