1
A
回答
5
假設你有與已知的圓心和半徑的橢圓:
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]);
1
有這樣做的許多不同的方式。這裏有一個,可能會滿足您的需求。這只是一個用戶控件。圓的半徑取決於用戶控件的大小,並且會強制控件大小一致。定位用戶控件將由您決定。內線的角度是可綁定的。
用戶控制的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>
相關問題
- 1. WPF橢圓路徑
- 2. 刪除橢圓形邊框半徑時,2位數字顯示
- 3. WPF:創建半橢圓按鈕
- 4. 計算橢圓形的半徑
- 5. React Native中的橢圓邊框半徑
- 6. WPF無法在DataGridTemplateColumn中顯示橢圓
- 7. 確定橢圓的X半徑和Y半徑
- 8. 橢圓路徑
- 9. 可以圓被轉換成橢圓使用半徑在畫布
- 10. iPhone如何半夾橢圓
- 11. 讓半圈多橢圓
- 12. paint()不顯示橢圓
- 13. 將地圖顯示爲橢圓
- 14. WPF TextBlock的重疊橢圓
- 15. C#WPF橢圓滑塊
- 16. 橢圓圖WPF動畫
- 17. WPF使用橢圓進步
- 18. WPF綁定改變橢圓
- 19. wpf中的橢圓按鈕
- 20. WPF橢圓黑色背景
- 21. K最近的鄰居在sklearn半徑 - 橢圓
- 22. 特定圓半徑
- 23. 半徑公差圓
- 24. 結合兩個半透明橢圓
- 25. 如何繪製Python中的半橢圓?
- 26. 新行,當我顯示10橢圓
- 27. OpenCV - 根本不顯示橢圓
- 28. CSS3餅圖邊框半徑顯示薄邊框的圓
- 29. iOS版SDK:刀尖圓弧半徑顯示不正確
- 30. 劍道網格圖標欄顯示橢圓中的橢圓圖版本:58.0.3029.96
你有沒有試過_anything_這麼遠嗎?先顯示你的努力..閱讀[常見問題]和[問] – 2013-04-23 15:12:41
你想使用XAML或代碼? – David 2013-04-23 15:27:04
是的,我試過了,對不起,我忘了把代碼放在這裏。 David - 在代碼中 – Sangeetha 2013-04-24 10:17:13