drv*_*rvj 3 data-binding wpf listview
我创建了一个用户控件,其中包含一个带有自定义ItemsPanelTemplate的ListView.
<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>
Run Code Online (Sandbox Code Playgroud)
UserControl的DataContext有两个属性Start和UnitsPerSecond.当我使用群组时,我不能简单地写
Start={Binding Path=.Start}
Run Code Online (Sandbox Code Playgroud)
因此我使用了上面的代码.但是如果我将Start的绑定更改为this,我会得到一个异常:
ItemsPanelTemplate的VisualTree必须是单个元素.
很明显,ItemsPanelTemplate只有一个元素.
那可能是什么问题呢?我的自定义面板不会创建任何元素.它只是安排他们.
您收到此异常可能是因为您尝试将"Children"添加到TimeLinePanel(或者您正在覆盖面板的可视树并在"VisualChildrenCount"中返回除1以外的内容).遗憾的是,如果由于ItemsPanelTemplate而在ItemsControl中创建了面板的"Children"属性,则无法修改它.在ItemsControl之外,没有问题.
或者,您可以将ListView的模板覆盖为类似下面的内容 - 它适用于我的情况,并消除异常.
<ListView.Template>
<ControlTemplate>
<cd:TimeLinePanel IsItemsHost="True" UnitsPerSecond="..."/>
<ControlTemplate>
</ListView.ItemsPanel>
Run Code Online (Sandbox Code Playgroud)
有一篇CodeProject文章(http://www.codeproject.com/KB/WPF/ConceptualChildren.aspx),它提供了有关此行为的一些详细信息,这可能有助于您理解为什么会出现这种情况.
归档时间: |
|
查看次数: |
3440 次 |
最近记录: |