一个ListView的ItemsPanelTemplate apperently抛出worngly异常 [英] ItemsPanelTemplate of a ListView throws apperently worngly an exception
问题描述
我创建了包含自定义ItemsPanelTemplate一个ListView的用户控件。
I created a user control which contains a ListView with a custom ItemsPanelTemplate.
<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的有两个属性开始和UnitsPerSecond。当我使用我组不能简单地写
The DataContext of the UserControl has two properties Start and UnitsPerSecond. As I am using groups I cannot simply write
Start={Binding Path=.Start}
于是我用上面的code。但是,如果我更改启动的结合这一点,我得到一个异常:
Thus I used the code above. But if I change the binding of Start to this I get an exception:
的 ItemsPanelTemplate的的VisualTree必须是单个元素。的
显然ItemsPanelTemplate得到了只是一个单一的元件。
Obviously the ItemsPanelTemplate has got just a single element.
所以,可能是什么问题呢?我的自定义面板不会产生任何元素。它只是安排他们。
So what could be the problem? My custom panel does not create any elements. It just arranges them.
先谢谢了。
推荐答案
您很可能收到此异常,因为你想孩子添加到TimeLinePanel(或要覆盖面板的可视化树,并返回比其他东西1VisualChildrenCount)。可悲的是,如果是因为ItemsPanelTemplate的一个ItemsControl内部创建你不能修改面板的孩子属性。一个ItemsControl以外,不存在问题。
You are receiving this exception probably because you are trying to add "Children" to TimeLinePanel (or you are overriding the visual tree of the panel and returning something other than 1 in "VisualChildrenCount"). Sadly, you cannot modify the "Children" property of a panel if it is created inside an ItemsControl because of an ItemsPanelTemplate. Outside of an ItemsControl, there is no problem.
另外,你可以覆盖的ListView的模板类似下面的 - 它的作品在我的情况,并摆脱了异常
Alternatively, you can override the template of ListView to something like the following - it works in my case, and gets rid of the exception.
<ListView.Template>
<ControlTemplate>
<cd:TimeLinePanel IsItemsHost="True" UnitsPerSecond="..."/>
<ControlTemplate>
</ListView.ItemsPanel>
有一个$ C $的CProject文章( HTTP:// WWW。 codeproject.com / KB / WPF / ConceptualChildren.aspx ),这也是对这种行为的一些细节,这可能会帮助你理解为什么它是这样的。
There is a CodeProject article (http://www.codeproject.com/KB/WPF/ConceptualChildren.aspx) which gives some details on this behavior, which may help you understand why it is the case.
这篇关于一个ListView的ItemsPanelTemplate apperently抛出worngly异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!