Question

Having the following very simple xaml:

<DocumentViewer Name="dv">
    <FixedDocument Name="fd" Loaded="fd_loaded">
        <FixedDocument.Resources>
            <Style x:Key="TestStyle">
                <Style.Setters>
                    <Setter Property="TextBlock.Foreground" Value="BlueViolet"/>
                </Style.Setters>
            </Style>
            <SolidColorBrush x:Key="foregroundBrush" Color="Orange"/>
        </FixedDocument.Resources>
        <PageContent Name="pc">
            <FixedPage Name="fp" Width="800" Height="600" Name="fp">
                <TextBlock Name="tb" Style="{DynamicResource TestStyle}">
                        Lorem ipsum
                </TextBlock>
                <TextBlock Foreground="{DynamicResource foregroundBrush}" Margin="20">
                        Lorem ipsum
                </TextBlock>
            </FixedPage>
        </PageContent>
    </FixedDocument>
</DocumentViewer>

The use of Dynamic Resources (which I actually need in a more complex situation) here doesn't work. Using Static Resources colors the TextBlocks in the desired colors. Moving the Resources to the level of the FixedPage also does the trick. But I would like to have one generic resource dictionary on a top level element (because of runtime changes the user can make for colours, fonts, etc.). Placing the resources on Application level also does work. But it's not an option for good reasons.

Anybody have any clue why this doesn't work. Does it have something to do with the Logical Tree from the TextBlock upwards?

MSDN Resources Overview states that:

The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.

  • If the element defines a Style property, the Resources dictionary within the Style is checked.
  • If the element defines a Template property, the Resources dictionary within the FrameworkTemplate is checked.

The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.

I also tried putting the Brush and the Style into the Resources of a (dummy) Style according to the above explanation of MSDN. But that didn't work either.

Really have the feeling that this can not be that complex, but most probably I oversee something. Any help is appreciated.

EDIT

When naming the TextBlock to "tb" and then using tb.FindResource("TestStyle") throws an exception. So that resource clearly can't be found. If I check out LogicalTreeHelper.GetParent(tb) and repeat that for the parents found I get the expected result: TextBlock > FixedPage > PageContent > FixedDocument ...

EDIT2

This works perfect. What's the difference with the XAML projected earlier?

<Window x:Class="WpfDynamicStyles2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <SolidColorBrush x:Key="resBrush" Color="Orange"></SolidColorBrush>
        </Grid.Resources>
            <StackPanel>
            <Button>
                <TextBlock Foreground="{DynamicResource resBrush}">Dummy text...</TextBlock>
            </Button>           
        </StackPanel>
    </Grid>
</Window>

EDIT3

private void fd_Loaded(object sender, RoutedEventArgs e)
{
    Object obj = pc.TryFindResource("foregroundBrush");
    obj = fp.TryFindResource("foregroundBrush");
    obj = tb.TryFindResource("foregroundBrush");
}

The dynamic resource placed on the Foreground property of the textbox cannot be resolved (the actual resource is at the FixedDocument.Resources level). Also using the TryFindResource in code behind works from pc (PageContent) but from fp (FixedPage) and tb (TextBlock) it cannot resolve the resource (obj is null). When using a Static Resource in the XAML Markup everything works fine.

Was it helpful?

Solution 2

By the way: the reason for this post had some more complex background. I really wanted to use a single resource dictionary whereby I could change colors, fonts, fontsizes, etc. dynamically during runtime (by an end-user). I am working on a tax form application. And the tax forms presented on screen for user input are resolving their resources at the Application level. So far so good.

But at the same time I want to present the user a print preview of the tax form where the same resourcedictionary (as object type, not as object instance) is used to define color schemes, fonts, fontsizes, etc. to be used for printing. Which can differ from the styling used for on screen user input. That's why I wanted to "attach" the resource dictionary on (for example) the FixedDocument level so that all pages in the document would refer to it. And when changing colors, fonts etc. all pages with common elements (TextBlocks, TextEditors, etc.) would respond to the change.

And then I became stuck...as described in this post.

For now I have a nice workaround by creating a specific instance of the resourcedictionary, storing it as a private variable and attach it to every individual page I put in the fixed document. It works and that already pleases me. But my programmers heart is still a little ached, because I would prefer to use a single instance of the resource dictionary and put it at some top level control so all controls in it can use it.

As a programmer we have to live with workarounds as well ;) If you have any further suggestions, I'm open to receive them. Thanx for your support thus far.

Greetz, Joep.

Also see: MSDN Forum Post about this issue

OTHER TIPS

Well going the style way is a good design but not necessary. To make dynamic color based same named (keyed) brushes, we can use as dynamic color dictionaries (NOT brush dictionaries)

The solution can be as below ...

  1. Create a single brush resource dictionary.
  2. Refer the color in the brush with DynamicResource attribute.
  3. Create multiple resource dictionaries with same keyed Color resource in each of them them.
  4. Based on user's requirement, clear and add into Application.Current.resources.MergedDictionaries.

Example

  1. Make a WPF project with a window that has following XAML....

     <Window.Resources>
       <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>                
            <ResourceDictionary Source="Resources/Window11Resources.xaml"/>
         </ResourceDictionary.MergedDictionaries>
       </ResourceDictionary>
     </Window.Resources>
     <DockPanel LastChildFill="True">
       <ComboBox DockPanel.Dock="Top" VerticalAlignment="Top"
              SelectionChanged="ComboBox_SelectionChanged"
              SelectedIndex="0">
        <ComboBox.ItemsSource>
            <Collections:ArrayList>
                <System:String>Orange</System:String>
                <System:String>Red</System:String>
                <System:String>Blue</System:String>
            </Collections:ArrayList>
        </ComboBox.ItemsSource>
    </ComboBox>
    
    <DocumentViewer>
        <FixedDocument>
            <PageContent>
                <FixedPage Width="793.76" Height="1122.56">
                    <TextBlock
                          FontSize="30"
                          Foreground="{StaticResource LabelColorBrush}"
                          Text="Test"/>
                </FixedPage>
            </PageContent>                
        </FixedDocument>
    </DocumentViewer>        
    

If you observe the window doesnt need to use anything which is dynamic. All refernces can remain static. So LabelColorBrush can be found in dictionary Window11Resources.xaml

  1. In Window11Resources.xaml dictionary add a dynamic color brush.

     <SolidColorBrush x:Key="LabelColorBrush"
                  Color="{DynamicResource DynamicColor}"/>
    
  2. Add following 3 color brush dictionaries in some folder from your project...

     <!-- Name = OrangeColorResource.xaml -->
     <ResourceDictionary 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
         <Color x:Key="DynamicColor">Orange</Color>
    </ResourceDictionary>
    
     <!-- Name = BlueColorResource.xaml -->
     <ResourceDictionary 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
         <Color x:Key="DynamicColor">Blue</Color>
    </ResourceDictionary>
    
     <!-- Name = RedColorResource.xaml -->
     <ResourceDictionary 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
         <Color x:Key="DynamicColor">Red</Color>
    </ResourceDictionary>
    

Note that the key remains the same i.e. DynamicColor.

  1. Now clear and recreate color dictionaries in App.Resources. I have done that in the code behind of Window.xaml.cs

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Application.Current.Resources.MergedDictionaries.Clear();
        Application.Current.Resources.MergedDictionaries.Add(
           new ResourceDictionary()
           { 
              Source = new Uri("Resources\\"
                               + ((ComboBox)sender).SelectedItem.ToString()
                               + "ColorResource.xaml",
                               UriKind.RelativeOrAbsolute) });
    }
    

Now as and when you select a color from the drop down, the dynamic color changes on the static resource brush. Note that there is no Style involved.

I hope this answers what you are asking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top