0

I have a template of an elliptical button in my "ButtonStyles.xaml" resource dictionary. This is part of the code:

<SolidColorBrush x:Key="brush" Color="Red"/>
<Style TargetType="Button" x:Key="MyButtonStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <VisualState.Setters>
                                    <Setter Target="ellipse.(Shape.Fill)" Value="{StaticResource ResourceKey=brush}">
                                    </Setter>
                                    <Setter Target="ellipse.(Shape.Stroke)" Value="{x:Null}"/>
                                </VisualState.Setters>
                            </VisualState>
                            [...]
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Ellipse x:Name="ellipse"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And here is the button in the MainPage:

<Button x:Name="toggle" Content="" HorizontalAlignment="Left" Height="132" Margin="40,146,0,0" VerticalAlignment="Top" Width="132" Style="{StaticResource MyButtonStyle}"/>

Now I'd like to change the brush resource from the c# code. To do this I tried as follows :

Application.Current.Resources["brush"] = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 125, 126, 126));

While the brush resource effectively changes the colour of the ellipse doesn't. I think that the problem is that I should not change the colour in the template but in the actual MainPage button. Is this the problem? How can I do that?

MasterArch
  • 63
  • 1
  • 5
  • If you want to change the fill dynamically in the code why are you making it a static resource? Why not bind it to a field? – Felix Castor Mar 05 '16 at 16:18
  • **This will help you somehow** [Ellipse Fill Color with c#](http://stackoverflow.com/questions/2451326/wpf-binding-to-change-fill-color-of-ellipse?rq=1) – CodeConstruct Mar 05 '16 at 16:55

1 Answers1

0

I got to a result following @felix-castor 's suggestion and this guide. I placed a DependencyProperty in the C# code and a Binding in the XAML code.

It works pretty well but it doesn't refresh the button when the property is changed and I have to change between VisualStates so as to make it update the fill. I'll post another question about it. But if you have a suggestion you could leave a comment.

MasterArch
  • 63
  • 1
  • 5