将UIElement附加或绑定到CustomControl

时间:2014-04-24 12:21:51

标签: c# wpf xaml binding custom-controls

我有四个对象:      - 1画布(父母)      - 1个椭圆(画布之子)      - 1个UserControl(画布之子)      - 1 CustomControl(画布之子)

用户可以通过在画布内拖动来移动UserControl。 Ellipse表示拖动前UserControl的初始点。 最后,我的CustomControl应该在UserControl和Ellipse之间画一条线。

我尝试将我的Canvas,Ellipse和UserControl绑定/附加到我的CustomControl,但它不起作用。

欢迎任何解决方案。

解决方案必须简单,但我目前找不到解决方案。 感谢。

MainWindow.xaml

<Canvas x:Name="CanvasTrackObject" Canvas.Left="125" Canvas.Top="95" Background="Aqua">
        <Controls:LineConnector CanvasContainer="{Binding CanvasTrackObject}"
                                Source="{Binding SourcePoint}"
                                Target="{Binding TrackObject}"/>

        <Ellipse x:Name="SourcePoint"
                 Canvas.Left="-5" Canvas.Top="-5" 
                 Width="10" Height="10" Fill="DimGray"/>

        <TestShapes:MainSignal x:Name="TrackObject" Canvas.Left="35" Canvas.Top="64"/>

    </Canvas>

LineConnector.cs:

public class LineConnector : Control 
{ 
  #region Dependency properties

  #region CanvasContainer

  public static readonly DependencyProperty CanvasContainerProperty = DependencyProperty.Register("CanvasContainer", typeof(Canvas), typeof(LineConnector), new PropertyMetadata(default(Canvas)));

  public Canvas CanvasContainer
  {
    get
    {
      return (Canvas)GetValue(CanvasContainerProperty);
    }

    set
    {
      SetValue(CanvasContainerProperty, value);
    }
  }

  #endregion

  #region Source

  public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(UIElement), typeof(LineConnector), new PropertyMetadata(default(UIElement)));

  public UIElement Source
  {
    get
    {
      return (UIElement)GetValue(SourceProperty);
    }
    set
    {
      SetValue(SourceProperty, value);
    }
  }

  #endregion

  #region Target

  public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("Target", typeof(UIElement), typeof(LineConnector), new    PropertyMetadata(default(UIElement)));

  public UIElement Target
  {
    get
    {
      return (UIElement)GetValue(TargetProperty);
    }
    set
    {
      SetValue(TargetProperty, value);
    }
  }

  #endregion

  #endregion

  static LineConnector()
  {
    DefaultStyleKeyProperty.OverrideMetadata(typeof(LineConnector), new FrameworkPropertyMetadata(typeof(LineConnector)));
  }
}

0 个答案:

没有答案