WPF, Binding a combobox and textbox to each other AND data source - wpf

I have a two column combobox, and a textbox, bound to xml data.
The textbox shows the equivalent of the comboboxes second column of the currently selected item.
I've bound the datacontext of the textbox to the SelectedItem in the combobox, which then updates if you select a row in the combobox. Now, I'd it so that if you type something into the textbox that corresponds to a value in the 2nd column of the combobox, it selects that row.
I realise this is slightly circular.
I've managed it before in winforms, by effectively suspending events when the CombobBox OnSelectedItemChanged fires and updates the textbox or OnTextChange fires and updates selectedItem.
The idea is that the user can either select an option from the combo, or if they know a short code (in this case, country ISO), they can just type it in and immediately see the appropriate country selected in the combobox.
Is it somehow possible to bind the selectedItem in the combobox to the textBox in addition to the underlying data (and indeed does that idea make any sense?), or possibly do some sort of two-way-binding between these elements?
I'm hoping there's a simpler solution than dependencyproperties-- ideally something purely in xaml, but appreciate as I'm new at WPF, I've no idea if this is even possible.
Thanks!
Mike

If you've got a ViewModel you can two-way bind each to the same property of that, and this is probably the best way to do it.

Related

Setting Focus to a Control after the Loading of the DataGrid

I have a UserControl in WPF and it contains a variety of controls in it... The most important being the DataGrid Control. The DataGrid control is bound to an Observable Collection list. This list filled with different items based on couple of filters selected by the user. Now, once the DataGrid displays all the data. I want to set the Focus on the first (Filters) combobox on my usercontrol. Is there any way to know when the DataGrid has completely loaded??? Is there any DataTrigger I could set that i could trigger to inform me that Grid was refreshed with new list and I can set the ComboBox to focus. Basically i'm not able to set the focus to the first control on my UserControl when the data in the DataGrid has been repopulated... Please let me know if anyone knows how to resolve this issue!!
Thanks in advance.
I hate to work in code behind but sometimes that is only way. This can be done in two ways according to me:
Use EventAggregator to raise the event from your ViewModel once you are done with your filtering logic in it. Listen to this event in view code behind and in the handler do firstCombo.Focus()
Second is bit dirty, So there must be the button or the last control after which you apply filter on your listview. In the buttonClickHandler/or any event of the last control (like selectionchange) directly do firstCombo.Focus(). In this case the moment you will press your button focus will move to combo.
Thanks

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).

how to raise an event when a value in a cell of a wpf datagrid changes using MVVM?

I need help with a wpf datagrid using the MVVM design pattern.
I have a datagid that is bound to an observablecollection. The first column in the grid contains decimal values that cannot be edited. The second column contains a textbox into which a decimal value must be entered. The third column must display the difference between the value in the first column and the value in the second column AS IT IS ENTERED. I was hoping that handling the observablecollection's Collectionchanged event will allow met to determine when a field of one of the items in the collection has changed, but that does not seem to work.
I've also tried handling the PropertyChanged event of the grid's selected item, but that's not working either.
Can someone please indicate to me how to raise an event in the viewmodel whenever 'n value in a textbox, in a datagrid DataGridTemplateColumn, is changed? And then how do I set the calculated value in the third column's corresponding row?
You should try to tackle it from the other end (i..e from the ViewModel).
Your item(calling it CollectionItem) in the ObservableCollection should implement INotifyPropertyChanged.
You should tweak your grid so that data change is registered/commited as you change them (not on focus out/move)
and then in your CollectionItem should try to refresh the value based on the value change of input. let me know if you want more detail

How to Get the SelectedValue of Combox inside an DataGridTemplateColumn in wpf Datagrid?

I have am DataGridTemplate Column with an Combox inside it.I want to get the selected value of an combox in an particular row when they cllick on the save button oputside the DataGrid.
That is not a very good approach since you cannot directly access rows or the controls that visualize them from the selection. I'd suggest you use events to signal that changes should be committed or something similar to that.

Append Items to Databound ItemsControl in 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.

Resources