Fred
Fred

Reputation: 5808

Multiple resource dictionaries, select one

I am working on an app that uses a resource dictionary for styling. I have to make a change that will enable a config setting to change the dictionary being used.

I have three dictionaries: Original.xaml, Neon.xaml & Graphite.xaml.

App.xaml:

 <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Resources/Original.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

I can change the dictionary being used by calling:

private void DynamicLoadStyles(string StyleToUse)
{
    string fileName = "C:\\Data\\Projects\\MyApp\\MyApp\\Resources\\" +
        StyleToUse + ".xaml";
    using (FileStream fs = new FileStream(fileName, FileMode.Open))
    {
        ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
        Resources.MergedDictionaries.Clear();
        Resources.MergedDictionaries.Add(dic);
    }
 }

All works as expect (however I am not sure if this is the correct way to do it). The problem is I would rather embed the files and not have to load them from an external file.

I have searched for info to help but cannot find what I am looking for. That said I am new to WPF (3 weeks) and not really sure what I am doing yet.

Any help would be greatly appreciated.

Upvotes: 3

Views: 2440

Answers (1)

Fred
Fred

Reputation: 5808

I don't really like to answer my own question as it suggests I shouldn't have asked in the first place. But I have solved the problem using ....

    private void LoadDynamicResource(String StyleToUse)
    {

        ResourceDictionary dic = new ResourceDictionary { Source = new Uri(StyleToUse, UriKind.Relative) };
        Resources.MergedDictionaries.Clear();
        Resources.MergedDictionaries.Add(dic);

    }

I would be interested in opinions on this though.

Upvotes: 4

Related Questions