Wpf - 在运行时编辑资源

时间:2010-12-01 17:38:28

标签: wpf resources styles

我有这个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>

我的问题是如何从后面的代码中将btnStyle setter值更改为Red?

1 个答案:

答案 0 :(得分:1)

这是您问题的直接答案:

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

但是你想要完成什么呢?