這可能是一個非常簡單的問題,但我不知道如何解決這個問題。將樹視圖綁定到對象而不是列表<object>
我有一個模型在我的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);
}
}
}
現在我的問題是:如何可以綁定這個模型我的樹視圖,而無需使用觀察集合。我想這樣做,因爲不需要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。
問候 Ruhrpottpatriot
好吧,看來我只能綁定的東西,暴露的IEnumerator。感謝您的澄清。我會重新考慮存儲這些信息的方法,也許我會想出一個更好的系統。 – Ruhrpottpatriot 2013-04-21 20:02:57