WPF TabControl rebinding every time - wpf

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.

Related

Should a ViewModel control focus in the View

I have an ItemsControl binding to a collection in the ViewModel. As a result of user input, a new item gets added to the collection and this gets displayed on the View.
The item is also a View-ViewModel pair, the View contains a TextBox that I would like to receive focus immediately after being added to the collection.
How do I set the focus to a TextBox without referencing the View from within the ViewModel? Are attached properties the way to go here?
well you can do this by creating a behaviour..
have a look here to get an idea of behaviours controlling focus

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

ComboBox loses state when clicked away

I have a ComboBox with an ItemsSource bound to an ObservableCollection<T>. This ComboBox is part of a detail view which updates dynamically based on a selected master item.
The issue I'm having is that the ComboBox SelectedItem does not persist after the master item is changed. If I click away and then back to the item the selection in the ComboBox is cleared.
Any ideas why this is happening?
Maybe there are better ways to do this but this approach seemed to work:
The ObservableCollection<T> had to be a property in the the source object I am binding to. Previously I was using a {Binding RelativeSource={RelativeSource AncestorType=VisualParent} expression that was getting the source property from a parent in the visual tree.
If anyone has insight into why this was the case feel free to leave comment on this for my own (and others) learning.

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

Resources