加入线和椭圆

时间:2015-06-25 15:07:14

标签: join ellipse

我确实找到了如何通过XAML将一条线绑定到一个椭圆,但是我对绑定并不是很熟悉,虽然我已经研究过它,但我仍然不明白如何使用用户输入进行绑定

有没有办法,以编程方式将椭圆连接到一条直线的末端,这样当直线旋转一条直线时,椭圆随之旋转?我为此写的所有内容都是在代码中完成的,而不是XAML。

    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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public void geo()
        {
            freeCanvas.Children.Clear();
            double one, two, three, four, ang;
            if(double.TryParse(txtOne.Text, out one))
                if(double.TryParse(txtTwo.Text, out two))
                    if(double.TryParse(txtThree.Text, out three))
                        if (double.TryParse(txtFour.Text, out four))
                            if(double.TryParse(txtAngle.Text, out ang))
                        {
                            RotateTransform ans = new RotateTransform();
                            ans.Angle = ang;
                            ans.CenterX = 75;
                            ans.CenterY = 150;                             

                            Line lineA = new Line();
                            lineA.Stroke = Brushes.Black;
                            lineA.StrokeThickness = 2;
                            lineA.X1 = 75;
                            lineA.Y1 = 150;
                            lineA.X2 = 75;
                            lineA.Y2 = 250;
                            lineA.HorizontalAlignment = HorizontalAlignment.Center;
                            lineA.VerticalAlignment = VerticalAlignment.Top;
                            freeCanvas.Children.Add(lineA);

                            Line lineB = new Line();
                            lineB.Stroke = Brushes.Black;
                            lineB.StrokeThickness = 2;
                            lineB.X1 = 75;
                            lineB.Y1 = 50;
                            lineB.X2 = 75;
                            lineB.Y2 = 150;
                            //lineB.HorizontalAlignment = HorizontalAlignment.Center;
                            //lineB.VerticalAlignment = VerticalAlignment.Top;
                            lineB.RenderTransform = ans;
                            freeCanvas.Children.Add(lineB);

                            Line lineC = new Line();
                            lineC.Stroke = Brushes.Black;
                            lineC.StrokeThickness = 2;
                            lineC.X1 = -25;
                            lineC.Y1 = 150;
                            lineC.X2 = 175;
                            lineC.Y2 = 150;
                            lineC.HorizontalAlignment = HorizontalAlignment.Center;
                            lineC.VerticalAlignment = VerticalAlignment.Top;
                            freeCanvas.Children.Add(lineC);

                            PathFigure pthFigure = new PathFigure();
                            pthFigure.StartPoint = new Point(-25, 150);

                            ArcSegment arcSeg = new ArcSegment();
                            arcSeg.Point = new Point(175, 150);
                            arcSeg.Size = new Size(100, 100);
                            arcSeg.IsLargeArc = true;
                            arcSeg.SweepDirection = SweepDirection.Clockwise;
                            arcSeg.RotationAngle = 90;

                            PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
                            myPathSegmentCollection.Add(arcSeg);

                            pthFigure.Segments = myPathSegmentCollection;

                            PathFigureCollection pthFigureCollection = new PathFigureCollection();
                            pthFigureCollection.Add(pthFigure);

                            PathGeometry pthGeometry = new PathGeometry();
                            pthGeometry.Figures = pthFigureCollection;

                            Path arcPath = new Path();
                            arcPath.Stroke = new SolidColorBrush(Colors.Black);
                            arcPath.StrokeThickness = 1.5;
                            arcPath.Data = pthGeometry;
                            //arcPath.Fill = new SolidColorBrush(Colors.Yellow);
                            freeCanvas.Children.Add(arcPath);

                            Ellipse ellA = new Ellipse();
                            ellA.Width = 10;
                            ellA.Height = 10;
                            ellA.Stroke = Brushes.Black;
                            ellA.Fill = Brushes.Lime;
                            TranslateTransform ella1 = new TranslateTransform(70, 145);
                            ellA.RenderTransform = ella1;
                            freeCanvas.Children.Add(ellA);

                            Ellipse ellB = new Ellipse();
                            ellB.Width = 10;
                            ellB.Height = 10;
                            ellB.Stroke = Brushes.Black;
                            ellB.Fill = Brushes.Yellow;
                            TranslateTransform ellb1 = new TranslateTransform(70, 45);
                            ellB.RenderTransform = ellb1;
                            freeCanvas.Children.Add(ellB);



                        }

        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            geo();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

没关系,我明白了。我发布了提供答案的代码段。

                            Rads = Math.PI * ang / 180;

                            x1 = Math.Sin(Rads) * 80;
                            y1 = Math.Cos(Rads) * 80;
                            x2 = Math.Sin(Rads) * 30;
                            y2 = Math.Cos(Rads) * 30;

                            Ellipse ellA = new Ellipse();
                            ellA.Width = 10;
                            ellA.Height = 10;
                            ellA.Stroke = Brushes.Black;
                            ellA.Fill = Brushes.Lime;
                            TranslateTransform ella1 = new TranslateTransform(70 + x1, 145 - y1);
                            ellA.RenderTransform = ella1;
                            freeCanvas.Children.Add(ellA);

                            Ellipse ellB = new Ellipse();
                            ellB.Width = 10;
                            ellB.Height = 10;
                            ellB.Stroke = Brushes.Black;
                            ellB.Fill = Brushes.Yellow;
                            TranslateTransform ellb1 = new TranslateTransform(70 + x2, 145 - y2);
                            ellB.RenderTransform = ellb1;
                            freeCanvas.Children.Add(ellB);