Delete resource dictionary and add another one in WPF

I have two resource dictionaries. One of them is called ResDictGlass.xaml, and the other is ResDictNormal.xaml. Both have the same properties and different meanings. For instance,

ResDictGlass.xaml has one style:

<Style x:Key="StyleTitleText" TargetType="TextBlock">
    <Setter Property="FontFamily" Value="Arial" />
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Foreground" Value="Green" />
</Style>

Same style in ResDictNormal.xaml:

<Style x:Key="StyleTitleText" TargetType="TextBlock">
    <Setter Property="FontFamily" Value="Tahoma" />
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Foreground" Value="WhiteSmoke" />
</Style>

I installed the text block in xaml as:

 <TextBlock Style="{DynamicResource StyleTextblock}" Text="Prod.Code" VerticalAlignment="Top" />

I want to switch between these styles at runtime. I do like this:

           case "normal":
               ResourceDictionary ResDict1 = new ResourceDictionary();
               ResDict1.Source = new Uri("/ResDictNormal.xaml", UriKind.RelativeOrAbsolute);
               Application.Current.Resources.MergedDictionaries.Add(ResDict1);
               break;

           case "flip":
               ResourceDictionary ResDict2 = new ResourceDictionary();
               ResDict2.Source = new Uri("/ResDictGlass.xaml", UriKind.RelativeOrAbsolute);
               Application.Current.Resources.MergedDictionaries.Add(ResDict2);
               break;

Is this the right approach? Do I need to delete the current dictionary and then add the dictionary?

+5
source share
1 answer

Yes, you would like one of the two dictionaries to be combined in the application, and not both. Otherwise, ambiguous resourecs will cause an error by their link.

, DynamicResource over StaticResource, (.. ).

, .

+4

All Articles