Erez
Erez

Reputation: 6446

Wpf - Edit Resource in run time

I have this xaml:

 <Grid x:Name="root">
    <Grid.Resources>
        <Style x:Key="btnStyle">
            <Setter Property="Button.Background" Value="LightBlue"/>
        </Style>
    </Grid.Resources>
    <Button Style="{DynamicResource btnStyle}"></Button>
</Grid>

and my question is how can I change the btnStyle setter value to Red from code behind?

Upvotes: 0

Views: 892

Answers (1)

Jon
Jon

Reputation: 437336

This is the direct answer to your question:

var style = (Style) this.root.findResource("btnStyle");
style.Setters.Item[0].Value = Brushes.Red;

But what are you trying to accomplish exactly?

Upvotes: 1

Related Questions