如何从数据库 WPF 创建带有父/子的动态 TreeView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14190169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:55:45  来源:igfitidea点击:

How to create Dynamic TreeView with Parent/Child from database WPF

c#wpfwindows

提问by Rocky

I am trying to create a Dynamic Treeview with the following code. I am using User Control

我正在尝试使用以下代码创建动态树视图。我正在使用用户控制

On Control Load

控制负载

TreeViewItem treeviewItems = new TreeViewItem();
treeviewItems.ItemsSource = TreeViewDataSource.DefaultView;
treeviewItems.ItemTemplate = GetHierarchicalData(ID, Desc);
treeViewCntrl.Items.Add(treeviewItems);

public HierarchicalDataTemplate GetHierarchicalData(string id, string desc)
{
   HierarchicalDataTemplate hierdatatemp = null;
   try
   {
    hierdatatemp = new HierarchicalDataTemplate(typeof(DataTable));
    hierdatatemp.ItemsSource = new Binding(itemSourceBindingName);
    FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
    textBlock.SetBinding(TextBlock.TextProperty, new Binding(desc));
    hierdatatemp.VisualTree = textBlock;
    }
    catch (Exception ex)
    {  }
    return hierdatatemp;
 }

With this code I am able to add an item but it is not adding properly. First it is adding an empty node under and the items are getting added after that.

使用此代码,我可以添加一个项目,但添加不正确。首先,它在下面添加一个空节点,然后添加项目。

What I want is items should add without adding any empty node, and on clicking on the Parent node, Child node should get added.

我想要的是在不添加任何空节点的情况下添加项目,并且在单击父节点时,应该添加子节点。

How to add a child node into parent node?

如何将子节点添加到父节点?

回答by Rocky

public TreeViewItem CreateTreeViewItem(string nodeName, string headerText, string ImagePath)
{
        TreeViewItem treeViewItem = new TreeViewItem();
        try
        {
            StackPanel stackPanel = new StackPanel();
            Label lblHeaderText = new Label();
            Image imgFrontIcon;
            imgFrontIcon = new Image(); 

            stackPanel.Orientation = Orientation.Horizontal;

            if (ImagePath != null && ImagePath != string.Empty)
            {
                Uri uri = new Uri(@"pack://application:,,," + ImagePath);
                BitmapImage bitMapSource = new BitmapImage();
                bitMapSource.BeginInit();
                bitMapSource.UriSource = uri;
                bitMapSource.EndInit();
                imgFrontIcon.Source = bitMapSource;
            }

            lblHeaderText.Content = headerText;
            stackPanel.Children.Add(imgFrontIcon);
            stackPanel.Children.Add(lblHeaderText);
            nodeName = nodeName.Replace("-", "_");
            treeViewItem.Name = nodeName;
            treeViewItem.Header = stackPanel;
        }
        catch (Exception ex)
        { }
        return treeViewItem
}

回答by Sebastian K

I found using a treeview in WPF quite challenging. This article was very helpful:

我发现在 WPF 中使用树视图非常具有挑战性。这篇文章很有帮助:

http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode

http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode