WPF:由于只读状态,无法通过隐藏的代码更改样式 SolidColorBrush [英] WPF: cannot change style SolidColorBrush via code behind because read-only state

查看:27
本文介绍了WPF:由于只读状态,无法通过隐藏的代码更改样式 SolidColorBrush的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在 Window.Resources 里面我有这个 SolidColorBrush:

So inside Window.Resources i have this SolidColorBrush:

在单独的文件 (GridViewColumnHeader.xaml) 中,我有这个 Style:

And in separate file (GridViewColumnHeader.xaml) i have this Style:

<Style x:Key="ListViewHeaderDefaultStyle" TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                <Border BorderThickness="0,0,0,1" BorderBrush="Gray" Background="Transparent">
                    <TextBlock 
                            x:Name="ContentHeader"
                            Text="{TemplateBinding Content}"
                            Padding="0,5,0,0"
                            Width="{TemplateBinding Width}"
                            TextAlignment="Left"
                            Margin="5,0,0,0"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Foreground" Value="{DynamicResource GridViewColumnHeaderForegroundColor}" />
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="FontSize" Value="12" />
</Style>

现在我想做的是通过后面的代码改变我的 Style Foreground 颜色:

Now want i try to do is change my Style Foreground color via code behind:

SolidColorBrush solidColorBrush = (SolidColorBrush)this.TryFindResource("GridViewColumnHeaderForegroundColor");
if (solidColorBrush != null)
    solidColorBrush.Color = Colors.Black;

但是由于某种原因得到了这个InvalidOperationException:

But from some reason got this InvalidOperationException:

附加信息:无法设置对象#FFDCDCDC"的属性因为它处于只读状态.

Additional information: Cannot set a property on object '#FFDCDCDC' because it is in a read-only state.

推荐答案

您可以定义另一个画笔并将其分配给样式的前景,而不是更改您的资源画笔.

Instead of changing your Resource Brush, you could define another brush and assign it to the foreground of your style.

但是,还有另一种方法可以实现您的目标.看看这里.

However, there is another way of accomplishing your goal. Have a look here.

首先,您还应该将使用的样式声明为动态资源.

First of all you should declare the used style as dynamicresource aswell.

示例:

<ListView VerticalAlignment="Bottom" Height="63" IsSynchronizedWithCurrentItem="True">
    <ListView.View>
        <GridView x:Name="test" ColumnHeaderContainerStyle="{DynamicResource ListViewHeaderDefaultStyle}" >
            <GridViewColumn Header="header1"/>
        </GridView>
    </ListView.View>
</ListView>

现在您可以通过像这样更改资源画笔来简单地更改前景色:

Now you can simply change the Foreground Color by changing the resource brush like that:

this.Resources["GridViewColumnHeaderForegroundColor"] = Brushes.Black;

这篇关于WPF:由于只读状态,无法通过隐藏的代码更改样式 SolidColorBrush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆