【问题标题】:Bind Tree view to object and not to List<object>将树视图绑定到对象而不是 List<object>
【发布时间】:2013-04-21 14:52:34
【问题描述】:

这可能是一个非常简单的问题,但我仍然不知道如何解决。

我的 MVVM 应用程序中有一个模型,我想将它绑定到树视图。但是,我只能将树视图绑定到项目列表(或者在我的情况下是 ObservableCollection)。 这是我目前使用的模型:

/// <summary>
/// Represents an group a character can belong to.
/// </summary>
public class OrganisationBase : ModelBase<OrganisationBase>
{
    /// <summary>
    /// The name.
    /// </summary>
    private string name;

    /// <summary>
    /// The parent organization.
    /// </summary>
    private ObservableCollection<OrganisationBase> parentOrganizations;

    /// <summary>
    /// Gets or sets the name of the organization.
    /// </summary>
    public string Name
    {
        get
        {
            return this.name;
        }

        set
        {
            this.name = value;
            this.NotifyPropertyChanged(p => p.Name);
        }
    }

    /// <summary>
    /// Gets or sets the parent organization.
    /// </summary>
    public ObservableCollection<OrganisationBase> ParentOrganizations
    {
        get
        {
            return this.parentOrganizations;
        }

        set
        {
            this.parentOrganizations = value;
            this.NotifyPropertyChanged(p => p.ParentOrganizations);
        }
    }
}

现在我的问题是:如何在不使用 Observable 集合的情况下将此模型绑定到我的树视图。我想这样做,因为不需要 ObservableCollection,因为每个组织只能有一个父级。

或者,如何将以下代码绑定到我的树视图:

/// <summary>
/// Represents an group a character can belong to.
/// </summary>
public class OrganisationBase : ModelBase<OrganisationBase>
{
    /// <summary>
    /// The name.
    /// </summary>
    private string name;

    /// <summary>
    /// The parent organization.
    /// </summary>
    private OrganisationBase parentOrganizations;

    /// <summary>
    /// Gets or sets the name of the organization.
    /// </summary>
    public string Name
    {
        get
        {
            return this.name;
        }

        set
        {
            this.name = value;
            this.NotifyPropertyChanged(p => p.Name);
        }
    }

    /// <summary>
    /// Gets or sets the parent organization.
    /// </summary>
    public OrganisationBase ParentOrganizations
    {
        get
        {
            return this.parentOrganizations;
        }

        set
        {
            this.parentOrganizations = value;
            this.NotifyPropertyChanged(p => p.ParentOrganizations);
        }
    }
}

这是我目前使用的树视图的代码:

<TreeView ItemsSource="{Binding Path=Character.CharacterAllegiances.MemberOf}">
  <TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="IsExpanded"
              Value="True" />
    </Style>
  </TreeView.ItemContainerStyle>
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding ParentOrganizations}">
      <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

注意:组织属性是角色模型的一部分。仍然当我使用 ObservableCollections 时,一切都显示得很好。但是如上所述,我想丢弃这个 ObservableCollections。

问候 鲁尔波特爱国者

【问题讨论】:

    标签: c# .net xaml mvvm


    【解决方案1】:

    TreeView 需要一个项目集合来构建它的节点,这显然是正确的方法,并且非常不言自明。

    将树视图绑定到单个对象仅在该对象将自身公开为集合时才有效,例如实现IEnumerable,这样树视图将使用IEnumerable 实现来填充数据。

    或者,您可以只绑定到针对对象的集合,例如

    <TreeView ItemsSource="{Binding object.SomeCollection}" />
    

    【讨论】:

    • 好的,看来我只能绑定到暴露 IEnumerator 的东西。感谢您的澄清。我会重新考虑存储这些信息的方法,也许我会想出一个更好的系统。
    【解决方案2】:

    您需要使用 HierarchicalDataTemplate。

    要绑定数据(例如上面的文件夹和文件),您可以参考这篇文章: WPF Treeview: data binding of heterogeneous types of data using CompositeCollection class

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-20
      • 2022-01-08
      • 2012-06-21
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      相关资源
      最近更新 更多