English 中文(简体)
积极填充树木的更好方法
原标题:A better way to dynamically fill in the TreeView
  • 时间:2010-09-08 06:17:50
  •  标签:
  • wpf
  • mvvm

我想动态地用诺德建造一View树,代之以典型的树 no。 诺德人喜欢

class Node
{
    public Node(string cont) {
        Content = cont;
        Children = new List<Node>();
    }

    public string Content { get; set; }
    public List<Node> Children { get; set; }
    public bool IsLeaf {
        get { return Children.Count == 0; }
    }
    public bool IsVisible {
        get { return true; }
    }
}

为了做到这一点,我写了简单的树木渗透器,增加了“树林”号。

   void XmlTreeTraversal(DataPoolNode curNode, TreeViewItem curViewNode) {
        if (curNode.IsLeaf)
            return;

        var contentNode = (DataPoolNode)curNode;
        foreach (var node in contentNode.Children) {
            TreeViewItem childViewNode = AddNewNodeToTreeView(node.Content, curViewNode);

            XmlTreeTraversal(node, childViewNode);
        }
    }

    TreeViewItem AddNewNodeToTreeView(string description, TreeViewItem curViewNode) {
        TreeViewItem newTVI = new TreeViewItem();
        newTVI.Header = description;
        curViewNode.Items.Add(newTVI);
        return newTVI;
    }

这种做法的问题是数据和观点相互交织。 因此,它没有达到MVC。 也许,你们知道这个问题的另一个解决办法?

最佳回答

不要 create树。 树木的电荷控制是数据集和甚微的微型和小型设备集权器之一。 否则,我们就会痛苦地工作。 有了数据集和磁力测,使用树木真的容易:

你们已经拥有丰富的树木。 页: 1 列出根基清单,作为你们的树木来源......

m_treeView.ItemsSource=new List<Node>(){yourRootNode};

......为你们的节点做一个等级制度。 ......

<HierarchicalDataTemplate x:Key="TreeViewItem_HierarchicalDataTemplate" ItemsSource="{Binding Children}" >
         <TextBlock Text="{Binding Content}"  />                
</HierarchicalDataTemplate>

......并据此确定树木的标本......

<TreeView Name="m_treeView" ItemTemplate="{StaticResource TreeViewItem_HierarchicalDataTemplate}" .../>

如果诺德-阶级已经是“观点”的话,还可以包括伊塞斯当选和伊斯凡德-property,以及将项目ContainerStyle-properies附在其中(不要忘记关于公用商品的发明)。

<TreeView.ItemContainerStyle>                
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
</TreeView.ItemContainerStyle>

从一开始,它的工作就少了,但如果你想采取更复杂的行动,如自动选择和扩大行动,就可以节省时间。

问题回答

暂无回答




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签