I have a Listview in my WPF windows and there I have a binding to a property List<User> users;
I also have a edit/add form beside the listview where I want to edit the user and click on the save button to save the user. I have the bindingContext of the groupbox of the form set to currentUser which is set on OnSelectionChange event of the ListView. Everything(bindings) work fine but my problem is:
When I change the text of the textbox which are bound to currentUser and leave the textbox, it is automatically updated in the listView. I first want to click on 'Save User' before the ListView is updated. How can I achieve this?
Thank you.
Option1:
You could set UpdateSourceTrigger of your textbox (on your update form) to Explicte,
and only call UpdateSource method, when clicking Save.
Option2:
You could also set Listview's each column's BindingMode to OneTime, and update/refresh it's DataContext after Save. (ie. by setting View's DataContext to null and then set it back.)
Related
I bound Selected Item of ListView with my viewmodel property SelectedLayout and I have changed SelectedLayout from code behind. The Selected Item is changing properly But focus is not coming to the selected item. It is clear when I press enter. Focus is at button where it was before setting SelectedLayout . How can I get Focus to the selected Item in windows 10 UWP?
Just changing the value of bound property does not automatically change the focus to the corresponding element. It is done by design, as in many cases you do not need to change focus, just to update the value of the control. So what you need to do is to implement the logic of changing focus in your MVVM code.
To do that, the good practice would be to use Attached Property as outlined here: Set focus on textbox in WPF from view model (C#). In this manner, you can bind the IsFocused attached property of your controls to your ViewModel, and then implement any focusing logic in the ViewModel.
I have a class that implements INotifyPropertyChanged. I use a binding source to bind the class to a form's controls. I'm using WinForms.
When I enter some text in a textbox and click on the Save button (I don't leave the textbox), the property bound to the textbox doesn't get updated.
Is this normal behavior? And if yes, is there a way to accept all changes?
I have a C# WPF MVVM application that works fine.
The only problem is when I modify a textbox and click on the menu. If I do that without clicking on another control, the view->viewmodel event is never fired because the textbox hasn't lost focus. Correct me if I am wrong, but I think the RaisePropertyChanged is only fired on LostFocus (or OnBlur, or any similar event).
So, clicking on the menu save button right after editing the textbox causes the viewmodel to save the data using old values.
So, resuming:
This sequence works fine:
Edit the text box
Click on another control
RaisePropertyChanged is fired, the viewmodel is updated
Click on save button on the menu
Data Saved with correct values
This sequence gives me an error:
Edit the text box
Click on save button on the menu
Data Saved with correct values
How to solve this?
This is a common gotcha with TextBoxes in both WPF and WinForms. You can get around this by instructing the binding system to update the VM with every change to the TextBox instead of when it loses focus. To do this, set the UpdateSourceTrigger of the binding to PropertyChanged. This will write back to the VM any time the TextBox raises the PropertyChanged event for its Text property.
<TextBox Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
For the TextBox.Text dependency property, its default UpdateSourceTrigger is LostFocus (ie, your view model property gets updated when the control loses focus). To make the property update immediately whenever text is entered, set UpdateSourceTrigger=PropertyChanged. (See the link above for more info -- it actually covers your example specifically.)
I have a Listview that uses databind. I set the DataSource property to a binding source. All works fine. The problem is that I need to have a column that is not databinded and contains only buttons that have the same handler for click event. To accomplish this I tried to add a subitem that is a button for each ListViewItem after InitializeComponent but doesn't work, nothing is displayed. Also I set the list view column type to Control.
If I add elements to ListView and isn't databinded that the buttons appear.
So it will be a great help for me to know if buttons could be displayed in column that is not databinded when the listview uses databinding for rest of columns.
Thanks!
The best thing to do here (assuming you mean ListBox), is to have a single button above or below the listbox, that uses the ListBox.SelectedItem property to investigate the selected item and do something with it.
Is there a property or setting to force a bound control in Winforms to update the object it is bound to when the input value, either in a textbox or whatever, actually changes?
And not after the control is tabbed out of.
Assuming you have already bound the textbox to something:
Select the textbox in the designer
Click property "databindings"
Click property "advanced"
In this dialog look for the "datasource update mode"
Select onpropery changed instead of onvalidate
Now the datasource gets updated on every change of the text property of the textbox.