【发布时间】:2012-07-15 03:18:48
【问题描述】:
我有一个自定义面板,我在其中声明了一个自定义属性来保存内容(我不想使用 Children 作为内容):
[ContentProperty(Name = "PanelContent")]
public class CustomPanel : Panel
{
public static readonly DependencyProperty PanelContentProperty =
DependencyProperty.Register("PanelContent",
typeof(Collection<UIElement>), typeof(CustomPanel),
new PropertyMetadata(new Collection<UIElement>(), null));
public Collection<UIElement> PanelContent
{
get
{
return (Collection<UIElement>)GetValue(PanelContentProperty);
}
}
}
这样使用时效果很好:
<CustomPanel>
<TextBlock>A</TextBlock>
<TextBlock>B</TextBlock>
</CustomPanel>
但是当我想将面板用作 ItemsControl 中的 ItemsPanelTemplate 时,ContentProperty 属性将被忽略,并将所有内容添加到 Children 集合中,而不是 PanelContent 集合中:
<ItemsControl ItemTemplate="{StaticResource ReviewTemplate}" ItemsSource="{Binding Reviews}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<CustomPanel></CustomPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
这不是它应该如何工作的。根据documentation:
ItemsPanelTemplate 对象元素应该包含一个FrameworkElement 派生类,该类用作项目的根元素。在大多数情况下,这是一个 Panel 派生类。扩展模板作为已实现项的父项,通常有多个项。因此,ItemsPanelTemplate 的预期根元素的 XAML 内容属性应该支持集合,就像 Panel.Children 所做的那样。
【问题讨论】:
标签: c# xaml windows-8 microsoft-metro