WPF - Temporarily Stop Data Binding (databinding) - wpf

I have a textbox that is bound (oneway) to a datatable that updates a couple of time a second.
So the textbox value continually reflects changes in datatable.
When I enter the textbox to manually set a value, the binding causes the value to continually be overwritten. How do I stop this?
When I have entered a value (textbox lost focus) I want the textbox to return to display the bound value and not the value I have just entered manually.

I had the same problem and I solve it by
With the BindingNavigator, I set it to null
bdNavProduct.BindingSource =null;
With other textbox control, datagridview, I clear the DataBinding
txtProductID.DataBindings.Clear();
txtProductName.DataBindings.Clear();
txtQuantity.DataBindings.Clear();
txtUnitPrice.DataBindings.Clear();
dgvProduct.DataBindings.Clear();
Tony

You could attach to the text input event and cancel the binding and then reapply it on the lost focus event.
I think however you need to consider why you are displaying your bound value in a text box at all? Would it not be more appropriate to have an uneditable Textblock displaying your database information with the editable Textbox separate for the optional user input.
While what you are asking is doable using the Textbox event I mentioned, it seems like it would be confusing from a user perspective.

Related

How do I pick right TextBox to input value in it

Need help in achieving functionality for the image that I insert.
I have 4-5 textboxes and near them some type of calculator.
All TextBoxes are bound to properties in ViewModel.
All calculator buttons use Command="{Binding AddNumberCommand}" CommandParameter="9" for passing value to a property.
Problem is that I don't know how to pick the right TextBox for inputting value.
Thanks for the advice.
There are a couple ways I could think of to do this. Note that whichever way you do it, I'm pretty sure you'll have to set Focusable to false on all your Buttons, otherwise they'll steal the focus away from the TextBoxes when clicked.
Keyboard Focus Events
You could declare a field where you track the currently focused TextBox (e.g. private TextBox focusedBox;). And for each TextBox, you could handle the GotKeyboardFocus and LostKeyboardFocus events. You could use those events to track which TextBox currently has focus, and when you want to add a number you just add it to focusedBox.
You would also have to check if focusedBox is null, in case none of your TextBoxes are currently focused.
This method would ensure that the calculator buttons only ever effect those TextBoxes you want them to.
Keyboard.FocusedElement
Alternatively, you could just use the Keyboard.FocusedElement static property. This will point to whatever element currently has keyboard focus. This coud be any focusable element in your application, or null if the keyboard focus is outside your application.
You could attempt to cast Keyboard.FocusedElement to TextBox, and if the cast succeeds, append the text.
This would require much less code, but also allows the buttons to add text to any TextBox in your application. This may or may not be a problem, and could be prevented by setting some unique property on the TextBoxes you want to allow input on and checking for it, or keeping a list of said TextBoxes.

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

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

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.

Force Binding Update Silverlight

How can I force my objects DataContext bindings to update? I'm using an event on a grid, and binding updates are not being processed before my event fires.
Any cheap tricks to get around this?
In the end I can always do things the old manual way of getting the values from my textboxes and updating my object, but it'd be nice to have binding do it for me.
UPDATE
My grid contains two textboxes. If a user clicks on the grid (MouseButtonUp event) then I save the changes. But in my MouseButtonUp event handler, the datacontext is not up to date yet. I'd imagine it's because a text box only updates when focus is lost.
You can force an update of the source a binding by calling the UpdateSource() methond on on the binding. Try adding it to the MouseButtonUp event handler just before saving.
Like so:
BindingExpression binding = FirstTextBox.GetBindingExpression(TextBox.TextProperty)
if (null != binding) binding.UpdateSource();
You can find more information about this on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingexpression.updatesource(VS.95).aspx
Alternativly you can catch TextInput event (it will fire when user presses enter inside text box)
and then change focus to second textBox. loosing focus will update binded value.

Wpf combobox text property binding problem

I am trying to bind to a combobox text with the IsEditable property set to true. I have a property in my viewmodel which is bound to the text.
I want to validate on the text being typed in the text of the combobox, and restrict some values that the user is typing in. So some will be allowed, and some not, and these need to set the combobox back to its old value.
I do this in the view model and I have tried setting my text property in my view model explicity to the old value or just ignoring the change and raising that the property has been changed, but for the life of me it will not refresh the text back to the old value.
Is this because the combobox is editable, and it has the text caret and focus somewhere in the text of the combobox.
Basically, I want it to refresh back to the previous text when I restrict some typing in the combobox during in editing. Anyone have any ideas to reset the text back to its old value through the ViewModel. Thanks in advance!
Thanks for your replies. But I could never get it to work instead, I made my own UserControl which comprises a textbox overlayed over a combobox, and manipulate those two controls to meet my needs. A long way to go to solve a simple problem, but it works in the end.
Is the viewmodel property you are binding to created as a DependencyProperty? This is probably the problem you are facing Two-way binding in WPF
If you don't want to create a Dependency property then you need to implement INotifyProperty changed and manually force the update in the Property changed event.
I think this is because of a 'bug' in WPF not refreshing the UI if you change the value of a property in the setter. You can workaround it by implementing an IdentityConverter that force the UI to refresh as per this arcticle.

Resources