我有嵌套的自定义ContentControls,我很难让内部控件在外部控件内部渲染。以下是两个控件组成方式的示例:
<Window ...>
<StackPanel>
<local:Outer> <!-- Nothing in here gets displayed -->
<local:Inner>
<TextBlock>Fahrvergnügen</TextBlock>
</local:Inner>
</local:Outer>
<local:Inner> <!-- This is displayed just fine -->
<TextBlock>some text</TextBlock>
</local:Inner>
</StackPanel>
</Window>
Inner和Outer的样式定义如下:
<ResourceDictionary ...>
<Style TargetType="{x:Type local:Outer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Outer}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:Inner}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Inner}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
额外的皱纹是Outer正在使用指向List的ContentPropertyAttribute:
[ContentPropertyAttribute("InnerItems")]
public class Outer : ContentControl
{
private List<Inner> innerItems = new List<Inner>();
static Outer()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Outer), new FrameworkPropertyMetadata(typeof(Outer)));
}
public List<Inner> InnerItems { get { return this.innerItems; } }
}
为了完整性,这里是Inner的定义,这是微不足道的:
public class Inner : ContentControl
{
static Inner()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Inner), new FrameworkPropertyMetadata(typeof(Inner)));
}
}
我猜测问题是Outer的内容无法用简单的<ContentPresenter />
显示,但我不确定要用什么替换它。建议?