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

时间:2017-02-23 09:36:09

标签: c# wpf xaml

我正在开发一个WPF自定义控件,它使用元素并隐藏/使用切换按钮显示它们。

要隐藏的元素应由设计时的控件用户定义。

我的控制权的重要部分如下所示:

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

相应的ContentPresenter如下:

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

我现在的问题是,我需要将ContentPresenter的高度设置为0,因为我希望在开始时隐藏内容。有没有一种方法我可以允许我的控件的用户在他的设计时间通过拖放添加元素? 此控件应在XAML中完全运行。 Code Behind中定义的唯一内容是属性。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

即使ContentPresenter未设置为0高度,您也无法将UIElements拖放到控件中。

在设计时查看有关WPF控件拖放的类似问题 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?

在我看来,没有任何官方方法可以在设计时为Content / UserControl启用拖放功能,但设计并不支持它。但是你应该考虑从Panal,Grid或Border继承。