WPF 复合形状
我使用基本形状创建了一个有点复杂的形状。我想在画布上使用多个这个,在代码隐藏中以编程方式创建。
我考虑过创建一个 UserControl,但是封装这个复合形状的最佳方法是什么?
I have created a somewhat complex shape using basic shapes. I would like to use multiples of this on a canvas, created programmatically in code behind.
I have thought about creating a UserControl, but what's the best way to encapsulate this composite shape?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于大多数目的,将它们放入 ControlTemplate 或 DataTemplate 效果最佳。以下是 ControlTemplate 方式:
和 DataTemplate 方式:
要在这些方式之间进行选择,请决定您想要哪些附加功能(如果有)。您可能希望向控件或数据对象添加属性。
您还可以使用 ItemsControl 及其子类(ListBox、ComboBox 等)来适当地呈现您的形状,而不是列出单个控件。
替代方法
另一种完全不同的方法是将形状集合转换为 Drawing 对象,并使用 DrawingImage 或 DrawingBrush 来呈现它。
For most purposes putting them in a ControlTemplate or DataTemplate works the best. Here's the ControlTemplate way:
And the DataTemplate way:
To choose between these, decide what additional functionality (if any) you want. You will probably want to add properties to your control or your data object.
Instead of listing individual controls you can also use ItemsControl and its subclasses (ListBox, ComboBox, etc) to present your shapes appropriately.
Alternate approach
Another completely different approach is to convert your collection of shapes to a Drawing object and present it using a DrawingImage or a DrawingBrush.