我有一个ObservableCollection< Foo> Foos,我在XAML中展示.
让我们说Foo看起来像:
class Foo{ public ObservableCollection<bar> bars;}class bar{ public ObservableCollection<Qux> Quxes;}
我正在使用以下xaml显示Foos:
<GrID> <GrID.Resources> <CollectionVIEwSource x:Key="MyCVS" Source="{Binding relativeSource={relativeSource Mode=FindAncestor,AncestorType={x:Type ListVIEw}},Path=DataContext.UnifIEdSymbols}" Filter="MyCVS_Filter" /> <DataTemplate x:Key="nestedTabheaderTemplate"> <TextBlock Text="{Binding Path=name}"/> </DataTemplate> <DataTemplate x:Key="nestedTabContentTemplate"> <ListBox ItemsSource="{Binding Path=Quxes}" displayMemberPath="name"/> </DataTemplate> <DataTemplate x:Key="topLevelTabheaderTemplate"> <TextBlock Text="{Binding Path=name}"/> </DataTemplate> <DataTemplate x:Key="topLevelTabContentTemplate"> <TabControl ItemsSource="{Binding Path=bars}" ItemTemplate="{StaticResource nestedTabheaderTemplate}" ContentTemplate="{StaticResource nestedTabContentTemplate}" /> </DataTemplate> </GrID.Resources> <TabControl ItemSource="{Binding correct binding for my control's collection of Foos}" ItemTemplate="{StaticResource topLevelTabheaderTemplate}" ContentTemplate="{StaticResource topLevelTabContentTemplate}" x:name="tabControl" /></GrID>
换句话说,有一个标签控件,每个Foo都有一个标签.每个Foo都是一个选项卡控件,每个bar都包含在它自己的选项卡中.每个bar都包含其Quxes的列表框.
要么:
______ ______ ______ | Foo1 | Foo2 | Foo3 | |______ ______ | | bar1 | bar2 |______| | | qux1 || | | qux2 || | | qux3 || ----------------------
我还有一个TextBox,我想用它来过滤这个细分.当我在文本框中输入内容时,我想过滤掉quxes,以便那些不包含文本的内容不可见.理想情况下,如果bar选项卡没有可见的qux,它们也会被隐藏,而当Foo选项卡没有可见的bars时隐藏它们
我考虑过两种方法:
方法1,重置相应CollectionVIEwSources上的Filter属性
在我的文本框的TextChanged事件中,我遍历我的Foo,询问相应的(静态)TabControl的CollectionVIEwSource:
foreach(Foo foo in tabControl.Items){ var tabItem = tabControl.ItemContainerGenerator.ContainerFromItem(foo); // This is always of type TabItem // How do I get the TabControl that will belong to each of Foo's bar's?}
方法2,将ListVIEw的ItemSource声明为CollectionVIEwSource
我尝试通过更改此行来设置过滤器xaml:
<ListBox ItemsSource="{Binding Path=Quxes}" displayMemberPath="name">
对此,
<CollectionVIEwSource x:Key="MyCVS" Source="?????" Filter="MyCVS_Filter" />...<ListBox ItemsSource="{Binding Source={StaticResource MyCVS}}" displayMemberPath="name">
我尝试过很多东西,我有“?????”但我无法正确绑定到ListBox的datacontext和相应的Quxes成员.我没有尝试任何结果显示quxes,我在控制台上没有错误.即使我可以使用这种方法,我也不确定当搜索框中的文本发生变化时,我将如何重新触发此过滤器.
任何建议或方向将不胜感激.
解决方法 编辑最后,我已经满足您的要求.
Here is the link to the updated project.
(由卢克编辑)
这是我最终选择的(优秀)解决方案,所以我将提取重要的部分,并实际上将它们作为帖子的一部分:
关键的xaml部分最终看起来像这样:
<CollectionVIEwSource x:Key="FooCVS" x:name="_fooCVS" Source="{Binding Foos,relativeSource={relativeSource Mode=FindAncestor,AncestorType={x:Type WpfApplication1:MainWindow}}}" Filter="_fooCVS_Filter"/><CollectionVIEwSource x:Key="barCVS" x:name="_barCVS" Source="{Binding bars,Source={StaticResource FooCVS}}" Filter="_barCVS_Filter"/><CollectionVIEwSource x:Key="QuxCVS" x:name="_quxCVS" Source="{Binding Quxs,Source={StaticResource barCVS}}" Filter="_quxCVS_Filter"/>
我将相应的控件设置为每个视图作为控件的ItemSource.神奇的是每个CVS的绑定.每个CVS都会获取出现的控件/模板控件的数据上下文,因此您可以使用绑定对象集合的真实名称.我不确定我理解为什么绑定源绑定的源自身(CVS)有效,但它确实如此美妙.
过滤器TextBox的代码然后变成如下:
private voID filterTextBox_TextChanged(object sender,TextChangedEventArgs e){ var cvs = TryFindResource("FooCVS") as CollectionVIEwSource; if (cvs != null) { if (cvs.VIEw != null) cvs.VIEw.Refresh(); } cvs = TryFindResource("QuxCVS") as CollectionVIEwSource; if (cvs != null) { if (cvs.VIEw != null) cvs.VIEw.Refresh(); } cvs = TryFindResource("barCVS") as CollectionVIEwSource; if (cvs != null) { if (cvs.VIEw != null) cvs.VIEw.Refresh(); }}
优秀的解决方案,因为它不需要更改底层对象或层次结构.
总结以上是内存溢出为你收集整理的c# – 过滤使用嵌套的xaml数据模板显示的分层对象全部内容,希望文章能够帮你解决c# – 过滤使用嵌套的xaml数据模板显示的分层对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)