我想做的是在特定位置以编程方式创建一个矩形,我想在添加到 Canvas 之前将其分配给 Thumb 控件。原因是我想稍后实现拖放。但是下面这段代码的问题是我从来没有在画布内看到任何东西,请帮忙。当我尝试调试时,我在 fact 变量中看不到任何数据,并且 t.HasContent 为假。
public void DrawShape2()
{
Thumb th = new Thumb();
Path myPath1 = new Path();
myPath1.Stroke = Brushes.Black;
myPath1.StrokeThickness = 1;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255);
myPath1.Fill = mySolidColorBrush;
Point a = new Point(25, 150);
Point b = new Point(500, 490);
Rect myRect1 = new Rect(a, b);
RectangleGeometry myRectangleGeometry1 = new RectangleGeometry();
myRectangleGeometry1.Rect = myRect1;
GeometryGroup myGeometryGroup1 = new GeometryGroup();
myGeometryGroup1.Children.Add(myRectangleGeometry1);
myPath1.Data = myGeometryGroup1;
ControlTemplate t = new ControlTemplate();
var fact = new FrameworkElementFactory(typeof(Path));
fact.SetValue(Path.DataProperty, myPath1.Data);
t.VisualTree = fact;
th.Template = t;
cnv.Children.Add(th);
}