I'm simply trying to bind my tree view in xaml but for some reason the list does not seem to be showing, I have attached the code. Any help would be great.
Many thanks
class Item
{
public string Header { get; set; }
public List<string> TreeItems { get; set; }
public List<Item> SubItems { get; set; }
}
this is the view model
public StoreLibraryViewModel()
{
this.DisplayName = "hierarchy Library";
TreeItems = new ObservableCollection<Item>();
string[] data = new string[]{
"Warwick","Birmingham",
};
Item item = new Item()
{
Header = "Region",
TreeItems = new List<string>(data)
};
TreeItems.Add(new Item()
{
Header = "Region 1",
SubItems = new List<Item>(new Item[] { item }),
TreeItems = new List<string>(data)
});
TreeItems.Add(new Item()
{
Header = "Region 2",
SubItems = new List<Item>(new Item[] { item }),
TreeItems = new List<string>(data)
});
TreeItems.Add(new Item()
{
Header = "Region 3",
SubItems = new List<Item>(new Item[] { item }),
TreeItems = new List<string>(data)
});
}
ObservableCollection<Item> TreeItems { get; set; }
this is the XAML
TreeView ItemsSource="{Binding TreeItems}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
<Expander Header="{Binding Header}">
<ItemsControl Margin="25,0,0,0" ItemsSource="{Binding TreeItems}" />
</Expander>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
All I seem to be displaying is a empty tree view, any assistance would be greatly appreciated.
Many Thanks
You just have to assign your datacontext in the xaml code like this:
<Window ....
xmlns:local="clr-namespace:myStoreLibraryViewModelNameSpace"
....
/>
<Window.DataContext>
<local:StoreLibraryViewModel />
</Window.DataContext>
....
where myStoreLibraryViewModelNameSpace is the namespace in which StoreLibraryViewModel is located
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.