Append Items to Databound ItemsControl in WPF - wpf

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.

Related

PropertyGrid-like multiselection in other elements(listView)

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.

Change the selected item of a treeview in WPF

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

Databinding when source changes from null to object array

I have an MMVM project and with one of my View/ViewModels have an issue with databinding.
The view consists of a few combo boxes, and the user is required to select a value from each combox box. After a value is selected, I need to populate the next combo box.
How can I ensure the databinding works correctly in WPF, since only the the first combo box's value is populated on load. The others are all null, and seem to break all databinding to those attached controls. I have INotifyPropertyChanged implemented on my ViewModel, but I think things are getting lost due to the initial null values.
I would use ObservableCollections for each ItemsSource.
Create a new instance of each collection before binding them to your ComboBoxes. Leave them empty, but instantiated so that they are not null.
Then, when you are affecting the content of each combobox, directly modify each collection respectively rather than rebinding the ItemsSource (though I assume you are not doing that since you are using MVVM).

What are the best practices with Databinding and ObservableCollections in WPF?

I am using LinqToSql as my datasource, let’s say I query a list of programmers from a table and then loop through that data populating an ObservableCollection. (First question, is this part wrong? It just seems weird to query data into a list and then loop through it to populate another list)
Next, when I databind my ObservableCollection of programmers to a ListBox. Then I bind the selectedItem of the Listbox to a TextBox. I understand that when I select an item in the ListBox the textbox will be updated and when I make a change in the textbox, the ListBox will get updated and as a result the ObservableCollection that the listbox is bound to will also be updated.
What I don’t completely understand is what the best practice is to get the data from the OC back into my original datasource. Do I just loop through the observable collection commiting my changes by updating each record? That doesn’t seem terribly efficient.
Thanks so much for your help!
-Josh
This is purely a view-model issue. You have complete control over all the objects and properties on the data side of the fence. Sometimes you have a simple arrangement where the user can only edit a single record, databinding does all the work, and you just write it back to the database when the user clicks save.
If you have a lot more data being displayed and any one piece of it can be modified by the user, then it is probably wise for you to keep a dirty flag in the object itself that records whether any of the properties have been changed. That way, you can efficiently save back just the modified entries.
Since your objects probably support INotifyPropertyChanged and your collections are observable, you can even automatically detect and manage the dirty flag. Or it might be simpler to just set dirty to true in all of your setters.
In any case, this information can even be useful to the user. For example, a user-interface can show unsaved records in bold or with some other convention.
The ObservableCollection is not the only one collection which you could use in wpf. But it is standard collection allows wpf to automatically update ui item containers like ListBox on collection changes like addition or removal of an item. You can't do it with List.
When the user modifies a textbox in ListBoxItem the ObservableCollection is not updated cause you don't add or remove or reorder items in the collection. You change the property in one of the items.
The ListBox contains a list of ListBoxItem containers, one for each item in collection specified as an ItemsSource.
The DataContext of each ListBoxItem is set to the corresponding item stored in ObservableCollection. So, if you change the text in TextBox in code, the binding engine will change the property of that item specified for TextBox.Text in binding. Nothing to change or update for the ObservableCollection object.
In the setter of such item's property the PropertChanged event of INotifyPropertyChanged interface are usually raised. That is also the usual the place to set up a dirty flag. Then you could also commit changes immediately from there, keep some list of dirty objects or search for them on commit like:
items.Where(item => item.IsDirty)
Also there are good tools like Snoop and WPFInspector, which greatly help you to understand the wpf app visual tree and datacontexts for each element

How can I set a datatrigger to select\unselect listbox items?

This issue has been plaguing me for a while, and I just can't get it right. I have multiple listboxes each bound to different lists. In the end, I just want to be able to keep it so only one item is selected at a time across all the lists. Any ideas?
This has got to be very simple, but I have had no luck. I tried the idea of a gloabl\static DependencyObject to match against each ListBoxItem's Tag property, but have trouble getting the datatrigger to fire for each listbox when the value is updated.
For example, if my form has different ListBoxes, each with their own backing list collections and their own DataTemplates, I want to be able to select an item from one list, and have it be the only selected item. Which means I want the others to unselect. I tried binding the ListBoxItem IsSelected Property to a MultiBinding that compares the unique ListBoxItem value to a global value, but had no luck there.
As far as I understand, IsSynchronizedWithCurrentItem works for lists using the same backing collection.
ok, here's a try:
get the collectionviewsource.View of each of your lists (if you don't use these already, get the default views). In the View you can subscribe to CurrentChanged and in there for each other lists' CollectionViewSource.View.MoveCurrentToPosition(-1);

Resources