2012-09-20 2 views
0

저는 WPF로 조금 놀았습니다. 내가하고 싶은 일은 템플릿 팔레트 윈도우의 커스텀 컨트롤을 다른 윈도우의 캔버스로 끌어다 놓고 그 커스텀 컨트롤의 클론을 생성하는 것입니다. 이것은 Visio에서 볼 수있는 것과 매우 흡사합니다.XamlReader를 통해 파싱 된 UserControl에서 Canvas.Left를 설정하십시오.

팔레트에서 템플릿 컨트롤을 복제하기 위해 XamlWriter 및 XamlReader의 직렬화 및 비 직렬화 기능을 남용합니다. 컨트롤은 다음과 같습니다

private void drawingCanvas_Drop(object sender, DragEventArgs e) 
{ 
    DrawingShape shape = (DrawingShape)e.Data.GetData(typeof(DrawingShape)); 
    if (shape != null) 
    { 
     Canvas target = this.drawingCanvas; 
     Point p = e.GetPosition(target); 

     string xamlString = System.Windows.Markup.XamlWriter.Save(shape); 
     DrawingShape c = (DrawingShape)System.Windows.Markup.XamlReader.Parse(xamlString); 

     Canvas.SetLeft(c, p.X - c.Width/2); 
     Canvas.SetTop(c, p.Y - c.Height/2); 
     target.Children.Add(c); 

     this.shapesPalette.shapesGallery.ClearValue(ListView.SelectedIndexProperty); 
    } 
} 

내 사용자 :

<UserControl x:Class="MyNamespace.Graphics.DrawingShape" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="60" d:DesignWidth="60"> 
    <Grid Width="Auto" Height="Auto" > 
     <Grid.RowDefinitions> 
      <RowDefinition Height="38"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Canvas HorizontalAlignment="Center" Grid.Row="0" Width="38" Height="38" Canvas.Left="0" Canvas.Top="0"> 
      <Path x:Name="Path" Width="35.8774" Height="31.2047" Canvas.Left="1.0613" Canvas.Top="3.29528" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF230FD2" Fill="#FF0096FF" Data="F1 M 19,3.79528L 1.5613,34L 36.4387,34L 19,3.79528 Z "/> 
     </Canvas> 
     <Label x:Name="Label" HorizontalAlignment="Center" Grid.Row="1">MyLabel</Label> 
    </Grid> 
</UserControl> 

그러나, Canvas.SetLeft()Canvas.SetTop() 아무것도하지 않는 내 드롭 콜백에서

나는 다음과 같은 코드가 있습니다. 복제본은 항상 대상 캔버스의 왼쪽 위 모서리에 추가됩니다. 내 사용자 컨트롤 대신 다른 WPF 컨트롤을 사용하면이 코드가 제대로 작동합니다.

복제 된 사용자 정의 컨트롤이 시각적 계층 구조의 일부가 아닌데도 문제가있는 것 같습니다. 그러나, 위의 코드가 작동하도록하기 위해 MS 컨트롤이 더 잘하는지 궁금합니다. WPF를 처음 접했을 때 내 사용자 정의 컨트롤을 수정하기 위해 무엇을 찾고 있어야하는지 정확히 알지 못합니다. 모든 포인터 크게 감사하겠습니다. 내가 (단지 어쨌든 프로토 타입에 사용되었다), 지금은 레이아웃 업데이트 적용 후 ActualWidth/ActualHeight를 사용 XamlWriter/XamlReader를 통해 멀리 복제에서 이동 한

답변

0

그러나

private void drawingCanvas_Drop(object sender, DragEventArgs e) 
{ 
    DrawingShape templateShape = (DrawingShape)e.Data.GetData(typeof(DrawingShape)); 
    if (templateShape != null) 
    { 
     Canvas target = this.drawingCanvas; 
     Point p = e.GetPosition(target); 

     DrawingShape addedShape = new DrawingShape(); 
     addedShape.IsDraggingEnabled = true; 

     target.Children.Add(addedShape); 
     addedShape.UpdateLayout(); 
     addedShape.RenderTransform = new TranslateTransform(p.X - addedShape.ActualWidth/2, p.Y - addedShape.ActualHeight/2); 
     this.shapesPalette.shapesGallery.ClearValue(ListView.SelectedIndexProperty); 
    } 
} 

을, 나는 이것을 기대 솔루션을 사용하여 XamlWriter/XamlReader 복제를 수행 할 수 있습니다.