Tono Nam
Tono Nam

Reputation: 36058

Remove usercontrol resources

I change the style of my user control by changing resource dictionaries. In other words I have:

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>

UserControl1:

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

Code Behind

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 );
        }
    }
}

Now I am able to change themes dynamically by calling the method: ChangeTheme


The problem that I have now is that if I place:

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

on UserControl1 The method ChangeTheme no longer works. I am looking for a method that does something like:

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

Upvotes: 1

Views: 1176

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

You are setting your a Dictionary in the Xaml without any MergedDictionary's so when you create your Merged Dictinarys they are being overridden by the base Dictionary. You can try one of two things.

The first being is to Create a MergedDictionary in your UserControls Xaml. This will work without changing your CodeBehind.

i.e.

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

The second would be to assign your newly created ResourceDictionary to the UserControls Resources thus overwriting the pre-existing ResourceDictionary. This will work without changing your Xaml.

i.e.

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

Upvotes: 3

Related Questions