Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从c sharp文件在WPF画布上显示矩形_C#_Wpf_Wpf Controls - Fatal编程技术网

C# 如何从c sharp文件在WPF画布上显示矩形

C# 如何从c sharp文件在WPF画布上显示矩形,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我知道如何在WPF xaml中创建一个矩形,然后在c sharp文件中编辑它。我感兴趣的是在c sharp文件中创建矩形,将其显示在WPF画布上,然后能够对其进行操作。我和我的朋友正在开发一个游戏,我们希望能够从源代码中动态生成新对象。任何帮助都很好:)很简单。前提是您有一个类似于这个XAML的画布 <Window ...> <Canvas Name="canvas" /> </Window> 您不应该这样做,而是使用数据绑定和数据模板;阅读这些东西

我知道如何在WPF xaml中创建一个矩形,然后在c sharp文件中编辑它。我感兴趣的是在c sharp文件中创建矩形,将其显示在WPF画布上,然后能够对其进行操作。我和我的朋友正在开发一个游戏,我们希望能够从源代码中动态生成新对象。任何帮助都很好:)

很简单。前提是您有一个类似于这个XAML的画布

<Window ...>
    <Canvas Name="canvas" />
</Window>

您不应该这样做,而是使用数据绑定和数据模板;阅读这些东西。非常感谢!我感谢你的帮助。
Rectangle rect = new Rectangle
{
    Fill = Brushes.Red,
    Width = 100,
    Height = 100
};

Canvas.SetLeft(rect, 100);
Canvas.SetTop(rect, 100);
canvas.Children.Add(rect);