1

我通过更改资源字典来更改用户控件的样式。换句话说,我有:

字典1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Grid">        
        <Setter Property="Background" Value="Green"></Setter>        
    </Style>

    <SolidColorBrush x:Key="Foo" Color="Blue"></SolidColorBrush>

</ResourceDictionary>

字典2.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Grid">        
        <Setter Property="Background" Value="Black"></Setter>
    </Style>

    <SolidColorBrush x:Key="Foo" Color="Orange"></SolidColorBrush>

</ResourceDictionary>

用户控件1:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="103" d:DesignWidth="101">                

    <Grid >
        <Ellipse Fill="{DynamicResource Foo}" />
    </Grid>
</UserControl>

代码背后

namespace WpfApplication1
{
    using System; using System.Windows; using System.Windows.Controls;

    public partial class UserControl1 : UserControl
    {
        public enum ControlTheme
        {
            Theme1 , Theme2
        }

        public UserControl1 ( )
        {
            InitializeComponent( );            
        }

        public void ChangeTheme ( ControlTheme theme )
        {
            Resources.MergedDictionaries.Clear( );

            int dic = 2;

            if ( theme == ControlTheme.Theme1 )
                dic = 1; 

            ResourceDictionary rd = new ResourceDictionary( );
            rd.Source = new Uri( @"pack://application:,,,/WpfApplication1;component/Dictionary" + dic + ".xaml" );
            Resources.MergedDictionaries.Add( rd );
        }
    }
}

现在我可以通过调用方法动态更改主题:ChangeTheme


我现在遇到的问题是,如果我放置:

 <UserControl.Resources>
        <ResourceDictionary Source="Dictionary1.xaml" ></ResourceDictionary>
 </UserControl.Resources>

UserControl1上的ChangeTheme方法不再起作用。我正在寻找一种类似的方法:

  //PseudoCode
  var itemToRemove = this.UserControlResources.resources.where(x=> x.isDictionary==true);
  this.UserControlResources.Remove(itemToRemove);
4

1 回答 1

3

您在没有任何 MergedDictionary 的 Xaml 中设置您的字典,因此当您创建合并字典时,它们将被基本字典覆盖。您可以尝试以下两种方法之一。

第一个是在您的 UserControls Xaml 中创建 MergedDictionary。这将在不更改您的 CodeBehind 的情况下工作。

IE

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml" />
        </ResourceDictionary.MergedDictionaries> 
        </ResourceDictionary>
</UserControl.Resources>

第二个是将您新创建的 ResourceDictionary 分配给 UserControls 资源,从而覆盖预先存在的 ResourceDictionary。这将在不更改 Xaml 的情况下工作。

IE

public void ChangeTheme(ControlTheme theme)
{
    int dic = 2;

    if (theme == ControlTheme.Theme1)
        dic = 1;

    ResourceDictionary rd = new ResourceDictionary();
    rd.Source = new Uri(@"pack://application:,,,/WpfApplication1;component/Dictionary" + dic + ".xaml");
    this.Resources.Clear();
    this.Resources = rd; 
} 
于 2013-01-25T22:10:23.550 回答