我正在学习WPF中的绘图形状。我想以编程方式绘制Path
,而不是通过XAML。我不知道什么可以分配给Data属性。
Path p = new Path();
p.Data = ???
答案 0 :(得分:7)
//Add the Path Element
myPath = new Path();
myPath.Stroke = System.Windows.Media.Brushes.Black;
myPath.Fill = System.Windows.Media.Brushes.MediumSlateBlue;
myPath.StrokeThickness = 4;
myPath.HorizontalAlignment = HorizontalAlignment.Left;
myPath.VerticalAlignment = VerticalAlignment.Center;
EllipseGeometry myEllipseGeometry = new EllipseGeometry();
myEllipseGeometry.Center = new System.Windows.Point(50,50);
myEllipseGeometry.RadiusX = 25;
myEllipseGeometry.RadiusY = 25;
myPath.Data = myEllipseGeometry;
myGrid.Children.Add(myPath);
您正在寻找的是myPath.Data = myEllipseGeometry;
行。只需为它指定一个Geometry对象。
答案 1 :(得分:0)
以下是我正在尝试的一个例子:
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Data;
namespace GridNineExperiment
{
public class Hamburger : Button
{
static GeometryCollection DataHamburger = new GeometryCollection
{
new RectangleGeometry {Rect = new Rect{X = 0, Y = 0, Width = 20, Height = 5 }},
new RectangleGeometry {Rect = new Rect{X = 0, Y = 10, Width = 20, Height = 5 }},
new RectangleGeometry {Rect = new Rect{X = 0, Y = 20, Width = 20, Height = 5 }},
};
static Path PathHamburger = new Path
{
Fill = new SolidColorBrush(Colors.White),
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 1.0,
Data = new GeometryGroup { Children = DataHamburger }
};
public Hamburger()
{
Content = PathHamburger;
}
}