1

リソース ディクショナリを変更して、ユーザー コントロールのスタイルを変更します。言い換えれば、私は持っています:

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

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

UserControl1ChangeThemeメソッドが機能しなくなりました。次のような方法を探しています:

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

1 に答える 1

3

MergedDictionary を使用せずに Xaml で Dictionary を設定しているため、Merged Dictinary を作成すると、ベース Dictionary によってオーバーライドされます。2つのうちの1つを試すことができます。

1 つ目は、UserControls Xaml で MergedDictionary を作成することです。これは、CodeBehind を変更せずに機能します。

すなわち

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

2 つ目は、新しく作成した ResourceDictionary を UserControls リソースに割り当てて、既存の ResourceDictionary を上書きすることです。これは、Xaml を変更しなくても機能します。

すなわち

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 に答える