Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Is there any simple tutorial for beginners about treeview binding in WPF?
What should we write in ItemsSource, DataType, ItemTemplate attributes if there's
one List of items?
IList<string> items = new List<string>();
items.Add("item1");
items.Add("item2");
items.Add("item3");
XAML code:
<TreeView Name="treeView1">
<TreeView.Resources> <!-- what does it mean? -->
<HierarchicalDataTemplate DataType="???" ItemsSource="{Binding ???}"></HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
To Fully understand how to use the wpf treeview with data binding, I went through the following tutorials in order -
A very simple example of treeview binding using recursion
http://testdrivendevelopment.wordpress.com/2008/07/15/databinding-wpf-treeview-using-recursion/
Claus Konrads simple example of data binding with the treeview. It's the most straightforward example I have come across and should get any newcomers to wpf up to speed.
http://blog.clauskonrad.net/2011/04/how-to-make-hierarchical-treeview.html
Mike Hillbergs tutorial shows, in detail, the ins and outs of the treeview, how it compares to other wpf controls, and how to bind data.
http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-by-step.aspx
The trick is that ItemsSource points to the next collection down.
e.g Imagine you have a collection of type A, and each A contains a description and a collection of type B; and each B contains a description and a collection of type C. The binding would look like this:
<TreeView Width="400" ItemsSource="{Binding CollectionOfA}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type TypeA}" ItemsSource="{Binding CollectionOfB}">
<TreeViewItem Header="{Binding TypeADescription}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type TypeB}" ItemsSource="{Binding CollectionOfC}">
<TreeViewItem Header="{Binding TypeBDescription" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type TypeC}">
<TreeViewItem Header="{Binding TypeC}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
Have a look at Josh Smiths excellent tutorial
Treeview is one control in wpf that you have to appoach in a little diffrent manner.It is simple and efficient and at the same time a pain to understand and get in track for a beginer,especially those coming from the windows appliaction backgroud.Please go through the MVVM pattern first and then try to approach the treeview.
The Josh Smith article below is a good place to start.
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to show in WPF a grid (no matter if grid, datagrid etc.). Every cell should be filled by a usercontrol. The usercontrol gets their content by an object.
So in the beginning I there's an object list List. The number of columns of the grid is given. The number of rows results from the number of objects in the list (this is not my problem). Every object can be shown in a usercontrol.
MyObject1 + MyUserControl -> Cell1
MyObject2 + MyUserControl -> Cell2
...
Here's a picture from Excel, to show, what it should look like.
Thank you!
You don't need grid, you should use ItemsControl with WrapPanel as ItemsPanel.
<ItemsControl ItemsSource="{Binding YourList}" ItemTemplate="{StaticResource YourItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<DataTemplate x:Key="YourItemTemplate">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Email}"/>
<Button Content="Remove item" Command="{Binding RemoveCommand}"/>
</StackPanel>
</DataTemplate>
I know this question has been asked and answered several times already but I still don't get it. I seem to be missing a piece of understanding.
I have a TabControl bound to an observable list of viewmodels. The viewmodels can be of different types, derived from the same base type, of course. When a viewmodel is added to the list I want the tabcontrol adds a new tabpage based on the type of the view model.
I do not understand how to set up the ContentTemplate of the TabControl to pick the right view based on the type of the view model.
A Basic example can be found here, but I do not get it up and running with dynamic views:
How to bind items of a TabControl to an observable collection in wpf?
Thanks! Johannes
Ok, i will modify the sample code in the answer you linked:
<Window.Resources>
<DataTemplate x:Key="templateForTheHeader" DataType="{x:Type vm:BaseViewModel}">
<TextBlock Text="{Binding CommonPropertyToDisplayInTheHeader}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ViewModel1}">
<TextBlock Text="{Binding PropertyInVM1}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ViewModel2}">
<TextBlock Text="{Binding PropertyInVM2}"/>
</DataTemplate>
</Window.Resources>
...
<TabControl ItemsSource="{Binding YourCollection}"
ItemTemplate="{StaticResource templateForTheHeader}">
</TabControl>
The header displays some property in the base VM class.
And the important thing, i removed the x:key of the other DataTemplates, this will make it being applied to every instance of the defined DataType found in the Window (ContentTemplate in the TabControl not needed).
YourCollection is a mix of objects, each of it will get its template applied based on its type if a DataTemplate with matching DataType exists. Simple uh?
This is a basic question but I'm just coming back to WPF after a long break and can't remember how to do this. I've tried looking around but can't find exactly the answer I'm looking for.
I have a Listbox control that I want to bind to a List<TradeViewModel> collection. I want the ListBoxItems to pick up that the items are of type TradeViewModel and based on that, to use a custom data template including a checkbox that is bound to TradeViewModel.IsChecked and for the text of the row to be TradeViewModel.TradeId.
I have created the ViewModel class with exposed dependency properties and INotifyPropertyChanged but it's how to hook up the XAML and data templates that I can't quite remember how to do.
Can someone please help me with a quick example?
Thanks!
<ListBox ItemsSource="{Binding YourList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid> <!-- Or whatever -->
<CheckBox IsChecked="{Binding IsChecked}"/>
<!-- Other UI Elements here -->
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have a UserControl that I have successfully been using as a header for presentations that involve a list which can be headered, using the xaml below:
<DockPanel >
<uc:ListSubjectHeader Subject="{Binding DisplayName}"
AddNewItemCommand="{Binding AddCommand}"
ImageSource="..." />
<!-- other controls -->
</DockPanel>
I would like to use this same control in another presentation where it would be the content for the header in a HeaderedContentControl, and came up with this xaml to do that:
<HeaderedContentControl Content="{Binding Path=DetailViewDepartment}" >
<HeaderedContentControl.HeaderTemplate>
<DataTemplate DataType="{x:Type vm:DepartmentSelectionViewModel}">
<uc:ListSubjectHeader Subject="{Binding DisplayName}" ... />
</DataTemplate>
</HeaderedContentControl.HeaderTemplate>
</HeaderedContentControl>
The visual elements show up the way I want them to, but data does not. I should note that I am using the same view model (vm:DepartmentSelectionViewModel) in a different control's DataTemplate in the same presentation, which I asked as a different question here. If you know the answer to this one you likely know the answer to that one too.
How can I fix this?
Cheers,
Berryl
The HeaderTemplate applies to the object in the Header property, not Content. Content uses the ContentTemplate, just like in the normal ContentControl.
I have a very flat-structured treeview with only two levels of items - the main ones, and one level of subitems. I am using WPF MVVM and would like a way to style the two levels differently, but have no idea how.
I bind the treeview to an ObservableCollection in my ViewModel, and each element has one more ObservableCollection for the next level.
Any help?
This can be accomplished via DATABINDING and using DATATEMPLATES.
You would design two DataTemplates. 1 as a Hierarchical DataTemplate and the other as a standard version for your lower level (this is since you only utilize 2 levels)
Then set the ItemTemplate of your HierarchicalDataTemplate to the regular DataTemplate
Details can be found here: http://msdn.microsoft.com/en-us/magazine/cc700358.aspx
Code snippet from the above site:
<!-- ORDER DETAIL TEMPLATE -->
<DataTemplate x:Key="OrderDetailTemplate">
<TextBlock>
<Run>Product:</Run>
<TextBlock Text="{Binding Path=Product}" />
<Run>(</Run>
<TextBlock Text="{Binding Path=Quantity}" />
<Run>)</Run>
</TextBlock>
</DataTemplate>
<!-- ORDER TEMPLATE -->
<HierarchicalDataTemplate
x:Key="OrderTemplate"
ItemsSource="{Binding Path=OrderDetails}"
ItemTemplate="{StaticResource OrderDetailTemplate}"
>
<TextBlock Text="{Binding Path=Desc}" />
</HierarchicalDataTemplate>