Reputation: 3938
I am working on a WPF application and I am trying to change colors during runtime. I tried the solution posted in this thread: WPF: Changing Resources (colors) from the App.xaml during runtime; however, the solution doesn't seem to work.
I have 3 resource dictionaries defined
Colors1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BaseColor">#ffaa01</Color>
<SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" />
</ResourceDictionary>
Colors2.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BaseColor">#aaff01</Color>
<SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" />
</ResourceDictionary>
Colors3.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BaseColor">#aa01ff</Color>
<SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" />
</ResourceDictionary>
All 3 resource dictionaries are located in a sub folder within the root of my application called "Resources".
I have added the Colors1 resource dictionary to the Application resources (in the Application.xaml file) so that it is loaded by default.
Here is my Application.xaml:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="Colors1" Source="Resources/Colors1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
I have a Window in my application called "Window1" which contains a Grid and a ComboBox that allows you to switch between resource dictionaries.
Here is the xaml code for the Window1:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Picture Orders" Height="600" Width="600"
xmlns:myProj="clr-namespace:TryingWPF">
<Window.Resources>
<x:Array x:Key="ResourceNames" Type="sys:String"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String>Colors1</sys:String>
<sys:String>Colors2</sys:String>
<sys:String>Colors3</sys:String>
</x:Array>
</Window.Resources>
<Grid x:Name="VisualRoot" Background="{DynamicResource BackgroundColorBrush}">
<ComboBox x:Name="ResourceOptions"
ItemsSource="{StaticResource ResourceNames}"
SelectedIndex="0"
VerticalAlignment="Top"
HorizontalAlignment="Center"
SelectionChanged="ResourceOptions_SelectionChanged"/>
</Grid>
</Window>
And here is the VB.NET code that handles the Selection Changed event for the ComboBox that should change the background color for the Grid in the Window:
Public Class Window1
Private Sub ResourceOptions_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Dim resourceSource As String = String.Format("pack://application:,,,/Resources/{0}.xaml", ResourceOptions.SelectedValue)
Dim newResourceDictionary As New ResourceDictionary()
newResourceDictionary.Source = New Uri(resourceSource)
Application.Current.Resources.MergedDictionaries.RemoveAt(0)
Application.Current.Resources.MergedDictionaries.Insert(0, newResourceDictionary)
End Sub
End Class
I'm not sure what I'm doing wrong.
How do I get the colors to change?
*Edit: * Well, right after posting this question, I discovered that if I moved the Brush out of the color resource file and into the Window resources, the color would change upon switching resources.
However, If I move the brush to another ResourceDictionary, called ColorBrushes, and simply switch the Color ResourceDictionaries, it would not change colors.
In my main application, all of my brushes are defined in ResourceDictionaries, not within the windows or user controls themselves....so this is still a problem for me.
Upvotes: 1
Views: 4023
Reputation: 3938
I solved the problem by changing the Colors dictionary and then changing the Brushes dictionary to the Brushes dictionary.
Like so:
Public Class Window1
Private Sub ResourceOptions_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Dim resourceSource As String = String.Format("pack://application:,,,/Resources/{0}.xaml", ResourceOptions.SelectedValue)
Dim newResourceDictionary As New ResourceDictionary()
newResourceDictionary.Source = New Uri(resourceSource)
Application.Current.Resources.MergedDictionaries.RemoveAt(0)
Application.Current.Resources.MergedDictionaries.Insert(0, newResourceDictionary)
Dim brushesResourceSource As String = "pack://application:,,,/Resources/ColorBrushes.xaml"
Dim newBrushesResourceDictionary As New ResourceDictionary()
newBrushesResourceDictionary.Source = New Uri(brushesResourceSource)
Application.Current.Resources.MergedDictionaries.RemoveAt(1)
Application.Current.Resources.MergedDictionaries.Insert(1, brushesResourceSource)
End Sub
End Class
-Frinny
Upvotes: 2