简体   繁体   English

如何以编程方式添加更多样式设置器? 当我尝试时,出现InvalidOperationException异常(正在使用SetterBaseCollection)

[英]How to add further style setters programmatically? When I try I got an InvalidOperationException exception (SetterBaseCollection is in use)

I have several style definitions in my App.xaml file. 我的App.xaml文件中有几个样式定义。 Like this: 像这样:

<Application x:Class="MyClient.App" ... >
    <Application.Resources>
        <SolidColorBrush x:Key="color1" Color="#FF7D7D" />
        <SolidColorBrush x:Key="color2" Color="#FF7D7E" />

        <Style x:Key="styleFor1" TargetType="charting:ColumnDataPoint">
            <Setter Property="Background" Value="{StaticResource color1}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="charting:ColumnDataPoint">
                        <Grid>
                            <Rectangle>
                                <Rectangle.Fill>
                                    <LinearGradientBrush>
                                        <GradientStop Color="#ffff3737" Offset="0" />
                                        <GradientStop Color="#80000000" Offset="1" />
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ToolTipService.ToolTip>
                                <StackPanel>
                                    <ContentControl Content="VALUES:" FontWeight="Bold" />
                                    <ContentControl Content="{TemplateBinding FormattedIndependentValue}" />
                                    <ContentControl Content="{TemplateBinding FormattedDependentValue}" />
                                </StackPanel>
                            </ToolTipService.ToolTip>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

I generate a chart. 我生成一个图表。 And give its DataPointStyle this: 并为其提供DataPointStyle:

Style dpStyle = Application.Current.Resources["styleFor1"]

after that, I would like to add some more Setters to this dpStyle. 之后,我想向此dpStyle添加更多的Setter。 And when it is done I set the chart's DataPointStyle to this dpStyle. 完成后,将图表的DataPointStyle设置为此dpStyle。 And then I got the exception. 然后我得到了例外。 What should I do? 我该怎么办? Please guide me. 请指导我。

UPDATE: 更新:

Exception details (might needed): 异常详细信息(可能需要):

InvalidOperationException was unhandled 未处理InvalidOperationException

{"After a 'SetterBaseCollection' is in use (sealed), it cannot be modified."} {“使用(密封)'SetterBaseCollection'之后,将无法对其进行修改。”}

TargetSite: {Void CheckSealed()} TargetSite:{无效CheckSealed()}

I figured out the solution. 我想出了解决方案。 I had to use this overload of the consturctors of the Style class: 我不得不使用Style类的构造函数的此重载:

public Style(Type targetType, Style basedOn);

Simply passing it the Style from the Application.Current... solves the problem. 只需从Application.Current ...传递样式即可解决此问题。 Cool. 凉。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试将Metadatatype与viewmodel一起使用时,出现InvalidOperationException - I get InvalidOperationException when I try to use Metadatatype with a viewmodel 如何将自定义样式与设置器和触发器一起使用? - How can i use a custom style with setters and triggers? 当尝试比较 linq 中的日期时,我在 .net 中遇到了这个异常 - i got this exception in .net when try to compare date in linq 当我尝试使用Web API 2的属性路由时,为什么会出现InvalidOperationException? - Why do I get an InvalidOperationException when I try to use attribute routing with Web API 2? 为什么在尝试渲染此.NET视图时会获得此“ InvalidOperationException”? - Why I obtain this “InvalidOperationException” when I try to render this .NET view? 我为dataGridCell添加了样式,但是当我尝试找到它时,出现了找不到样式的异常 - I added style for dataGridCell, but when I try to find it, exception is appearing that style didn't find 当我尝试从WPF中的RichTextBox手动删除图像时出现InvalidOperationException - InvalidOperationException when I try to delete an image manually from RichTextBox in WPF 为什么我在尝试使用 AutoMapper 时会出现异常? - Why I get exception when I try to use AutoMapper? 尝试借助OleDb从数据库读取数据时出现异常 - I got exception when try to read data from database with the help of OleDb 为什么在使用backgroundworker时出现InvalidOperationException异常? - Why i'm getting exception InvalidOperationException when using backgroundworker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM