locked
Override resource color from common resource dictionary in custom controls RRS feed

  • Question

  • I have 10+ custom controls all referencing a common resource dictionary in SL3.

    The common resource dictionary has values for common colors used in its styles eg:

    <SolidColorBrush x:Key="GlyphBrush" Color="#FF53E245" />

    I want to be able to change this colour in the final application but after trying 3-4 different ways I've been unable to get it to work.

    I assumed that I would simply need to include a new resource dictionary in the final application that overrode this value, but since I reference this common dictionary in each control this has no effect, It seems like a don't have a common resource, rather each control has an instance of this common dictionary. Since the controls are compiled I can't change this without overriding their entire template.

    I tried to change the values programatical like so:

    var glyphBrush = (SolidColorBrush)Resources.MergedDictionaries[1]["GlyphBrush"];

    glyphBrush.Color = Colors.Orange;

    With no effect at all.

    Has anyone found a good solution? The main objective is to have a default style that requires nothing more then the custom control's dll and element added but easily override the simple colours used in its styling.

    The only method I can think of that would work would be to override the generic template and reference a completely different resource dictionary. This would be painful and tonnes of duplicate code.

    Thursday, February 25, 2010 11:02 PM

Answers

  • Hi,

      Em, IMO, static resources will only be read onetime.(when parse)

      But i tested and verified that controls using the same style share a same instance. So the suggestion here is keep a reference of your brush and change it.

          <UserControl.Resources>
            <SolidColorBrush Color="Red" x:Key="brush" > </SolidColorBrush>
        </UserControl.Resources>
        <StackPanel x:Name="LayoutRoot" Background="White">
            <Rectangle x:Name="rect" Width="200" Height="100"  Fill="{StaticResource brush}"></Rectangle>
            <Rectangle x:Name="rect1" Width="100" Height="200"  Fill="{StaticResource brush}"></Rectangle>
            <Button Click="Button_Click" Content="click" Width="100" Height="100"></Button>   
        </StackPanel>

       In my click event , i use rect.Fill get the reference and then change the color will effect both rectangle.

    Best Regards

    Sunday, February 28, 2010 10:33 PM
  • What I understand is, "You want to change color of a brush, after brush is applied to the control. So you get a differently styled control."

    However, staticResources are references only once, when a control is created. Changing them after the control is created has no effect. So swaping the resource dictionary has no effect.

    Not sure, but you can try, getting the brush object and changing the color of the same brush object.

    Friday, February 26, 2010 12:43 AM

All replies

  • What I understand is, "You want to change color of a brush, after brush is applied to the control. So you get a differently styled control."

    However, staticResources are references only once, when a control is created. Changing them after the control is created has no effect. So swaping the resource dictionary has no effect.

    Not sure, but you can try, getting the brush object and changing the color of the same brush object.

    Friday, February 26, 2010 12:43 AM
  • Hi,

      Em, IMO, static resources will only be read onetime.(when parse)

      But i tested and verified that controls using the same style share a same instance. So the suggestion here is keep a reference of your brush and change it.

          <UserControl.Resources>
            <SolidColorBrush Color="Red" x:Key="brush" > </SolidColorBrush>
        </UserControl.Resources>
        <StackPanel x:Name="LayoutRoot" Background="White">
            <Rectangle x:Name="rect" Width="200" Height="100"  Fill="{StaticResource brush}"></Rectangle>
            <Rectangle x:Name="rect1" Width="100" Height="200"  Fill="{StaticResource brush}"></Rectangle>
            <Button Click="Button_Click" Content="click" Width="100" Height="100"></Button>   
        </StackPanel>

       In my click event , i use rect.Fill get the reference and then change the color will effect both rectangle.

    Best Regards

    Sunday, February 28, 2010 10:33 PM