Question

I am trying to make a library (with WPF) that will contain some of my basic templates for windows , buttons and some another new costume controls and templates for later use in projects .

I am still new to WPF . I understood that i should create a new ResourceDictionary in my external library and put there all my styles and their dynamic variables . And later make a MergedResourceDictionary in my main application project that will give me access to use my usual styles and controls .

Well i success to create a basic experimental style with a template which uses two variables as DynamicResource , Just for testing i made a <Brush x:Key="brush_back_standard">RoyalBlue</Brush> . This is used for regular back coloring . Then i declared to use this brush dynamically in the style itself : <Grid Background="{DynamicResource brush_back_standard}">, And It works well .

My problem begins when i am trying to change the brush value a runtime : Style.Resources[ "brush_back_standard" ] = Brushes.Aqua;. To simulate the case that the user will change the theme in the settings of the application later at runtime . So that didn't work at all , and i got the exception : ResourceDictionary is read-only and cannot be modified. . So if it's really readonly then it's sort of useless to me because the user cannot change the template at runtime . I need to find some way to let the user change anything in the window template at run time .

Full style code (external library) :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib">
<Style x:Key="window_standard"
       TargetType="{x:Type Window}">
    <Style.Resources>
        <system:Double x:Key="thickness_shadow">10</system:Double>
        <Brush x:Key="brush_back_standard">RoyalBlue</Brush>
    </Style.Resources>
    <Setter Property="WindowStyle"
            Value="None" />
    <Setter Property="AllowsTransparency"
            Value="True" />
    <Setter Property="BorderThickness"
            Value="{DynamicResource thickness_shadow}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid Background="{DynamicResource brush_back_standard}">
                    <AdornerDecorator>
                        <ContentPresenter />
                    </AdornerDecorator>
                    <ResizeGrip x:Name="WindowResizeGrip"
                                HorizontalAlignment="Right"
                                VerticalAlignment="Bottom"
                                Visibility="Collapsed"
                                IsTabStop="false" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="ResizeMode"
                             Value="CanResizeWithGrip">
                        <Setter TargetName="WindowResizeGrip"
                                Property="Visibility"
                                Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
        </Setter>
</Style>

Accessing the library in my main application project :

<Application x:Class="diamond.sandbox.executer"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="window_main.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/diamond.core;component/xaml/standard.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

The window_main.xaml which is magically affected by the style :

<Window x:Class="diamond.sandbox.window_main"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="window_main" Height="250" Width="500"
    Style="{DynamicResource window_standard}"
    WindowStartupLocation="CenterScreen"
    Loaded="initialise">

Initialise method (After window_main is loaded) :

private void initialise( object p_sender , RoutedEventArgs p_args )
    {
        Style.Resources[ "brush_back_standard" ] = Brushes.Aqua;
    }
Was it helpful?

Solution

Well, you can't modify a Style (not even its Resources) at all because it has been frozen. The Style is a Freezable, and will be frozen by WPF when added to a ResourceDictionary. Instead, you should just modify the Window's Resources:

private void initialise( object p_sender , RoutedEventArgs p_args )
{
    Resources[ "brush_back_standard" ] = Brushes.Aqua;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top