Reputation: 2085
I have a Textbox and on the LostFocus
event of it, I have a function called SaveWeight()
to save the value based on few conditions. I need to programatically trigger that function.
For example, I have a OnClearButClicked
function in which the the value in the textbox is cleared. So after it is cleared, I need to call the SaveWeight
function. But since it is a LostFocus event I need to manually set the focus of the textbox to Unfocused
which might hopefully trigger the SaveWeight()
.
private void OnClearButClicked(object sender, RoutedEventArgs e) {
weightTBox.Text = "";
weightTBox.Focus(Windows.UI.Xaml.FocusState.Unfocused);
}
But the weightTBox.Focus(Windows.UI.Xaml.FocusState.Unfocused);
throws an ArgumentException
. So is my approach wrong? Can I set the FocusState
for an element and then trigger the event. Or some other thing I'm missing?
Upvotes: 1
Views: 3979
Reputation: 29963
Why not just call the method, passing in null as the arguments?
SaveWeight(null, null);
It seems to me, setting focus in the UI is a huge violation of the separation of concerns in your code base.
Upvotes: 1