G. *_*bol 9 wpf treeview count mvvm
我创建了一个树形视图,用于模拟硬盘上的目录和文件.每个treeviewItem都有一个复选框,绑定到isSelected属性.我想要实现的是为每个父节点显示总文件数上所选文件的数量(选择12个总数的10/12个10个文件).
有没有办法绑定属性...?
<ContentPresenter Content="{Binding MyItems.Count where MyItems.IsSelected, Mode=OneTime}"
Margin="2,0" />
Run Code Online (Sandbox Code Playgroud)
Mar*_*arc 15
无法直接过滤绑定中的集合.但是,WPF允许对集合进行过滤(以及排序和分组)CollectionViewSource.
一种方法是定义一个CollectionViewSource在你的资源,ItemTemplate其过滤ItemsSource的得到它通过结合这Count属性通过过滤器元件的数量CollectionViewSource.但是,您必须在代码隐藏中定义过滤器.看起来像这样:
<TreeView x:Name="Tree" ItemsSource="{Binding Items}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ChildItems}">
<HierarchicalDataTemplate.Resources>
<CollectionViewSource x:Key="FilteredItems"
Source="{Binding ChildItems}"
Filter="FilteredItems_OnFilter" />
</HierarchicalDataTemplate.Resources>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{} {0} of {1} selected">
<Binding Path="Count" Source="{StaticResource FilteredItems}" />
<Binding Path="ItemsSource.Count" ElementName="Tree" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Run Code Online (Sandbox Code Playgroud)
在代码隐藏中:
private void FilteredItems_OnFilter(object sender, FilterEventArgs e)
{
var item = sender as Item;
e.Accepted = item.IsSelected;
}
Run Code Online (Sandbox Code Playgroud)
我没有测试过,但它应该可以正常工作.你永远不会知道WPF,但......