[英]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.