2015-01-10 3 views
0

xaml에서 코드를 사용하고 동일한 스타일을 사용할 때 사용자 정의 컨트롤에 적용해야하는 스타일을 선언합니다 다음과 같은 오류가 두 번 throw됩니다.코드 숨김에서 xaml에 선언 된 사용자 정의`Style`을 사용하면 런타임 오류가 발생합니다.

요소에는 이미 논리 상위가 있습니다. 새로운 부모와 분리하기 전에 분리해야합니다.

내가 뭘 잘못하고 있니? 코드에서 동일한 사용자 컨트롤 유형의 여러 컨트롤을 만들고 하나의 동일한 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> 

그리고 코드 숨김 방법 style를 검색하고 그것을 적용 panel1LayoutGroup에 작품을 잘하지만 실행 - 추가

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); 
    } 

편집

을을 추가하자마자 오류가 발생합니다. 210. 또한, SciChartSurface의 새로운 인스턴스에 style을 삽입하지 않는 한 정상적으로 작동합니다. 두 표면에 스타일을 삽입하자마자 오류가 발생합니다.

+0

하는 당신이 예외를 가지고있는 내부 예외가 라인? –

+0

내부 예외 메시지가 표시되지 않고 코드 숨김의 마지막 줄을 실행 한 후에 오류가 발생하면 렌더링 측의 트리를 참조해야합니다. –

+0

'sciChartSurface1'과'sciChartSurface2' 또는'panel1'과'panel2'가'Name' 속성에 같은 값을 할당하지 않았는지 확인해보십시오. 때로는이 문제가 될 수 있습니다. – user2250152

답변

1

뒤에 직접 코드에 스타일을 설정하지 마십시오 :

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

하지만 SetValue 방법 :

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