locked
Binding Foreground Brush to a Static Resource.

    Question

  • I'm not sure if this is possible, but these templates and resources reside in my app.xaml Application.Resources for use by my entire application. One is a template for a group header and the other a resource define to hold the theme color.

    <DataTemplate x:Key="headerTemplate">
                    <Grid x:Name="GroupHeaderGrid" HorizontalAlignment="Stretch"  VerticalAlignment="Center">
    
                        <TextBlock  Text='{Binding Key}' VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="{Binding Source={StaticResource ThemeBrush}, Mode=OneWay}" MinWidth="30" FontSize="22"/>
                      
                </Grid>
    </DataTemplate>

    Here the values are defined for the color and brush at startup in the XAML.

                <ResourceDictionary.ThemeDictionaries>
                   
                    <ResourceDictionary x:Key="Light">
                        <Color x:Key="ThemeColor">#310B85</Color>
                        <SolidColorBrush x:Key="ThemeBrush" Color="{StaticResource ThemeColor}" />
                    </ResourceDictionary>
                </ResourceDictionary.ThemeDictionaries>

    Then when the user selects a new theme color, I updated the XAML resources. The resources now hold the correct values, however how can I get the group header template to update with the new values below? There is no x:name where I can manually update as it's a template used for various UIElements. If it were C# normally I would have a getter and setter for the value and attach to NotifyPropertyChanged but this is done in XAML

    public static void SetThemeColor(Color color)
            {
                App.Current.Resources["ThemeColor"] = color;
                App.Current.Resources["ThemeBrush"] = new SolidColorBrush(color);
            }

    Saturday, April 4, 2015 9:00 PM

Answers

  • FYI I resolved this, easy fix was just to refer to it as a themeresource instead.

    And as for updating the brush, pull the current value, save it as the object it is (Brush) and alter the brush directly on the SolidColorBrush object, no need to reinsert a new brush.

    • Marked as answer by craig91 Wednesday, April 8, 2015 10:30 AM
    Wednesday, April 8, 2015 10:30 AM

All replies

  • You should try creating a style for the group header template and use your theme colour resources in it. Then it should update automatically when you change the resources' values.


    Visit http://blog.grogansoft.com/ for Windows development fun.

    Sunday, April 5, 2015 2:22 AM
  • You should try creating a style for the group header template and use your theme colour resources in it. Then it should update automatically when you change the resources' values.


    Visit http://blog.grogansoft.com/ for Windows development fun.

    <Style x:Key="GroupHeaderTextStyle" TargetType="TextBlock">
                    <Setter Property="FontSize" Value="22"/>
                    <Setter Property="Foreground" Value="{StaticResource ThemeBrush}"/>
                </Style>
                
                <DataTemplate x:Key="headerTemplate">
                    <Grid x:Name="GroupHeaderGrid" HorizontalAlignment="Stretch"  VerticalAlignment="Center">
    
                        <TextBlock  Text='{Binding Key}' Style="{StaticResource GroupHeaderTextStyle}"/>
                </Grid>
                </DataTemplate>

    I tried what you said to some extent, created a style for the TextBlock and used the static resource themebrush in it, though it still does not update when the ThemeBrush resource has been changed. 

    Sunday, April 5, 2015 11:04 PM
  • FYI I resolved this, easy fix was just to refer to it as a themeresource instead.

    And as for updating the brush, pull the current value, save it as the object it is (Brush) and alter the brush directly on the SolidColorBrush object, no need to reinsert a new brush.

    • Marked as answer by craig91 Wednesday, April 8, 2015 10:30 AM
    Wednesday, April 8, 2015 10:30 AM