CHANDRA
CHANDRA

Reputation: 4938

How to create ResourceDictionary in code-behind?

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <sys:String x:Key="one">ONE</sys:String>
    <sys:String x:Key="two">TWO</sys:String>
    <sys:String x:Key="three">THREE</sys:String>
</ResourceDictionary>

Now want to dynamically create resource ResourceDictionary same as above by code-behind in WPF using C#. Is it possible to create so?

Upvotes: 3

Views: 5324

Answers (2)

yo chauhan
yo chauhan

Reputation: 12315

public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
        ResourceDictionary rd = new ResourceDictionary();
        rd.Add("one", "ONE");
        rd.Add("two", "TWO");
        rd.Add("three", "THREE");
        this.Resources.MergedDictionaries.Add(rd);
    }

I hope this will help.

Upvotes: 5

Daniel Holder
Daniel Holder

Reputation: 111

Have you seen the ResourceDictionary.Add method - http://msdn.microsoft.com/en-us/library/ms521848.aspx?

Upvotes: 0

Related Questions