title: How to dynamically add key and value to the ResourceDictionary in wpf?
body: <pre><code>&lt;Application
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="MultiLanguage.App"
  StartupUri="MainWindow.xaml"&gt;
  &lt;Application.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary x:Name="LanguageDictionary" Source="/LanguageResources;component/EnglishResources.xaml"/&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Application.Resources&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>EnglishResources.xaml</strong></p>
  
  <pre><code>&lt;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"&gt;
  
  &lt;sys:String x:Key="add"&gt;ADD&lt;/sys:String&gt;
  &lt;sys:String x:Key="key"&gt;Key&lt;/sys:String&gt;
  &lt;sys:String x:Key="stringValue"&gt;String Value&lt;/sys:String&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="MultiLanguage.MainWindow"
  x:Name="Window"
  Title="MainWindow"
  Width="640" Height="480"&gt;
  
  &lt;Grid x:Name="LayoutRoot" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="Auto"/&gt;
          &lt;ColumnDefinition Width="Auto"/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Button x:Name="btnAdd" Content="{DynamicResource add}" Height="48" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="10" Click="btnAdd_Click" /&gt;
      &lt;TextBlock Text="{DynamicResource key}" Height="40" Width="100" Grid.Column="0" Grid.Row="0" Margin="10"/&gt;
      &lt;TextBlock Text="{DynamicResource stringValue}" Height="40" Width="100" Grid.Column="0" Grid.Row="1" Margin="10"/&gt;
      &lt;TextBox x:Name="txtKey" Height="40" Width="200" Grid.Column="1" Grid.Row="0" Margin="10"/&gt;
      &lt;TextBox x:Name="txtStringValue" Height="40" Width="200" Grid.Column="1" Grid.Row="1" Margin="10"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>with above code, i get the following window</p>
  
  <p><img src="https://i.stack.imgur.com/QgZ59.png" alt="enter image description here"></p>
  
  <pre><code>private void btnAdd_Click(object sender, RoutedEventArgs e)
      {
          AddKeyValue(txtKey.Text, txtStringValue.Text);
      }
  
  
      private void AddKeyValue(object key, object value)
      {
          // load the resource dictionary
          var rd = new System.Windows.ResourceDictionary();
          rd.Source = new System.Uri("pack://application:,,,/LanguageResources;component/EnglishResources.xaml", System.UriKind.RelativeOrAbsolute);
  
          // add the new key with value
          rd.Add(key, value);
  
          // now you can save the changed resource dictionary
          var settings = new System.Xml.XmlWriterSettings();
          settings.Indent = true;
          var writer = System.Xml.XmlWriter.Create(@"EnglishResources.xaml", settings);
          System.Windows.Markup.XamlWriter.Save(rd, writer);
      }
  </code></pre>
  
  <p>If i click Add button the value should be inserted in the resource dictionary which(EnglishResources.xaml) is already i have. But it is not insert. Please help me out.</p>
  
  <p><strong>I need Like</strong></p>
  
  <pre><code>&lt;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"&gt;
  
  &lt;sys:String x:Key="add"&gt;ADD&lt;/sys:String&gt;
  &lt;sys:String x:Key="key"&gt;Key&lt;/sys:String&gt;
  &lt;sys:String x:Key="stringValue"&gt;String Value&lt;/sys:String&gt;
  
  &lt;!-- the value should be inserted here. But not Insert--&gt;
  
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p><strong>After Added</strong></p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:s="clr-namespace:System;assembly=mscorlib"&gt;
  
  &lt;s:String x:Key="add"&gt;ADD&lt;/sys:String&gt;
  &lt;s:String x:Key="key"&gt;Key&lt;/sys:String&gt;
  &lt;s:String x:Key="stringValue"&gt;String Value&lt;/sys:String&gt;
  
  &lt;!-- the value should be inserted here. But not Insert--&gt;
  
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>After i added the value into resource dictionary i get result like above. the <strong>sys</strong> is changed into to <strong>s</strong> and name space are merged as a line.</p>