3

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, which is not what I need.

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Nevermind
  • 1,521
  • 1
  • 15
  • 16
  • 1
    Here's a good link, looks like you're going to need s set of calsses with design time functionality http://www.codeproject.com/KB/WPF/XPlorerBar2.aspx –  Mar 04 '11 at 12:59
  • Sorry, dude, I didn't mean to vote down, how do I revert the it? –  Mar 04 '11 at 13:51
  • @Dmitry I haven't found an answer by your link, but it is still very helpful for other WPF stuff I have to do, thanks. – Nevermind Mar 05 '11 at 09:20
  • @Dmitry: I made an "edit", see if you can revert your downvote now. – BoltClock Mar 05 '11 at 18:35

3 Answers3

3

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Markus Hütter
  • 7,796
  • 1
  • 36
  • 63
1

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).

SDP190
  • 336
  • 3
  • 10
1

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);
}
Community
  • 1
  • 1
Stephen
  • 509
  • 8
  • 13