Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you port a theme from Silverlight to WPF?
    primarykey
    data
    text
    <p><strong>It's "easy"!</strong></p> <p>I just came across <a href="http://dotnet.org.za/rudi/archive/2008/10/29/silverlight-love-for-wpf.aspx">this blog post by Rudi Grobler</a> that says it's "easy" to port a theme from Silverlight to WPF. Unfortunately, he doesn't say how to do it.</p> <hr> <p><strong>Download and install</strong></p> <p>I have installed both the <a href="http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117">WPF Toolkit</a> and <a href="http://silverlight.codeplex.com/">Silverlight Toolkit</a> from Codeplex.</p> <p>I also went and dug up <a href="http://silverlight.codeplex.com/SourceControl/changeset/view/32933#639398">the source code for the theme I'm interested in (BureauBlue)</a> (warning, this takes a little while to load), and pasted that into a fresh Resource Dictionary file in my test project.</p> <hr> <p><strong>Ut oh, broken references</strong></p> <p>Lots of blue squiggly lines.</p> <p><a href="http://img32.imageshack.us/img32/6032/brokenreferences.jpg">alt text http://img32.imageshack.us/img32/6032/brokenreferences.jpg</a></p> <hr> <p><strong>No references here</strong></p> <p>So, I went to add the references and discovered...they aren't listed.</p> <p><a href="http://img35.imageshack.us/img35/7466/addreferencedialog.jpg">alt text http://img35.imageshack.us/img35/7466/addreferencedialog.jpg</a></p> <hr> <p><strong>Ahh, there they are</strong></p> <p>Fortunately, I was able to find them after some extensive <em>browsing</em>.</p> <p><a href="http://img269.imageshack.us/img269/3830/addreferencedialogbrows.jpg">alt text http://img269.imageshack.us/img269/3830/addreferencedialogbrows.jpg</a></p> <hr> <p><strong>And so I added them</strong></p> <p>Having found the missing references, I attempted to add them. This actually seemed to resolve the broken <code>xmlns</code> links (at least the blue squiggly lines went away), but when I built my project I got an error:</p> <blockquote> <p>Error 1 Unknown build error, 'Cannot resolve dependency to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.' NmtConcept</p> </blockquote> <hr> <p><strong>And, so I ask...</strong></p> <p>How do I make a Silverlight theme work in WPF?</p> <hr> <p><strong>Update!</strong></p> <p>Well, I thought I figured it out. I decided to start with the <code>DataGrid</code>. I figured out what all the WPF equivalents were for the Silverlight assemblies, and I actually got my project to compile and run. And then I saw the result...</p> <p><a href="http://img44.imageshack.us/img44/2418/porteddatagrid.jpg">alt text http://img44.imageshack.us/img44/2418/porteddatagrid.jpg</a></p> <p>The header is somewhat correct (except for the missing sort arrows), but the rest of it looks like there was some kind of horrible accident involving tan colored bricks.</p> <p>Here's the example on the <a href="http://silverlight.codeplex.com/wikipage?title=Silverlight%20Toolkit%20Overview%20Part%205&amp;referringTitle=Silverlight%20Toolkit%20Overview%20Part%204">Silverlight Toolkit webpage</a>.</p> <p><a href="http://img196.imageshack.us/img196/997/silverlightdatagrid.jpg">alt text http://img196.imageshack.us/img196/997/silverlightdatagrid.jpg</a> </p> <p>So, I didn't even come close.</p> <hr> <p><strong>What I did</strong></p> <p>Please allow me to explain what I did to arrive at this mess.</p> <hr> <p><strong>DataGridFrozenGrid</strong></p> <p>First, I had to grab <code>DataGridFrozenGrid</code> from <a href="http://silverlight.codeplex.com/SourceControl/changeset/view/32933#636999">this Silverlight Toolkit source code page</a> because WPF had never heard of such a class.</p> <hr> <p><strong>BureauBlue</strong></p> <p>Then I pasted in the <em>key pieces</em> from <a href="http://silverlight.codeplex.com/SourceControl/changeset/view/32933#639398">the source code for BureauBlue</a> (again, warning: this baby is slow to load).</p> <p>By <em>key pieces</em>, I mean:</p> <ol> <li>All the brush resources at the beginning of the file plus</li> <li><p>The <code>Style</code> for each of these controls:</p> <ul> <li>DataGridColumnHeader</li> <li>DataGridCell</li> <li>DataGridRowHeader</li> <li>DataGridRow</li> </ul></li> </ol> <p>Strangely, the original file contained no style for the <code>DataGrid</code> itself (please correct me if I'm wrong, but I used Find and everything).</p> <hr> <p><strong>Silverlight --> WPF</strong></p> <p>Next, I converted the <code>xmlns</code> references to WPF equivalents. Here's how my <code>ResourceDictionary</code> element turned out:</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mwc="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" xmlns:mwcp="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit" xmlns:swcp="clr-namespace:System.Windows.Controls.Primitives;assembly=CommonLibraryWpf" xmlns:sw="clr-namespace:System.Windows;assembly=WPFToolkit"&gt; </code></pre> <p>I of course edited the namespace tags to match.</p> <hr> <p><strong>Edit until it works</strong></p> <p>Once I had all this working, I still had a few minor problems. Some of the <code>x:Name</code> elements had spaces in them. The compiler wouldn't allow this, so I had to replace the spaces with underscores.</p> <p>Here are the relevant snippets:</p> <pre><code>&lt;!-- Important: all underscores used to be spaces --&gt; &lt;sw:VisualState x:Name="MouseOver_CurrentRow_Selected"&gt; &lt;sw:VisualState x:Name="Normal_CurrentRow"&gt; &lt;sw:VisualState x:Name="Normal_Selected"&gt; &lt;sw:VisualState x:Name="Normal_EditingRow"&gt; &lt;sw:VisualState x:Name="Normal_AlternatingRow" /&gt; &lt;sw:VisualState x:Name="Normal_Selected"&gt; &lt;sw:VisualState x:Name="MouseOver_Selected"&gt; &lt;sw:VisualState x:Name="Unfocused_Selected"&gt; </code></pre> <p>Changing these names seemed like a very bad idea--and may be the cause of all my problems--but I didn't know what else to do to get the thing to compile.</p> <p>The other change I had to make was: some of the <code>SolidColorBrush</code> and <code>LinearGradientBrush</code> items at the beginning used <code>x:Name</code> instead of <code>x:Key</code>. I changed all of them to <code>x:Key</code>. Perhaps this was also a bad idea, but again, the compiler made me.</p> <hr> <p><strong>Does this help you help me?</strong></p> <p>If you're still with me after all that, got any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload