简体   繁体   中英

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. 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:

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

after that, I would like to add some more Setters to this dpStyle. And when it is done I set the chart's DataPointStyle to this dpStyle. And then I got the exception. What should I do? Please guide me.

UPDATE:

Exception details (might needed):

InvalidOperationException was unhandled

{"After a 'SetterBaseCollection' is in use (sealed), it cannot be modified."}

TargetSite: {Void CheckSealed()}

I figured out the solution. I had to use this overload of the consturctors of the Style class:

public Style(Type targetType, Style basedOn);

Simply passing it the Style from the Application.Current... solves the problem. Cool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM