简体   繁体   中英

Designer process terminated in Custom Control with Binding Content

I have created a custom User Control from a Gruopbox. However, in order to be used as a container in a view, I must create a DependecyProperty for the Content . This results in an Unhandled Exception has occurred error in VS2017.

错误图片

This however, only happens when I bind the Content attribute in the gruopbox to my new Property.

<UserControl
    x:Class="Infrastructure.Controls.GroupBox.CollectionBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="Form"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">

    <GroupBox Content="{Binding Content, ElementName=Form}"/>

</UserControl>

With the code behind being

public new object Content
    {
        get => (object)GetValue(ContentProperty);
        set => SetValue(ContentProperty, value);
    }
public new static readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(CollectionBox), new PropertyMetadata(null));

I tried using FalloutValue in the Binding to other controls as I assumed the designer didn't know what to put inside the container. Nevertheless, the error keeps occurring.

In runtime and in the view Designer the control looks and works fine. It is just in its designer that I can not see it.

Thanks.

You don't need another Content property, just another ControlTemplate, that defines the visual structure of your control, including the GroupBox that binds to the control's Content :

<UserControl x:Class="Infrastructure.Controls.GroupBox.CollectionBox" ...>
    <UserControl.Template>
        <ControlTemplate TargetType="UserControl">
            <Border> <!-- or any other control(s) here -->
                <GroupBox Content="{TemplateBinding Content}"/>
            </Border>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

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