【问题标题】:Display treeviewitem as grid rows in wpf where the branches might have a different depth在 wpf 中将 treeviewitem 显示为网格行,其中分支可能具有不同的深度
【发布时间】:2015-07-11 10:43:53
【问题描述】:

我正在尝试创建一个 TreeView,每行包含多个列数据(例如,我的示例中的 2 列,属性和值),类似于 this thread。 Heena 提出的解决方案的不同之处在于树可能更复杂一些。每个分支的深度可能不同(因此在某些情况下,节点的子节点可能比其他节点多)。

我会用下面的类来实现代码,这样嵌套元素是可能的:

public class TreeItem
{
  private TreeItem()
  {
  }

  public TreeItem( string property, string value )
  {
    if( property == null )
    {
      property = string.Empty;
    }

    if( value == null )
    {
      value = string.Empty;
    }

    _property = property;
    _value = value;
  }

  public TreeItem( params TreeItem[] items )
  {
    AddItems( items );
  }

  public TreeItem( string property, string value, params TreeItem[] items ) : this( property, value )
  {
    AddItems( items );
  }

  public string Property
  {
    get
    {
      return _property;
    }
  }

  public string Value
  {
    get
    {
      return _value;
    }
  }

  public IEnumerable<TreeItem> Items
  {
    get
    {
      return (IEnumerable<TreeItem>)_items;
    }
  }

  private void AddItems( params TreeItem[] items )
  {
    foreach( TreeItem item in items )
    {
      if( item != null )
      {
        _items.Add( item );
      }
    }
  }

  private readonly string _property;

  private readonly string _value;

  private readonly List<TreeItem> _items = new List<TreeItem>();
}

是否可以使用 WPF 实现这一点?如果是,怎么做?

【问题讨论】:

  • 与其手动创建树,实际上可能更容易修改数据模板以遵守数据并绑定到它。您的数据结构如何?你能举一个数据的例子吗?
  • 有一个基于 ASN1 的通信协议。这些类是使用 ASN1 编译器自动创建的。从这些类中,我计划将子类实现为 ViewModel,以从它们自己的属性中获取 TreeItem。当数据从 TCP/IP 进来时,解码器会自动将数据翻译成这些 ASN1 对象,最终自动生成树。

标签: c# wpf xaml grid treeviewitem


【解决方案1】:
猜你喜欢
  • 2014-08-25
  • 2021-03-26
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多