title: WPF dynamically referenced application resources not updateable at runtime
body: <p>I have this very small test app that explores object binding in WPF. I am binding to instances of this <code>class MyData { public string Info { get; set; } }</code> local class.
  I am exploring binding to a local object in xaml, setting data context at runtime and modifying resource instances at runtime.
  I am using both window and application resources to see their impact, as defined below:</p>
  
  <p><em>App.xaml resources</em></p>
  
  <pre><code>&lt;local:MyData x:Key="myAppData" Info="App resource information" /&gt;
  &lt;local:MyData x:Key="myAppEmptyData" /&gt;
  &lt;local:MyData x:Key="myAppBogusData" Info="-" /&gt;
  </code></pre>
  
  <p><em>Window.xaml resources</em></p>
  
  <pre><code>&lt;local:MyData x:Key="myWindowData" Info="Window resource information" /&gt;
  &lt;local:MyData x:Key="myWindowEmptyData" /&gt;
  </code></pre>
  
  <p>The my[App|Window]EmptyData have a null Info member, and I am trying to modify it at runtime. I am doing this on window load:</p>
  
  <pre><code>(this.Resources["myWindowEmptyData"] as MyData).Info = "window resource info set on runtime";
  (Application.Current.Resources["myAppEmptyData"] as MyData).Info = "app resource info set on runtime";
  (Application.Current.Resources["myAppBogusData"] as MyData).Info = "app resource info set on runtime";
  this.lblDataContextSetAtRuntime.DataContext = new MyData() { Info = "data context set at runtime" };
  </code></pre>
  
  <p>The code for the main window below. Green labels are expected behavior. Blue labels represent unmodified resources that one expects not to get modified (static + window). Red labels are unexpected behavior. See inline comments too.</p>
  
  <p><em>MainWindow.xaml</em></p>
  
  <pre><code>    &lt;Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
          DataContext="{StaticResource myWindowData}"&gt;
      &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition Height="1*" /&gt;&lt;RowDefinition Height="1*" /&gt;&lt;RowDefinition Height="1*" /&gt;&lt;RowDefinition Height="1*" /&gt;&lt;RowDefinition Height="1*" /&gt;&lt;RowDefinition Height="1*" /&gt;&lt;RowDefinition Height="1*" /&gt;&lt;RowDefinition Height="1*" /&gt;
      &lt;/Grid.RowDefinitions&gt;   
      &lt;!--this uses data context from the grid--&gt;
      &lt;Label Content="{Binding Path=Info}" Background="Green" Grid.Row="0" /&gt;
      &lt;!--normal to not be updateable since window resources--&gt;
      &lt;Label DataContext="{StaticResource myWindowEmptyData}" Content="{Binding Path=Info}" Background="CadetBlue" Grid.Row="1" x:Name="lblStaticWindowResource" /&gt;
      &lt;Label DataContext="{DynamicResource myWindowEmptyData}" Content="{Binding Path=Info}" Background="CadetBlue" Grid.Row="2" x:Name="lblDynamicWindowResource" /&gt;
      &lt;!--data context set at runtime--&gt;
      &lt;Label Content="{Binding Path=Info}" Grid.Row="3" Background="Green" x:Name="lblDataContextSetAtRuntime" /&gt;
      &lt;!--uses data context from app--&gt;
      &lt;Label DataContext="{StaticResource myAppData}" Content="{Binding Path=Info}" Background="Green" Grid.Row="4" x:Name="lblAppData" /&gt;
      &lt;!--static app resource not modifiable--&gt;
      &lt;Label DataContext="{StaticResource myAppEmptyData}" Content="{Binding Path=Info}" Background="CadetBlue" Grid.Row="5" x:Name="lblStaticAppResource"/&gt;
      &lt;!--DYNAMIC APP RESOURCE SHOULD GET MODIFIED--&gt;
      &lt;Label DataContext="{DynamicResource myAppEmptyData}" Content="{Binding Path=Info}" Background="Red" Grid.Row="6" x:Name="lblDynamicEmptyAppResource" /&gt;
      &lt;Label DataContext="{DynamicResource myAppBogusData}" Content="{Binding Path=Info}" Background="Red" Grid.Row="7" x:Name="lblDynamicBogusAppResource" /&gt;
    &lt;/Grid&gt;
  </code></pre>
  
  <p>Even though in the debugger window one notices the resources are modified
  <img src="https://i.stack.imgur.com/PZ8ik.png" alt="enter image description here"></p>
  
  <p>, at runtime there's no content for the blue and red labels. I thought there was an issue with the resources with null property, bit not even myAppBogusData gets updated.</p>
  
  <p><img src="https://i.stack.imgur.com/a2Gzt.png" alt="enter image description here"></p>