I have a textbox in WPF and bind a command to its LostFocus event and do some validation in that command.
Now on the same window i have a Save button whose Key binding is Ctrl + S.
If a make some changes in textbox and then push Ctrl + S, it executes the save command without raising the Lost focus event on my textbox.
I know this is by design.
But i want to execute that piece of code before save command, i can't hardcode that code in save process, i want to execute it only when the focus is in textbox.
You need to factor the validation code out into a separate method that can be called by both the LostFocus and the Save command. Calling the LostFocus from the Save is a bad way of implementing this logic.
The new method can return a boolean to indicate whether the validation succeeded or not. There are better ways of doing validation that don't rely on binding a LostFocus command, check my blog post Taking data binding, validation and MVVM to the next level - part 1 for more ideas.
Related
I have a textbox inside a TabControl. The textbox binding has UpdateSourceTrigger=LostFocus. The textbox uses Attribute based validation from the data model. This validation is working correctly.
In the TabControl.SelectedItemChanged event I call modelObject.Validate() and prevent the switch to a different tab if an error occurs.
The problem I have is that the order of execution is backwards. The validate call occurs before the property setter. In the case of an invalid field I am able to switch away from the tab even though an error has been detected.
How do I get the order or these events ordered properly?
Is there a way to cancel TabControl.Items.CurrentChanging?
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d8ac2677-b760-4388-a797-b39db84a7e0f/how-to-cancel-tabcontrolselectionchanged?forum=wpf
Seems to work by subscribing the CurrentChanging event. Can cancel tab changing action by setting the CurrentChangingEventArgs Cancel to true
WPF is definitely incorrect here. However, the fix is really simple.
In the SelectionChanged event handler call {whatever your tabcontrol is called}.Focus(). This immediately forces the lostfocus event for the textbox (which forces the setter to be fired) and solves the problem.
I am using a textbox in winform (c#) and using the text to make consults in a database.
But I need constantly consult the text of the textbox every time that text changes. So for these, I use the KeyUp. But this event is too slow.
Is any event that just fires when the textbox editing has been finished ?. I consider for finish 2 conditions
The control lost focus.
The control has 200ms without keypress
You could use the LostFocus event, to capture when the user clicks on a control outside the textbox.
Otherwise, you'll need to choose from one of the existing events. (Listed here)
Come to think of it, you will likely have to capture multiple events. DragDrop if someone copies/pastes, for example...
You mean something like this?
Control.LostFocus
Provided that you consider finished as being when they click off the textbox.
Even if I associate the button with a class derived from ICommand, I am still left with figuring out how the button should trigger the CanExecute method and refresh its enabled state. I do know about the CanExecuteChanged event for which a button with an associated command registers, but see the following paragraph for why this is troublesome.
On a plain old dialog consisting of some 10-15 controls, it seems haphazard to have to process every change notification for every single one of those controls, triggering the CanExecuteChanged event on the button's command, causing the button's enabled state to be affected by the CanExecute method's return value. Even stating what needs to be done in the last sentence was quite cumbersome.
There must be a better way of coding a WPF dialog, so that the confirmation button (e.g., OK) is grayed out until all controls have valid information and is enabled at that point in time (i.e., when all controls are properly filled in). Sample code, ideas and pointers to articles would be immensely appreciated.
Thanks
I don't see anything haphazard here. Since your condition is "all controls have valid information", this can occur after any control is edited, and therefore you need to listen to change notifications from all controls.
On a plain old dialog consisting of some 10-15 controls, it seems
haphazard to have to process every change notification for every
single one of those controls,
I don't think so. Every Textbox, checkbox changed event is handled by the same handler, say SetState(), which calculates the overall state of the dialog. Every time a control is edited, the entire state is recalculated.
until all controls have valid information
Then that object would have a boolean property EnableOKButton, let's say, which is set according to the updated state. Then that property is bound to the button's Enabled property so it automagically changes - without dealing with extraneous events.
In windows forms, when I tab out of a text box, the bound data source value is updated. I'd like to capture the events right before and right after the data source changed. I think the OnLeave event is what I want for the before event. In the debugger, I'm not seeing the data source value changed. But, what event can I key off of for the after event?
I don't think there is an event that does exactly what you're asking, the closest I think you're going to get is to use the DataBindings and find your specific Binding and the you can catch the Parse event. But I believe this event fires before the data is pushed back to the source, so it's not much better than the LostFocus event.
The default event for TextBox DataBindings is DataSourceUpdateMode.OnValidation. When you tab out of the TextBox, the following events will fire:
Leave
Validating
(data source gets updated)
Validated
The Validating event has a CancelEventArgs parameter that allows you to cancel the leave attempt for the TextBox (the focus will remain in the TextBox).
If you use DataSourceUpdateMode.OnPropertyChanged, it will update the data source with every keystroke or text change.
I have a TreeView bound to an XmlDataProvider. The name of a node is reflected in the tree and the data of the node is in a TextBox. When you click on a tree item, it loads the data in the TextBox.
What is the best way to determine when a user has changed the source data referenced by the XmlDataProvider.
I would like to put an asterisk (*) next to the filename to say that the document is dirty and should be saved. This will also determine whether the user is reminded to save when the exit the program.
Unfortunately, when the TextBox is updated by the two-way binding, it fires the TextChanged event.
Is there a way to determine that the TextChanged event was fired by the two-way binding code instead of by the user?
I also tried to use the DataChanged event on the XmlDataProvider but this event only appears to fire when initially loading the data.
I suppose that I could trap the key presses in the TextBox directly but it seems messy.
[Edit] It appears that PreviewTextInput seems to work for this purpose...Is there a better way?
PreviewTextInput did the trick.