简体   繁体   English

在代码中使用在xaml中声明的自定义`Style`会引发运行时错误

[英]Using in code-behind a custom `Style`, declared in xaml, throws run-time error

I declare a style in xaml that I need to use and apply to a user control in code behind and when I use the same style twice the following error throws: 我在xaml中声明了我需要使用的样式,并在后面的代码中将其应用于用户控件,当我两次使用相同样式时,会抛出以下错误:

Element already has a logical parent. 元素已具有逻辑父级。 It must be detached from the old parent before it is attached to a new one. 必须先将其与旧的父级分离,然后再附加到新的父级。

What am I doing wrong? 我究竟做错了什么? I need to create multiple controls of the same user-control-type in code behind and apply one and the same Style to it. 我需要在后面的代码中创建相同用户控件类型的多个控件,并对其应用一个和相同的Style

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking"
    xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
    x:Class="MyChartControl.MainWindow"
    Title="MainWindow" Height="655" Width="1020">

<Window.Resources>

   <Style x:Key="SciChartSurfaceStyle" TargetType="{x:Type s:SciChartSurface}">

        <Setter Property="XAxis">
            <Setter.Value>
                <s:DateTimeAxis Visibility="Visible"
                                TextFormatting="dd/MM/yyyy"
                                SubDayTextFormatting="dd/MM/yyyy HH:mm:ss.fff"
                                GrowBy="0.02, 0.02"/>      
            </Setter.Value>
        </Setter>

        <Setter Property="YAxis">
            <Setter.Value>
                <s:NumericAxis  AxisAlignment="Right"
                                Visibility="Visible" 
                                TextFormatting="{Binding YAxisFormatting}" 
                                GrowBy="0.02, 0.02" 
                                AutoRange="Always"/>
            </Setter.Value>
        </Setter>

        <Setter Property="ChartModifier">
            <Setter.Value>
                <s:ModifierGroup>

                    <s:RubberBandXyZoomModifier IsAnimated = "False" IsXAxisOnly = "True" ExecuteOn = "MouseRightButton"/>
                    <s:ZoomPanModifier XyDirection="XYDirection" ClipModeX = "ClipAtExtents" ExecuteOn ="MouseLeftButton" />
                    <s:MouseWheelZoomModifier XyDirection = "XYDirection"/>
                    <s:ZoomExtentsModifier IsAnimated = "False" ExecuteOn = "MouseDoubleClick" />
                    <s:XAxisDragModifier  DragMode = "Scale"/>
                    <s:CursorModifier SourceMode="AllSeries"  UseInterpolation="True"/>
                    <s:LegendModifier ShowLegend="True" LegendPlacement ="Inside" GetLegendDataFor="AllSeries" Margin="10"/>

                     <!--<s:SeriesSelectionModifier ReceiveHandledEvents="True">
                            <s:SeriesSelectionModifier.SelectedSeriesStyle>
                                <Style TargetType="s:BaseRenderableSeries">
                                    <Setter Property="SeriesColor" Value="White"/>
                                    <Setter Property="PointMarkerTemplate">
                                        <Setter.Value>
                                            <ControlTemplate>
                                                <s:EllipsePointMarker Fill="#FF00DC" Stroke="White" Width="7" Height="7"/>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </s:SeriesSelectionModifier.SelectedSeriesStyle>
                        </s:SeriesSelectionModifier>-->


                </s:ModifierGroup>
            </Setter.Value>
        </Setter>

    </Style>

</Window.Resources>

<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="32" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <StackPanel Grid.Row="0" Orientation="Horizontal" Background="Black">
        <TextBlock Text="Dataseries Type:" Margin="5,0" VerticalAlignment="Center" FontSize="12" Foreground="White"/>
        <ComboBox x:Name="ComboBox_ChooseSeriesType" MinWidth="140" Margin="5,3" VerticalContentAlignment="Center"/>
        <TextBlock Text="Theme:" Margin="5,0" VerticalAlignment="Center" FontSize="12" Foreground="White"/>
        <ComboBox x:Name="ComboBox_ChooseTheme" MinWidth="140" Margin="5,3" VerticalContentAlignment="Center"/>
    </StackPanel>


        <dxdo:LayoutGroup Grid.Row="1" x:Name="LayoutGroup" Orientation="Vertical">

            <!--<dxdo:LayoutPanel Name="Panel1">
                <s:SciChartSurface Name="Surface1" Style="{StaticResource SciChartSurfaceStyle}"></s:SciChartSurface>
            </dxdo:LayoutPanel>-->




    </dxdo:LayoutGroup>



    </Grid>

And the code-behind method that retrieves the style and applies it: 以及检索style并应用style的代码隐藏方法:

private void TestSomeStuff()
    {
        var style = this.TryFindResource("SciChartSurfaceStyle") as Style;
        var sciChartSurface1 = new SciChartSurface() {Style = style};
        var panel1 = new LayoutPanel(){Content=sciChartSurface1};

        var style2 = this.TryFindResource("SciChartSurfaceStyle") as Style;
        var sciChartSurface2 = new SciChartSurface() {Style = style2};
        var panel2 = new LayoutPanel() {Content = sciChartSurface2};
        LayoutGroup.Add(panel1);
        LayoutGroup.Add(panel2);
    }

EDIT 编辑

Adding panel1 to LayoutGroup works just fine but the run-time error occurs as soon as I attempt to add panel2 . panel1添加到LayoutGroup可以很好地工作,但是一旦我尝试添加panel2 ,就会发生运行时错误。 Also, as long as do not inject style into a new instance of SciChartSurface it works just fine. 另外,只要不将style注入SciChartSurface的新实例中, SciChartSurface可以正常工作。 The error pops up as soon as I inject the style into both new surfaces. 一旦将样式注入到两个新曲面中,就会弹出错误消息。

Do not set Style in code behind directly: 不要在代码后面直接设置样式:

var style = this.TryFindResource("SciChartSurfaceStyle") as Style;
var sciChartSurface1 = new SciChartSurface() {Style = style};

but with SetValue method: 但是使用SetValue方法:

var style = this.TryFindResource("SciChartSurfaceStyle") as Style;
var sciChartSurface1 = new SciChartSurface();
sciChartSurface1.SetValue(StyleProperty, style);

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

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