我已经从Gruopbox创建了一个自定义用户控件。但是,为了在视图中用作容器,我必须为DependecyProperty
创建一个Content
。这导致VS2017中的Unhandled Exception has occurred
错误。
但是,只有当我将gruopbox中的Content
属性绑定到我的新属性时才会发生这种情况。
<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>
随着代码背后的存在
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));
我尝试在绑定到其他控件中使用FalloutValue
,因为我认为设计师不知道放在容器中的内容。然而,错误不断发生。
在运行时和视图设计器中,控件看起来很好。只是在它的设计师中,我看不到它。
谢谢。
您不需要另一个Content属性,只需要另一个ControlTemplate,它定义控件的可视结构,包括绑定到控件的Content
的GroupBox:
<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>