WPF - TreeView动态地将Children添加到Children

时间:2014-10-17 14:29:05

标签: c# wpf winforms treeview

我在WinForms中构建了一个完整的工作逻辑,用于在来自数据库的TreeView中插入数据,所以我不能在树视图后面放一个数据表并且很开心,因为我需要构建节点和undernodes和undernodes等。 旧WinForms中的工作代码:

    private void waregroup()
    {
        DataSet myDataSet = new DataSet();
        DataTable myDataTable = new DataTable();
        string sqlStatement = "SELECT * FROM APWGRUPP ORDER BY WG_WGSCHL ASC";
        string str_key = "";
        string str_desc = "";
        int int_node0 = 0;
        int int_node1= 0;
        int int_node2 = 0;
        int int_node3 = 0;
        int int_node4 = 0;

        myDataSet = ImportDBF(sqlStatement);
        myDataTable = myDataSet.Tables["Table"];
        foreach (DataRow row in myDataTable.Rows)
        {
            str_desc = row.Field<string>(1);
            str_key = row.Field<string>(2);
            switch (str_key.Length)
            {
                case 1:
                    trv_waregroup.Nodes.Add(str_key + "-" + str_desc);
                    int_node0++;
                    int_node1 = 0;
                    int_node2 = 0;
                    int_node3 = 0;
                    int_node4 = 0;
                    break;
                case 2:
                    trv_waregroup.Nodes[int_node0 - 1].Nodes.Add(str_key + "-" + str_desc);
                    int_node1++;
                    int_node2 = 0;
                    int_node3 = 0;
                    int_node4 = 0;
                    break;
                case 4:
                    trv_waregroup.Nodes[int_node0 - 1].Nodes[int_node1 - 1].Nodes.Add(str_key + "-" + str_desc);
                    int_node2++;
                    int_node3 = 0;
                    int_node4 = 0;
                    break;
                case 5:
                    trv_waregroup.Nodes[int_node0 - 1].Nodes[int_node1 - 1].Nodes[int_node2 - 1].Nodes.Add(str_key + "-" + str_desc);
                    int_node3++;
                    int_node4 = 0;
                    break;
                case 6:
                    trv_waregroup.Nodes[int_node0 - 1].Nodes[int_node1 - 1].Nodes[int_node2 - 1].Nodes[int_node3 - 1].Nodes.Add(str_key + "-" + str_desc);
                    int_node4++;
                    break;
                default:
                    trv_waregroup.Nodes[int_node0 - 1].Nodes[int_node1 - 1].Nodes[int_node2 - 1].Nodes[int_node3 - 1].Nodes[int_node4 - 1].Nodes.Add(str_key + "-" + str_desc);
                    break;
            }
        }
    }

现在我无法使用Items [Counter]。添加逻辑,但我能想到的唯一可能的解决方案是使用枚举。但我发现只是使用Header作为新项目来添加新节点。

我试过这种方式,但没有得到一个可用的项目:

TreeViewItem temp = (TreeViewItem)_WareGroups.Items.GetItemAt(Node0 - 1);
_WareGroups.Items.Add(GroupKey + "-" + GroupDesc);

任何人都知道如何以这种方式添加近2500件物品?

2 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。如果有人有兴趣:

XAML:

<TreeView ItemsSource="{Binding Items}" Height="{Binding ElementName=AdminConsoleWindow, Path=ActualHeight, Converter={StaticResource MathConverter}, ConverterParameter=(@VALUE/3)}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
      <TextBlock Text="{Binding Header}"/>
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

代码隐藏:

public partial class AdminConsole : Window
{
    public ObservableCollection<MyItem> Items { get; set; }

    public AdminConsole()
    {
        this.Items = new ObservableCollection<MyItem>();
        this.DataContext = this;
        InitializeComponent();
    }

    private void AddWareGroups()
    {
        DataTable MyTable = DBase.GetWareGroups();
        foreach (DataRow MyRow in MyTable.Rows)
        {
            string ItemDescription = MyRow.Field<string>(1);
            string ItemKey = MyRow.Field<string>(2);
            MyItem NewItem = new MyItem();
            NewItem.Header = ItemKey + " - " + ItemDescription;
            if (ItemKey.Length == 1)
            {
                this.Items.Add(NewItem);
            }
            else if (ItemKey.Length == 2 || ItemKey.Length == 5 || ItemKey.Length == 6)
            {
                IterateSubItems(1, NewItem, ItemKey, this.Items);
            }
            else // if (ItemKey.Length == 4 || ItemKey.Length == 8)
            {
                IterateSubItems(2, NewItem, ItemKey, this.Items);
            }
        }
    }

    private void IterateSubItems(int shortenLength, MyItem newItem, string itemKey, ObservableCollection<MyItem> myCollection)
    {
        foreach (MyItem ItemChild in myCollection)
        {
            if (ItemChild.Header.Substring(0, ItemChild.Header.IndexOf(" - ")).Equals(itemKey.Substring(0, itemKey.Length - shortenLength)))
            {
                ItemChild.SubItems.Add(newItem);
                return;
            }
            else
            {
                if (ItemChild.SubItems.Count > 0)
                {
                    IterateSubItems(shortenLength, newItem, itemKey, ItemChild.SubItems);
                }
            }
        }
    }

额外的课程:

public class MyItem : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members
    protected void OnPropertyChanged(string propertyName)
    {
        var evt = this.PropertyChanged;
        if (evt != null)
            evt(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    private string _Header = "Item";
    public string Header
    {
        get
        {
            return this._Header;
        }
        set
        {
            if (this._Header != value)
            {
                this._Header = value;
                this.OnPropertyChanged("Header");
            }
        }
    }
    private ObservableCollection<MyItem> _SubItems = new ObservableCollection<MyItem>();
    public ObservableCollection<MyItem> SubItems
    {
        get
        {
            return this._SubItems;
        }
        set
        {
            if (this._SubItems != value)
            {
                this._SubItems = value;
                this.OnPropertyChanged("SubItems");
            }
        }
    }
}

答案 1 :(得分:-1)

类似的东西:

foreach (DataRow row in myDataTable.Rows)
{
  str_desc = row.Field<string>(1);
  str_key = row.Field<string>(2);

  TreeNode node = new TreeNode(str_key + "-" + str_desc);

  int lvl = 1;

  addToLvl(lvl, trv_waregroup, trv_waregroup.Nodes[trv_waregroup.GetNodeCount(false) - 1], str_key.Length, node);

}

public void addToLvl(int actualLvl, TreeView tv, TreeNode last, int lvlToReach, TreeNode nodeToAdd)
{
  if (actualLvl == lvlToReach)
    last.Add(node); //wpf ?? last.Nodes.Add(node);
  else
    addToLvl(actualLvl++, tv, last.LastNode, lvlToreach, nodeToAdd);
}