【问题标题】:Show Radius of Ellipse - WPF [closed]显示椭圆的半径 - WPF [关闭]
【发布时间】:2013-04-23 15:11:51
【问题描述】:

我有一个椭圆几何。我想用 C# 从圆心到圆的边缘画一条线来显示椭圆的半径。我怎样才能做到这一点?

注意:椭圆的中心和半径不是固定的,由用户定义。

【问题讨论】:

  • 到目前为止,您尝试过任何东西吗?首先展示你的努力。阅读FAQHow to Ask
  • 您要使用 xaml 还是在代码中?
  • 是的,我试过了,抱歉我忘了把代码放在这里。大卫 - 在代码中

标签: c# wpf ellipse pathgeometry


【解决方案1】:

假设你有一个已知中心和半径的椭圆:

        Path path = new Path();
        EllipseGeometry eg = new EllipseGeometry();
        eg.Center = new Point(left + side / 2, top + side / 2);
        eg.RadiusX = side / 2;
        eg.RadiusY = side / 2;
        path.Data = eg;
        paths.Add(path);
        canvas1.Children.Add(paths[paths.Count - 1]);
        .
        .
        path = new Path();
        borderColor.Color = Colors.Red;
        path.Stroke = borderColor;
        path.StrokeThickness = 2;
        LineGeometry r = new LineGeometry();
        r.StartPoint = eg.Center;
        r.EndPoint = new Point(eg.Center.X + eg.RadiusX, eg.Center.Y);
        path.Data = r;
        paths.Add(path);
        canvas1.Children.Add(paths[paths.Count - 1]);

【讨论】:

    【解决方案2】:

    有很多不同的方法可以做到这一点。这是一个,可能会也可能不会满足您的需求。它只是一个用户控件。圆的半径取决于用户控件的大小,它会强制控件大小一致。定位用户控件将由您决定。内线的角度是可绑定的。

    用户控件 xaml

    <UserControl x:Class="TestWPF.CircleTest"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 Foreground="Blue" Background="White"
                 x:Name="CT" SnapsToDevicePixels="True">
        <Grid>
            <Ellipse Stroke="{Binding Foreground, ElementName=CT}" Fill="{Binding Background, ElementName=CT}" />
            <Line X1="{Binding Center.X, ElementName=CT}" X2="{Binding EndPoint.X, ElementName=CT}" Y1="{Binding Center.Y, ElementName=CT}" Y2="{Binding EndPoint.Y, ElementName=CT}" 
                  Stroke="{Binding Foreground, ElementName=CT}">
                <Line.RenderTransform>
                    <RotateTransform Angle="{Binding Angle, ElementName=CT}" CenterX="{Binding Center.X, ElementName=CT}" CenterY="{Binding Center.Y, ElementName=CT}" /> 
                </Line.RenderTransform>  
            </Line>
            <TextBlock Text="{Binding Angle, ElementName=CT, StringFormat='N2'}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="3" />
        </Grid>
    </UserControl>
    

    后面的用户控制代码

    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace TestWPF
    {
        public partial class CircleTest : UserControl, INotifyPropertyChanged
        {
            public CircleTest()
            {
                InitializeComponent();
    
                this.SizeChanged += CircleTest_SizeChanged;
            }
    
            void CircleTest_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
            {
                double radius;
                if (ActualHeight < ActualWidth)
                {
                    Width = ActualHeight;
                    _center = new Point(Width / 2, ActualHeight / 2);
                    radius = ActualHeight / 2;
                }
                else
                {
                    Height = ActualWidth;
                    _center = new Point(ActualWidth / 2, Height / 2);
                    radius = ActualWidth / 2;
                }
    
                _endPoint = new Point(Center.X, Center.Y - radius);
    
                NotifyOfPropertyChange("Center");
                NotifyOfPropertyChange("EndPoint");
            }
    
            public double Angle
            {
                get { return (double)GetValue(AngleProperty); }
                set { SetValue(AngleProperty, value); }
            }
            public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(CircleTest), new PropertyMetadata(45.0));
    
            private Point _center;
            public Point Center
            {
                get { return _center; }
            }
    
            private Point _endPoint;
            public Point EndPoint
            {
                get { return _endPoint; }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyOfPropertyChange(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    你会这样使用它:

    <Window x:Class="TestWPF.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:test="clr-namespace:TestWPF"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <test:CircleTest Width="200" Height="200" Foreground="Purple" Angle="{Binding Value, ElementName=SL}" />
            <Slider x:Name="SL" Minimum="0" Maximum="360" VerticalAlignment="Bottom" Margin="20" />
        </Grid>
    </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多