如何在设计时制作支持拖放的自定义WPF ContentControl? [英] How to make custom WPF ContentControl that supports drag-and-drop at design time?
问题描述
我想创建一个自定义的 WPF 控件,里面有一个子"控件.子类化 ContentControl
或 UserControl
有效,但有一个缺陷:这些控件在设计器模式下不起作用.
I want to create custom WPF control that has a single "child" control inside. Subclassing ContentControl
or UserControl
works, but has one flaw: these controls don't work in designer mode.
不工作"是指这种情况:假设我有一个带有自定义控件的 Canvas
.我想在我的控件中放置一个 Button
.我将它从工具箱中拖出,它出现在我的控件中.但是,XAML 视图显示新按钮实际上属于 Canvas
,而不是我的控件.
By "don't work" I mean this scenario: suppose I have a Canvas
with my custom control in it. I want to put, say, a Button
inside my control. I drag it from the toolbox, and it appears inside my control. However, XAML view shows that the new button actually belongs to Canvas
, not to my control.
我可以通过手动编辑 XAML 将其放置在我的控件中,但我希望设计器也能工作.
I can place it inside my control by manually editing XAML, but I want the designer to work too.
有趣的是,当我子类化 Canvas
、Grid
或 Panel
时,设计器按预期工作.但是,这些控件有很多 个子控件,这不是我需要的.
Interestingly, when I subclass Canvas
, Grid
or Panel
, designer works as expected. However, these controls have many children, which is not what I need.
如何制作在设计器中有效的单子控件?
How can I make a single-child control that works in designer?
推荐答案
继承 Border
怎么样?这样你就可以免去设计器可扩展性的麻烦
how about inheriting from Border
? that way you could spare yourself the hassle with Designer Extensibility
这篇关于如何在设计时制作支持拖放的自定义WPF ContentControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!