none
wpf treeview如何绑定对象集合? 绑定之后怎么根据treeview.SelectedItem获得选中的对象 RRS feed

  • 问题

  • 例如有一个类,其中有两个字段,如何让一个字段绑定treeviewitem的header属性呢?数据模板不行。绑定之后能否根据treeview.SelectedItem获得选中的被绑定的数据对应的数据对象?就像listview绑定对象集合,通过SelectedItem获得选中的对象能直接转成被绑定集合的对应的数据对象
    2016年8月29日 11:02

答案

  • Hi 轮回的齿轮,

    >>如何让一个字段绑定treeviewitem的header属性呢

    我尝试做了一个小程序,通过 HierarchicalDataTemplate 来设定。

    >>绑定之后能否根据treeview.SelectedItem获得选中的被绑定的数据对应的数据对象?

    TreeViewItem本身有一个Selected事件,其参数e中就提供了需要的TreeViewItem对象。 你可以使用此方法来获取你选中的数据源

    下面的程序代码供你参考,希望能帮助到你。

    XAML:

    <Grid Loaded="Grid_Loaded" >
        <TreeView Name="tv" ItemsSource="{Binding}" Background="Beige"  SelectedItemChanged="tv_SelectedItemChanged" TreeViewItem.Selected="tv_Selected">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=Folders}">
                    <Border BorderBrush="BurlyWood" BorderThickness="1">
                        <TreeViewItem>
                            <TreeViewItem.Header>
                                <TextBlock Text="{Binding Name}"></TextBlock>
                            </TreeViewItem.Header>
                        </TreeViewItem>
                    </Border>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>

    C# Code:

    public partial class testTreeViewss : Window
    {
    
        public testTreeViewss()
        {
            InitializeComponent();
        }
    
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            List<Folder> folders = new List<Folder>();
            folders.Add(Folder.CreateFolderTree(@"D:\12_files"));
            tv.DataContext = folders;
        }
    
        private void tv_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            //TreeViewItem item = (TreeViewItem)(tv.ItemContainerGenerator.ContainerFromIndex(tv.Items.CurrentPosition));
            TreeViewItem item = (TreeViewItem)tv.ItemContainerGenerator.ContainerFromItem(tv.Items.CurrentItem);
            //返回绑定数据对象本身
            ItemCollection dgItems = item.Items;
        }
    
        private void tv_Selected(object sender, RoutedEventArgs e)
        {
            TreeViewItem tvi = e.OriginalSource as TreeViewItem;
            Folder folder = (Folder)tvi.Header;
            string name = folder.Name;
            string Path = folder.Path;
            MessageBox.Show(name + " - " + Path);
        }
    }
    
    public class Folder
    {
        public string Name
        {
            get
            {
                if (!String.IsNullOrEmpty(Path))
                {
                   return System.IO.Path.GetFileName(Path);
                }
                return null;
            }
        }
    
        public string Path
        { get; set; }
    
    
        public List<Folder> Folders
        { get; set; }
    
        public Folder()
        {
            Folders = new List<Folder>();
        }
    
        public static Folder CreateFolderTree(string rootFolder)
        {
            Folder fld = new Folder { Path = rootFolder };
            foreach (var item in System.IO.Directory.GetDirectories(rootFolder))
            {
                fld.Folders.Add(CreateFolderTree(item));
            }
            return fld;
        }
    }

    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年8月30日 14:17