我不知道Ellipse中的任何内置功能将其中心设置在一个点上,但您可以扩展Ellipse类来执行此操作.
将此类添加到项目中
public static class EllipseX
{
public static void SetCenter(this Ellipse ellipse, double X, double Y)
{
Canvas.SetTop(ellipse, Y - ellipse.Height/2);
Canvas.SetLeft(ellipse, X - ellipse.Width/2);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在xaml中创建椭圆
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas Background="LightGray">
<Ellipse
Name="myEllipse"
Fill="Red"
Height="75"
Width="75"
/>
</Canvas>
</Window>
Run Code Online (Sandbox Code Playgroud)
然后在代码后面的代码中写入int:
myEllipse.SetCenter(200,200);
Run Code Online (Sandbox Code Playgroud)
这样做的好处是您不必重复在您创建的每个椭圆中查找中心的逻辑.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
5819 次 |
| 最近记录: |