2015-10-25 47 views
0

我写了一些椭圆到txtBox,然后点击在画布上添加它们。按行连接两个椭圆WPF

我想写一些应该按行连接的省略号。但是如何添加这条线的坐标?我认为它应该是椭圆的中心,但是如何知道它的坐标?

XAML:

<Window x:Class="draw.ellipse" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="draw" Height="768" Width="1024" WindowStartupLocation="CenterScreen" Name="Create" Closed="Create_Closed"> 
<Canvas Name="can" Background="White" MouseDown="can_MouseDown"> 
    <TextBox Name="txbNumber" Height="34" Canvas.Left="797" TextWrapping="Wrap" Text="5" Canvas.Top="76" Width="209" FontSize="18"/> 
    <Button Name="btnCreate" Content="create" Canvas.Left="797" Canvas.Top="138" Width="209" Height="66" FontSize="18" Click="btnCreate_Click"/> 
    <TextBox Name="txb1" Height="34" Canvas.Left="797" TextWrapping="Wrap" Text="3" Canvas.Top="222" Width="78" FontSize="18"/> 
    <Button Name="btnCreateEdge" Content="create edge" Canvas.Left="797" Canvas.Top="276" Width="209" Height="66" FontSize="18" Click="btnCreateEdge_Click"/> 
    <TextBox Name="txb2" Height="34" Canvas.Left="928" TextWrapping="Wrap" Text="4" Canvas.Top="222" Width="78" FontSize="18"/> 
    <Label Name='lbl' Content="Label" Canvas.Left="797" Canvas.Top="374" Height="54" Width="169" FontSize="20"/> 
</Canvas> 

C#:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
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.Shapes; 

namespace Graduate 
{ 
/// <summary> 
/// Логика взаимодействия для Create.xaml 
/// </summary> 
public partial class draw: Window 
{ 
    public draw() 
    { 
     InitializeComponent(); 
    } 
    int n, i, k, k2, j, j2, f; 
    Ellipse[] v; 
    private void btnCreate_Click(object sender, RoutedEventArgs e) 
    { 
     n = Convert.ToInt16(txbNumber.Text); 
     f = n; 
     v = new Ellipse[n]; 
     var elipsy = can.Children.OfType<Ellipse>().ToList(); 
     foreach (var ellipses in elipsy) 
     { 
      can.Children.Remove(ellipses); 
     } 
    } 


    private void Create_Closed(object sender, EventArgs e) 
    { 
     Application.Current.Shutdown(); 
    } 
    private void can_MouseDown(object sender, MouseButtonEventArgs e) 
    { 

     Ellipse myEllipse = new Ellipse(); 
     SolidColorBrush mySolidColorBrush = new SolidColorBrush(); 
     mySolidColorBrush.Color = Colors.Transparent; 

     for (i = 0; i < n; i++) 
     { 

      v[i] = new Ellipse(); 

      v[i].Fill = mySolidColorBrush; 
      v[i].StrokeThickness = 2; 
      v[i].Stroke = Brushes.Black; 

      v[i].Width = 75; 
      v[i].Height = 75; 

      v[i].Margin = new Thickness(e.GetPosition(can).X, e.GetPosition(can).Y, 0, 0); 



      can.Children.Add(v[i]); 
     } 
     if (n <= 0) 
      return; 
     n--; 
    } 

    private void btnCreateEdge_Click(object sender, RoutedEventArgs e) 
    { 
     k2 = Convert.ToInt16(txb1.Text); 
     j2 = Convert.ToInt16(txb2.Text); 
     k = f - k2; 
     j = f - j2; 

     Line line = new Line(); 
     //line.X1 = v[k].; 
     can.Children.Add(line); 

    } 
} 

}

+0

椭圆的中心很简单,它的X和Y坐标分别加上其宽度和高度的一半。你在这里有什么特别的麻烦? FWIW,最好使用Canvas.Left和Canvas.Top附加属性来定位你的椭圆,而不是像在这里一样设置边距。 –

回答

0

你的椭圆定位在彼此的顶部被调用的处理时,所以即使你找到一种方法在他们身上放置一条线,你可能不会看到它。

要回答你的问题:

  1. 查找通过Canvas.Top和Canvas.Left椭圆的位置。通过这些信息及其宽度或高度,您可以得到椭圆的中心。

  2. 您可以使用#1的信息作为行的X1,X2和Y1,Y2来连接省略号。