tags:

views:

38

answers:

2

Is there a way to change(and apply) a style dynamically in WPF?

Say I have the style declared in XAML:

    <Style TargetType="local:MyLine" 
           x:Key="MyLineStyleKey" x:Name="MyLineStyleName">
        <Setter Property="Fill" Value="Pink"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Fill" Value="Blue" />                    
            </Trigger>
        </Style.Triggers>
    </Style>
  1. In a moment, I need to change the Pink color, to, say Green, and all lines with style MyLineStyleKey became Green. A line is Pink when released, and Blue when selected... Now, I needed to change the unselected property(Pink to Green)..., so this is not just setting it to an other color, the trigger (selection>Blue) will not work anymore...Is that possible? How?

  2. Is that possible to bind to the Pink color in the Style, say, to a Button background, that will reflect the currently used style color?

EDIT:
For 1 I tried:

Style s = (Style)this.Resources["MyLineStyleKey"];

(s.Setters[0] as Setter).Value = background;
(s.Setters[1] as Setter).Value = background;

but an exception occured:

After a 'SetterBase' is in use (sealed), it cannot be modified.

+1  A: 

A style can only be modified before its first use. From MSDN:

A style is sealed when another style is based on it or when it is applied for the first time.

Instead, you could create a new style based on your existing style, and override the properties you want:

Style newStyle = new Style();
newStyle.BasedOn = originalStyle;
newStyle.TargetType = typeof(MyLine);
Brush blue = new SolidColorBrush(Colors.Blue);
newStyle.Setters.Add(new Setter(Shape.FillProperty, blue));
newStyle.Setters.Add(new Setter(Shape.StrokeProperty, blue));
Thomas Levesque
oh... I has a style with a trigger... A line was Pink when released, and Blue when selected... Now, I needed to change the unselected property..., so this is not just setting it to an other color, the trigger (selection>Blue) will not work anymore...
serhio
updated the question with details..
serhio
hm... I suppose, if I will detach all lines from the style, I will not however be able to modify it, once used...
serhio
+2  A: 

Create a brush as a resource

<SolidColorBrush x:Key="MyFillBrush" Color="Pink" />

and refer to that in your style

<Style x:Key="MyShapeStyle" TargetType="Shape">
    <Setter Property="Fill" Value="{DynamicResource MyFillBrush}" />
</Style>
...
<!-- Then further down you may use it like this -->
<StackPanel Width="100">
    <Rectangle Style="{StaticResource MyShapeStyle}" Height="50" Margin="8" />
    <Rectangle Style="{StaticResource MyShapeStyle}" Height="50" Margin="8" />
    <Ellipse Style="{StaticResource MyShapeStyle}" Height="50" Margin="8" />
    <Button Content="Click to change color" Click="Button_Click" Margin="8" />
</StackPanel>

Now to change the color of all shapes that use the "MyShapeStyle" style, you can do the following from your code-behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Random r = new Random();
    this.Resources["MyFillBrush"] = new SolidColorBrush(Color.FromArgb(
          0xFF, 
          (byte)r.Next(255), 
          (byte)r.Next(255), 
          (byte)r.Next(255)));
}

The thing that makes this work is the fact that you use a DynamicResource for the brush reference in your style - this tells WPF to monitor that resource for changes. If you use a StaticResource instead, you won't get this behavior.

Isak Savo
A question: Can I pass a dynamicBrush to a WPF userControl?
serhio
I don't know what you mean with "Pass" but you can certainly reference it from your user control even it is defined on a control higher up in the hierarchy (like on the window).
Isak Savo