簡體   English   中英

如何以編程方式添加更多樣式設置器? 當我嘗試時,出現InvalidOperationException異常(正在使用SetterBaseCollection)

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

我的App.xaml文件中有幾個樣式定義。 像這樣:

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

我生成一個圖表。 並為其提供DataPointStyle:

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

之后,我想向此dpStyle添加更多的Setter。 完成后,將圖表的DataPointStyle設置為此dpStyle。 然后我得到了例外。 我該怎么辦? 請指導我。

更新:

異常詳細信息(可能需要):

未處理InvalidOperationException

{“使用(密封)'SetterBaseCollection'之后,將無法對其進行修改。”}

TargetSite:{無效CheckSealed()}

我想出了解決方案。 我不得不使用Style類的構造函數的此重載:

public Style(Type targetType, Style basedOn);

只需從Application.Current ...傳遞樣式即可解決此問題。 涼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM