简体   繁体   English

在设计时将UIElements添加到自定义控件

[英]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. 我正在使用WPF自定义控件,它使用元素并隐藏/使用切换按钮显示它们。

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 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如下:

<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. 我现在的问题是,我需要将ContentPresenter的高度设置为0,因为我希望在开始时隐藏内容。 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. 此控件应在XAML中完全运行。 The only things defined in Code Behind are the Properties. Code Behind中定义的唯一内容是属性。

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. 即使ContentPresenter未设置为0高度,您也无法将UIElements拖放到控件中。

See similar questions about drag-and-drop for WPF controls at design time. 在设计时查看有关WPF控件拖放的类似问题。
WPF - How to enable dragging controls into ContentControl at Design-Time? WPF - 如何在设计时启用拖动控件到ContentControl?
How to make custom WPF ContentControl that supports drag-and-drop at design time? 如何在设计时制作支持拖放的自定义WPF ContentControl?

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. 我认为在Content / UserControl的设计时间内没有任何正式的方法来启用拖放功能,它只是不受设计支持。 however you should consider inheriting from Panal, Grid, or Border. 但是你应该考虑从Panal,Grid或Border继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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