I have a treeview with several items in it. How do I force the treeView to select a specific item ? Everytime I try to use any of the "Selected..." property, I get the error that it is read-only and cannot be set either in code or in XAML.
Regards,
You need to create a Selected property on your tree item's ViewModel, and bind to it.
Here is an article that explains how to do it.
Edit. Actually that uses a different way. The article I was thinking of was this one
Related
i want to immitate the behavior of multiselection of items (eg. in a list view) and their appearence in a PropertyGrid. So, obly the same properties are shown and values set in the grid are set on all multiselected properties.
I have created a listview with some datatemplates and have in another listview some data(with databinding). Now i want wo have a multiselection on the listview with the data and have the properties shown in the another listview like in a propertygrid.(explanation: The ProprtyGrid is still not available in WPF and if has not the flexibility of showing data the way i need it)
So, how do i need to prepare my data to be shown in the list propertygrid-multiselection-style? Is that even possible???
Greets,
Jürgen
I'm not sure if this question is still active, or what not. But I have found a solution that works for me - and I need somewhere to share it.
The first issue you are going to have solve is selecting the items, ie. multi-select. See my tip here that explains how to do that.
Next, I guess its basically binding to listview's selectedItem property and how you want to update your propertyGrid based on changes to the selectedItem prop.
Hope this helps.
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
I am data templating a tab control. Each time I select a tab, the binding of the contents get applied. So for eg, if I have a tree view expanded in tab1 and going to tab2 and coming back has this collapsed. The property binded to ItemsSource is invoked each time I flip the tabs.
learned its all because the visual tree gets recreated again and again as selectd item changes.
Now the qn is, any easy way to fix this. Wil be really helpful.
One way to go is to have an 'IsExpanded' property for a TreeViewItemViewModel and bind to it. Basically your viewmodel will hold the expanded/collapsed state information in your viewmodel.
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.
I've got a Combo Box that is databound to an ObservableCollection of items. I would like to have a default selected item that is (None) that would set the value of the property I've bound to "SelectedValue" to null.
I think there ought to be a way to achieve this with some combination of Style/DataTemplate/TemplateSelector. I'm trying to design this with MVVM in mind, so I'd like something that doesn't use codebehind and is as reusable as possible. I'd also like the benefits of the ObservableCollection (updating the collection causing the control to rebind) to remain intact.
Bonus part B:
I would like to also be able to append an extra visual element to the bottom of an ItemsControl as well. I was thinking it would be easy to change the DataTemplate if I knew how to trigger it on the last item of a collection. Willing to entertain other options here.
The simplest way I've found to do this is to insert a "special" value into the underlying collection, and display the "(None)" text when it's selected. Obviously then you need to run your binding through a converter to take this value into account and return null when it's selected. (See this question of mine which was a result of me trying to add an actual null value to a ComboBox's underlying collection.)
Having said that, it might actually be possible to do what you want with the CompositeCollection class. You could make a separate collection (with only one item - your Null item) and bind your ComboBox to both it and your original collection through the CompositeCollection.