我需要通过代码将几何对象添加到画布中。意思是当您单击按钮时添加了形状。我将canvas作为参数发送给函数,然后使用canvas.children.add(),但是这种方式会影响整个mvvm的想法,不是吗?有没有更好的方法来做到这一点?使用wpf将图形添加到画布的最佳方法是什么?
3
A
回答
-1
不,没有更好的方法来做到这一点。
如果您在XAML中定义形状,它们将Add()'编辑到Canvas.Children中的方式相同。
现在,如果你想以干净的Mvvm方式来做到这一点,那么你可能不得不用你的视图模型来获得创造性。我会为该按钮添加一个VM ICommand(因此您可以连接它),在处理程序中,我将某种对象添加到ObservableCollection,然后在视图中执行某些操作以从该ViewModel集合创建形状(在xaml或代码隐藏中)
5
您可以使用ItemsControl
和Canvas
作为它的项目面板。然后在虚拟机中,您需要一个集合来保存所有项目。每个项目应具有所有放置属性。
所以,在代码中,它看起来像这样(我省略为简洁变更通知):
项:
public class CanvasShape : INotifyPropertyChanged
{
public double Top {get; set;}//TODO: add change notification
public double Left {get; set;}//TODO: add change notification
public Geometry PathData {get; set;}//TODO: add change notification
}
在VM:
public ObservableCollection<CanvasShape> Shapes {get; set;}
.....
//Add some logic to fill the collection
在XAML中:
<!--The DataContext here is the VM-->
<ItemsControl ItemsSource="{Binding Shapes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<!--These setters will control the position of the shape; the DataContext here is CanvasShape-->
<Setter Property="Cavas.Top" Value="{Binding Top}"/>
<Setter Property="Cavas.Left" Value="{Binding Left}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Path Data="{Binding PathData}"
.......
/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
相关问题
- 1. WPF:将图形添加到画布中
- 2. 将Last属性添加到System.Array的最佳方法是什么?
- 3. 用低cpu使用动画画布的最佳方法是什么?
- 4. 用javascript添加css到页面的最佳方法是什么?
- 5. 用画布绘制图形并设置角度的最佳方式是什么?
- 6. 什么是将内容添加到模板的最佳方式?
- 7. 将数组添加到Mysql的最佳方式是什么
- 8. 将单元格添加到gridView的最佳方式是什么?
- 9. 用UIImageView创建条形图的最佳方法是什么?
- 10. 什么是在画布上动画的最佳方式?
- 11. 使用WPF中的设置将子图像添加到画布#
- 12. 什么将是层图像的画廊的最佳方式?
- 13. 在WPF中渲染图表的最佳方法是什么?
- 14. 将.mmmodel文件添加到我的xcode9项目的最佳方法是什么?
- 15. HTML5-移动画布上已有图像的最佳方法是什么
- 16. 使用PHP发布到Web API的最佳方式是什么?
- 17. 什么是将多个图像添加到progrram(qt)的最佳方式
- 18. 将TextView添加到游戏主视图的最佳方式是什么?
- 19. 在Jung中克隆图形的最佳方法是什么?
- 20. 在LaTeX中绘制条形图的最佳方法是什么?
- 21. 将项目添加到RecycleAdapter前面的最佳方法是什么?
- 22. 将SSIS(商业智能)项目添加到SourceSafe的最佳方法是什么?
- 23. 将此javascript添加到Asp.Net网站的最佳方法是什么?
- 24. 将“forCount”控件结构添加到Objective-C的最佳方法是什么?
- 25. 将空格添加到文本文件的最佳方法是什么?
- 26. 将第三方脚本添加到dom中的最佳做法是什么?
- 27. 将键/值添加到现有URL字符串的最佳方法是什么?
- 28. 强制WPF DataGrid添加特定新项目的最佳方法是什么?
- 29. 使用JQuery动态添加HTML的最佳方式是什么?
- 30. WPF:将项目添加到ListView的最有效/最快的方式是什么?