WPF and treeview: tricks with Items - wpf

I have some questions about treeview in WPF.
How can I change header of selected item?
How can I add child Items to selected item?

Use MVVM. In your HierarchicalDataTemplate, bind Header and ItemsSource to properties in your view model which implement change notification. Now any changes you make to those properties will be reflected in the TreeView.
If you're not using MVVM and data binding to populate the TreeView, you're wasting a lot of effort.

Robert is right, you should use MVVM and data binding. The tutorial that IMHO explained it best is here

Related

Binding control WPF break encapsulation

First of all, I use MVVM pattern in WPF.
I have a ViewModel which contains a grid. In order to render the grid in the view, I have to do the property of the grid public.
In this way the encapsulation of the control has been broken because if I want to use the control in other view models the grid is available for modifications.
Is there some solution about this problem?
I think Microsoft has screwed up, what is your opinion?
a viewmodel should not have a grid :) just the view. the viewmodel just have a collection for your grids itemssource.
edit: maybe what you want is some kind of usercontrol with Dependency Properties?

How to implement an editable listview in MVVM model?

I have an mvvm application...I need to have an editable listview.
I am binding my observable collection to listview.
How could I track changes of the values in listview ?...I mean if user edit an item...I need to have changed values in my observable collection.
If I use datagrid in WPFToolKit, is it easy ?
In a word, yes.
Take a look at data templates in WPF. They allow you to define how you want each item in your list (or any control) to appear and behave. So each item in your listview can one or more editable controls that are bound to each item in your collection (in this case an ObservableCollection). As you change data in the listview, the bound objects will in your collection will be updated in real time.
This is also possible with a datagrid.
Have a look at this link
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit
It is recommended you to use the Datagrid.It already provides the edit mode functionality. You can use a TemplateColumn to provide editing views.
if you have an editable Collection in your viewmodel, just take a datagrid(editable stuff built in). you can create styles or use templates so that the datagrid look the way you want.
If I use datagrid in WPFToolKit, is it easy ?
yes ;) but if you can, use the .net4 datagrid

getting selected checkbox items from listbox mvvm

I have listbox in which I am dynamically showing the pre selected checkboxes from the database. Now I want to loop through it to get the selected items/values. I am using mvvm light wpf.
kindly Suggest?
Thanks
If you are using MVVM, then you should have the 'IsChecked' value of the checkboxes bound to some property in your view model. That property should be accessible through code and you just need to loop over the items in your model collection.
More information about your specific ViewModel would be useful.

Change foreground/background color of Item in Listbox programmatically in WPF

How can i add different items with either background/foreground color different in WPF listbox?
You can can create a DataTemplete, set it to ListBox.ItemTemplate and use DataTrigger to change the display of the items.
ListBox has some very annoying behaviors that make styling it using data template difficult - in this blog post you will find the list of workarounds.
You may use an AlternationCount property, more info provided by this link
Or you can add to your objs binded to a listbox Background and Foreground properties and bind them in a DataTemplate of ListBox.ItemTemplate and change them in code however you like, properties must update themselves on every changing.
After tearing my hair out with this one (and your particular usecase might differ from mine) I found the WPF Toolkit's DataGrid cured all my needs.

How do I bind a TreeView node to a specific view in an Explorer-like UI?

I'm sure there is a simple answer to this but I can't seem to find it. Most examples for binding TreeView nodes are about using a ListView to show the node's details. In the scenario I am working on I've got a TreeView data bound to an Xml document using a simple MVVM pattern. As each node is selected in the TreeView I want to show a different UserControl for the type of XmlNode being represented.
What is the best control for hosting the different View's? (ViewBox, Panel?)
What's the best way to bind the view to the Selection in the treeview?
Thanks
In an application I'm working on I have a similar scenario. You should use UserControl to host your views.
In the TreeView each item controls its own IsSelected state. Have you tried binding to the TreeView's SelectedItem property? What I actually did was create an attached property for the mouse double click, and bound it to a Command. I defined this binding in my HierarchicalDataTemplate.

Resources