如何更改window.resource样式setter?

时间:2014-02-26 07:40:36

标签: c# wpf styles setter

 <Window.Resources >
        <Style x:Name="stylepropery" x:Key="BaseContentControlStyle" TargetType="{x:Type ContentControl}">
            <Setter Property="Foreground" Value="{DynamicResource MyFillBrush}"  />
        </Style>

        <Style  TargetType="{x:Type Label}" BasedOn="{StaticResource BaseContentControlStyle}" />

     <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseContentControlStyle}" />
    </Window.Resources>

将普通字体颜色应用于所有标签和文本框现在我想从后面的代码更改字体的颜色但是有些不适用 我只想更改setter 属性值

 Setter setter = new Setter(ContentControl.ForegroundProperty, dt.Rows[0]["value"]);
 Style style = this.FindResource("BaseContentControlStyle") as Style;
 style.Setters.Add(setter);

我已经使用了这个但没有成功

2 个答案:

答案 0 :(得分:0)

试试这段代码可能对您有所帮助

Style style = new Style(typeof(ContentControl));

style.Setters.Add(new Setter(ContentControl.ForegroundProperty,Brushes.Green)); 资源[“BaseContentControlStyle”] =样式;

答案 1 :(得分:0)

            var converter = new System.Windows.Media.BrushConverter();
            var brush = (Brush)converter.ConvertFromString(dt.Rows[0]["value"].ToString());
            Style st = this.Resources["BaseContentControlStyle"] as Style;
            Random r = new Random();
            this.Resources["MyFillBrush"] = (new BrushConverter().ConvertFrom(dt.Rows[0]["value"].ToString()));

经过大量的谷歌搜索后,我找到了解决方案,我为未来的参考用户添加了这个解决方案,所以他们不必搜索很多东西:)