好的我已經破解了一些不使用附加屬性方法的代碼。這可能不是「好」的代碼,因爲我在20分鐘內寫完了它,但它會讓你開始。
MainWindow.xaml
<Window x:Class="EllipseTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:EllipseTest"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" >
<Window.Resources>
<ControlTemplate x:Key="template1">
<Ellipse Width="60" Height="30" Fill="Blue" />
</ControlTemplate>
</Window.Resources>
<Canvas Name="canvas">
<my:ExtendedThumb x:Name="thumb1" Canvas.Left ="0" Canvas.Top="0" DragDelta="myThumb_DragDelta" Template="{StaticResource template1}" />
<my:ExtendedThumb x:Name="thumb2" Canvas.Left ="50" Canvas.Top="20" DragDelta="myThumb_DragDelta" Template="{StaticResource template1}" />
</Canvas>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace EllipseTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Path path;
public MainWindow()
{
InitializeComponent();
}
private void myThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
ExtendedThumb thumb = e.Source as ExtendedThumb;
Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
if (thumb == thumb1)
UpdateLine(thumb, thumb2);
else
UpdateLine(thumb1, thumb);
}
private void UpdateLine(ExtendedThumb firstThumb, ExtendedThumb secondThumb)
{
double left1 = Canvas.GetLeft(firstThumb);
double top1 = Canvas.GetTop(firstThumb);
double left2 = Canvas.GetLeft(secondThumb);
double top2 = Canvas.GetTop(secondThumb);
thumb1.ConnectingLine.StartPoint = new Point(left1 +firstThumb.ActualWidth/2, top1 + firstThumb.ActualHeight/2);
thumb1.ConnectingLine.EndPoint = new Point(left2 + secondThumb.ActualWidth/2, top2 + secondThumb.ActualHeight/2);
thumb2.ConnectingLine.StartPoint = new Point(left2 + secondThumb.ActualWidth/2, top2 + secondThumb.ActualHeight/2);
thumb2.ConnectingLine.EndPoint = new Point(left1 + firstThumb.ActualWidth/2, top1 + firstThumb.ActualHeight/2);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
path = new Path();
path.Stroke = Brushes.Black;
path.StrokeThickness = 2;
canvas.Children.Add(path);
LineGeometry line = new LineGeometry();
path.Data = line;
thumb1.ConnectingLine = line;
thumb2.ConnectingLine = line;
UpdateLine(thumb1, thumb2);
}
}
}
ExtendedThumb.cs
using System;
using System.Collections.Generic;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Linq;
using System.Text;
namespace EllipseTest
{
public class ExtendedThumb : Thumb
{
public LineGeometry ConnectingLine { get; set; }
public ExtendedThumb() : base() { ConnectingLine = new LineGeometry(); }
}
}
而且,我的想法從這個鏈接的內容:http://denisvuyka.wordpress.com/2007/10/13/wpf-draggable-objects-and-simple-shape-connectors/
嗯...看着附加的屬性,但找不到合理的。 – bl4kh4k
所以你會創建一個附加屬性,稱之爲'PointInEllipse'。現在在你的代碼背後你設置綁定,做一些數學,找到一個點在你的橢圓內(也許是中心),然後綁定線的'X1Property'到那個點。對第二個橢圓做同樣的事情,並將'X2Property'綁定到該橢圓。這裏是一篇關於綁定到附加屬性的文章。 http://geekswithblogs.net/NewThingsILearned/archive/2008/01/15/binding-to-an-attached-property.aspx這似乎有道理嗎? –
試圖弄清楚如何做到這一點......我還沒有在後面的代碼中綁定。你給的例子並不暗示如何去做這件事。但它聽起來像是它正確的路要走。 – bl4kh4k