Custom Skins/Themes

I want my users to be able to customize my program through xaml-files.

I've seen that this is possible in WPF in some Login-example(found here[1]). I've tried to copy that code into a new project but nothing happens when i start the program. (The buttons size will remain 30x30 pixels)

 [1] http://wpf.netfx3.com/files/folders/applications/entry3927.aspx

This is my code:


Main-window (XAML):

<Window x:Class="TestSkin.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="TestSkin" Height="600" Width="800"

>

<Canvas Width ="800" Height ="600">

<Button Height="30" Width="30" Style="{DynamicResource LoginButton}" >Hoho</Button>

</Canvas>

</Window>


Main window(C#):

namespace TestSkin

{

public partial class Window1 : Window

{

ResourceDictionary skin1;

public Window1()

{

InitializeComponent();

skin1 = new ResourceDictionary();

Application.Current.Resources = skin1;

}

}

}


Skin1(xaml) :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class ="TestSkin.Skin1"

>

<Style x:Key="LoginButton"

TargetType="{x:Type Button}">

<Setter Property="Width"

Value="500" />

<Setter Property="Height"

Value="500" />

</Style>

</ResourceDictionary>


 

I belive the problem is that when i create the ResourceDictonary, VS doesn't create any C#-file for it. In the login-example the resourcedictonarys have C#-files. I can't see anything else that differs from the example, i even tried to copy the code straight of into a new project but it still doesn't work.



Answer this question

Custom Skins/Themes

  • daff2

    Hmm, i don't understand much of that link, I was looking for a simple tutorial, that blog is more advanced than a spaceship. The sample source doesn't even compile without modifications and i still get exceptions when trying loading the baml-files .

    I'm just looking for the easiest way to style a simple button dynamicly. I don't want to compile my xaml-files before i use them like they do in the blog you posted, the users should be able to customize their skins with a normal texteditor.


  • R.S.N

    Haha, yeah of course

    But it still doesn't work

    Also tried to define skin1 as a TestSkin.Skin1 instead of ResourceDictionary but still noting..


  • Ngwane

    Solved the problem myself, it was just to add a c#-file with the same name + classname as the resource xaml-file and inside the constructor in that file call InitializeComponent();

    I also managed to load a ResourceDictonary 100% dynamicly from an external non-compiled xaml-file wich was the thing i was actually looking for, this method was IMO even easier than loading a compiled file

    To do this i simply used this piece of code:

    Stream s = File.OpenRead("themes\\red.xaml");

    ResourceDictionary rd = (ResourceDictionary)XamlReader.Load(s);

    s.Close();

    Application.Current.Resources = rd;

    The red.xaml -file looks just like before but without the x:Class declaration.


  • mrotoloni

  • Kiranvukkadala

    Removing the height/width in Window1.xaml doesn't work eighter :(
  • Abhishek Chadha

    you set the height and width in both Button and Theme, that will be overridden the setter in Theme.you could try to set the Background or etc of style then seeing whether effects.
  • TraceyH

    try replace

    skin1 = new ResourceDictionary();

    with

    skin1 = new TestSkin.Skin1();


  • Custom Skins/Themes