I have a collection of Database objects, each containing collections of Schema objects and User objects. I want to bind them to a TreeView, but adding
The problem is that a TreeView is not very well suited to what you want to acomplish: It expects all the subnodes to be of the same type. As your database node has a node of type Collection
The code below does what you want I think,while being as close as possible to your original intent:
using System.Collections.ObjectModel;
using System.Windows;
namespace TreeViewSelection
{
public partial class Window1 : Window
{
public ObservableCollection DataBases { get; set; }
public Window1()
{
InitializeComponent();
DataBases = new ObservableCollection
{
new Database("Db1"),
new Database("Db2")
};
DataContext = this;
}
}
public class Database:DependencyObject
{
public string Name { get; set; }
public ObservableCollection Schemas { get; set; }
public ObservableCollection Users { get; set; }
public Database(string name)
{
Name = name;
Schemas=new ObservableCollection
{
new Schema("Schema1"),
new Schema("Schema2")
};
Users=new ObservableCollection
{
new User("User1"),
new User("User2")
};
}
}
public class Schema:DependencyObject
{
public string Name { get; set; }
public Schema(string name)
{
Name = name;
}
}
public class User:DependencyObject
{
public string Name { get; set; }
public User(string name)
{
Name = name;
}
}
}