简体   繁体   中英

Add UIElements to Custom Control during Design Time

Im working on a WPF Custom Control that takes Elements and hides/shows them with a Toggle Button.

The Elements to be hidden should be defined by the User of the Control during Design Time.

The important part of my Control Looks like this:

MyCustomControl.cs

...
 public UIElement InnerContent
    {
        get { return (UIElement)GetValue(InnerContentProperty); }
        set { SetValue(InnerContentProperty, value); }
    }

    public static readonly DependencyProperty InnerContentProperty =
        DependencyProperty.Register("InnerContent", typeof(UIElement), typeof(MyCustomControl));
...

And the corresponding ContentPresenter like this:

<ContentPresenter Height="0"
                  x:Name="ToggleRegion"
                  Content="{Binding Path=InnerContent, RelativeSource={RelativeSource AncestorType=local:MyCustomControl}}">
</ContentPresenter>

My Problem now is it, that i need to make the Height of the ContentPresenter at 0 because i want the Content to be hidden at the Start. Is there a Way i can allow the user of my Control to add Elements by Drag and Drop during his Design Time? This Control should work completly in XAML. The only things defined in Code Behind are the Properties.

Thanks for your help.

Even with ContentPresenter not set to 0 height, you won't be able to drag-and-drop UIElements into the control.

See similar questions about drag-and-drop for WPF controls at design time.
WPF - How to enable dragging controls into ContentControl at Design-Time?
How to make custom WPF ContentControl that supports drag-and-drop at design time?

To my opinion there is no any official way to enable Drag-And-Drop during design-time for a Content/UserControl, it's just not supported by design. however you should consider inheriting from Panal, Grid, or Border.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM