Это может быть очень простой вопрос, но я не знаю, как его решить.
У меня есть модель в моем приложении 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.
Привет Рурпоттпатриот