如何在运行时更改颜色主题

时间:2012-11-16 21:24:38

标签: wpf styles

我正在研究WPF应用程序,我正在尝试在运行时更改颜色。我尝试了在这个帖子中发布的解决方案:WPF: Changing Resources (colors) from the App.xaml during runtime;但是,解决方案似乎不起作用。

我定义了3个资源词典

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>

所有3个资源词典都位于我的应用程序根目录中名为“Resources”的子文件夹中。

我已将Colors1资源字典添加到Application资源(在Application.xaml文件中),以便默认加载它。

这是我的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>

我的应用程序中有一个名为“Window1”的窗口,其中包含一个Grid和一个允许您在资源字典之间切换的ComboBox。

这是Window1的xaml代码:

<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>

这是用于处理ComboBox的Selection Changed事件的VB.NET代码,它应该改变窗口中Grid的背景颜色:

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

我不确定我做错了什么。

如何更改颜色?

*编辑:* 好吧,在发布这个问题之后,我发现如果我将画笔移出颜色资源文件并移动到Window资源中,颜色会在切换资源时发生变化。

但是,如果我将画笔移动到另一个名为ColorBrushes的ResourceDictionary,并简单地切换Color ResourceDictionaries,它就不会改变颜色。

在我的主应用程序中,我的所有画笔都在ResourceDictionaries中定义,而不是在windows或用户控件本身....所以这对我来说仍然是一个问题。

1 个答案:

答案 0 :(得分:1)

我通过更改Colors字典,然后将Brushes字典更改为Brushes字典来解决问题。

像这样:

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

相关问题