首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ItemsPanelTemplate of a ListView抛出明显错误的异常

ItemsPanelTemplate of a ListView抛出明显错误的异常
EN

Stack Overflow用户
提问于 2009-08-27 19:11:04
回答 1查看 4K关注 0票数 3

我创建了一个用户控件,它包含一个带有自定义ListView的ItemsPanelTemplate。

代码语言:javascript
复制
<UserControl x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="thisUserControl">
<ListView ItemsSource="{Binding Source={StaticResource cvs}}"
          Name="mainListView">
    <ListView.GroupStyle>
        <GroupStyle>
            ...
        </GroupStyle>
    </ListView.GroupStyle>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <cd:TimeLinePanel UnitsPerSecond="{Binding ElementName=thisUserControl,Path=DataContext.UnitsPerSecond}" Start="{Binding ElementName=thisUserControl, Path=DataContext.Start}"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

DataContext的UserControl有两个属性Start和UnitsPerSecond。因为我正在使用组,所以我不能简单地写

代码语言:javascript
复制
Start={Binding Path=.Start}

因此,我使用了上面的代码。但是,如果我更改了Start的绑定,就会得到一个例外:

VisualTree of ItemsPanelTemplate必须是一个单一元素。

显然,ItemsPanelTemplate只有一个元素。

那么有什么问题呢?我的自定义面板不创建任何元素。只是安排了他们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-09-17 17:26:47

您收到此异常可能是因为您试图将“子”添加到TimeLinePanel (或者您正在覆盖面板的可视树并在“VisualChildrenCount”中返回1以外的内容)。遗憾的是,如果面板是在ItemsControl中创建的,则不能修改它的“ItemsPanelTemplate”属性。在ItemsControl之外,没有问题。

或者,您可以覆盖ListView的模板如下所示-它在我的情况下工作,并消除异常。

代码语言:javascript
复制
<ListView.Template>
    <ControlTemplate>
        <cd:TimeLinePanel IsItemsHost="True" UnitsPerSecond="..."/>
    <ControlTemplate>
</ListView.ItemsPanel>

有一篇CodeProject文章(http://www.codeproject.com/KB/WPF/ConceptualChildren.aspx)给出了关于这种行为的一些细节,这可能有助于您理解为什么会出现这种情况。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1343205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档