如何将可观察集合绑定到Wpf树视图 [英] How to bind observable collection to Wpf Tree view

查看:80
本文介绍了如何将可观察集合绑定到Wpf树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将可观察集合绑定到Wpf树视图?给我一个很好的例子

How to bind observable collection to Wpf Tree view? give me good example

推荐答案

通过使用ItemSource属性,你可以将treeview绑定到Observable集合

例如: -

By using ItemSource property you can bind treeview to Observable collection
For Eg:-
<treeview itemssource="{x:Static local:Window1.AnimalCategories}">

   <treeview.itemtemplate>
     <hierarchicaldatatemplate itemssource="{Binding Path=Animals}">

       <textblock fontweight="Bold" text="{Binding Path=Category}" />

       <hierarchicaldatatemplate.itemtemplate>

         <datatemplate>

           <textblock text="{Binding Path=Name}" />

         </datatemplate>
       </hierarchicaldatatemplate.itemtemplate>

     </hierarchicaldatatemplate>

   </treeview.itemtemplate>

 </treeview>





欲了解更多信息,请查看此链接



WPF Treeview:样式和使用MVVM进行模板绑定 [ ^ ]



谢谢

Sujeet



for more information please check this link

WPF Treeview: Styling and Template Binding using MVVM[^]

Thanks
Sujeet


您好。如果在后面的代码中定义ObservableCollection,则必须为treeView设置x:name并在其加载的事件中写入:



private void myTreeView_Loaded(object sender,RoutedEventArgs e )

{

myTreeView.DataContext = this.DataContext;

}



和你的xaml中的ItemsSource:

ItemsSource ={Binding}



GoodLuck。
Hi. if you define your ObservableCollection in code behind, you must set x:name for your treeView and in its loaded event write:

private void myTreeView_Loaded(object sender,RoutedEventArgs e)
{
myTreeView.DataContext=this.DataContext;
}

and in your xaml set ItemsSource :
ItemsSource="{Binding}"

GoodLuck.


这篇关于如何将可观察集合绑定到Wpf树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆