开发者

How to make custom WPF ContentControl that supports drag-and-drop at design time?

开发者 https://www.devze.com 2023-02-14 15:41 出处:网络
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.

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.

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.

I can place it inside my control by manually editing XAML, but I want the designer to work too.

Interestingly, when I subclass Canvas, Grid or Panel, designer works as expected. However, these controls have many children,开发者_StackOverflow社区 which is not what I need.

How can I make a single-child control that works in designer?


how about inheriting from Border? that way you could spare yourself the hassle with Designer Extensibility


I had the same problem with a content control I am writing and found an easy solution on this StackOverflow thread.

Just implement the HitTestCore method:

protected override System.Windows.Media.HitTestResult HitTestCore(System.Windows.Media.PointHitTestParameters hitTestParameters)
{
    return new PointHitTestResult(this, hitTestParameters.HitPoint);
}


I also had a similar question here.

But after digging and digging it seams that the real answer is "NO", there isn't any official way to support dragging controls straight into a custom Content-Control at Design-Time, even implementing 'HitTestCore' as Stephan's answer suggests, does not enable drag&drop at design-time for ContentControl.

For such purposes you should consider inheriting from either Grid or Panel (to allow multiple child controls), or Border (to allow single child).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号