【发布时间】:2018-03-22 17:00:08
【问题描述】:
我正在创建一个自定义控件,其中包含要显示的对象集合。我希望这是一个包含一些东西的包装器和一个内部部分,它承载由控件的使用者确定的其他东西。基本上,我希望它的使用与 StackPanel 类似,用户在其中创建它并在内容部分添加控件。我基本上有一个我无法弄清楚的事情。这是需要设置的“页面”依赖属性:
public static readonly DependencyProperty PagesProperty =
DependencyProperty.Register("Pages", typeof(IEnumerable<MyContentPage>), typeof(UC_ApplicationWindow),
new PropertyMetadata(new List<MyContentPage>()));
public IList<MyContentPage> Pages
{
get => (IList<MyContentPage>)GetValue(PagesProperty);
set => SetValue(PagesProperty, value);
}
这是当前正在运行的控件的消费者窗口中的代码:
<graphicElements:UC_ApplicationWindow.Pages>
<x:Array Type="graphicElements:MyContentPage">
<graphicElements:MyContentPage>
<graphicElements:MyContentPage.Content>
...the contents of the page
</graphicElements:MyContentPage.Content>
</graphicElements:MyContentPage>
<graphicElements:MyContentPage>
<graphicElements:MyContentPage.Content>
...the contents of the page
</graphicElements:MyContentPage.Content>
</graphicElements:MyContentPage>
</x:Array>
</graphicElements:UC_ApplicationWindow.Pages>
理想情况下,我希望能够为 Pages 属性设置多个元素,而不必给它一个数组,但是如果我尝试直接这样做,我会收到错误“无法分配指定的值。预期以下类型:“IList'1”。我也希望能够像大多数其他 WPF 控件一样直接指定页面内容,但是当我取出直接内容属性时,它会显示“'MyContentPage' 类型不支持直接内容”。这是我最终希望它能够看起来的样子:
<graphicElements:UC_ApplicationWindow.Pages>
<graphicElements:MyContentPage>
...the contents of the page
</graphicElements:MyContentPage>
<graphicElements:MyContentPage>
...the contents of the page
</graphicElements:MyContentPage>
</graphicElements:UC_ApplicationWindow.Pages>
正如我所说,它目前有效,如果这是我必须做的,我可以,但它似乎有点笨拙,我有预感它可以更好地工作,但无法弄清楚如何。有人有我丢失的魔法钥匙吗?
【问题讨论】:
标签: wpf xaml user-controls dependency-properties