I have a form that contains two TextBoxes: MultiStageCountValue, which signifies the number of features to be selected, and MultistagePercentageValue, which signifies the percentage of features to be selected.
If the user types in the number of features, the code-behind will calculate the percentage of features, updating the MultistagePercentageValue textbox. If the user types in a value for the percentage of features, the code-behind will update the value for the MultiStageCountValue textbox. It also recalculates the actual percentage, since it only allows for integers to be typed into either field (for example, 33% of 76 features is 25 features, which is actually 32.89% of the features).
This is the code for each textbox in the xaml file. The value from the MultiStageCountValue textbox is used for a query in the ViewModel code, using the Binding variable MultistageCount.
<TextBox x:Name="MultistageCountValue"
Text="{Binding MultistageCount}"
PreviewTextInput="NumericTextBox_PreviewTextInput"
TextChanged="MultistageCountValue_TextChanged"/>
<TextBox x:Name="MultistagePercentageValue"
Text="{Binding MultistagePercentage}"
TextChanged="MultistageCountValue_TextChanged"
PreviewTextInput="NumericTextBox_PreviewTextInput"
LostFocus="MultistagePercentageValue_LostFocus"/>
The same code for the TextChanged event is used for both textboxes, which does some validation and updates the MultistagePercentageValue textbox when the MultiStageCountValue textbox value changes.
private void MultistageCountValue_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textbox = sender as TextBox;
if (textbox.Name == "MultistageCountValue")
{
MultistagePercentageValue.Text = ((double)int.Parse(textbox.Text) / NumberofFeatures * 100).ToString("F");
}
// other validation code
}
This is the code for when the MultistagePercentageValue textbox loses focus so that the MultiStageCountValue textbox only gets updated when the user is done typing in the percentage
private void MultistagePercentageValue_LostFocus(object sender, RoutedEventArgs e)
{
TextBox textbox = sender as TextBox;
MultistageCountValue.Text = (float.Parse(textbox.Text) / 100 * NumberofFeatures).ToString("F0");
}
In my testing, the MultistageCount value gets updated correctly if I type in the MultiStageCountValue textbox or when I type in the MultistagePercentageValue TextBox and then click in the MultiStageCountValue textbox.
However, if I type in the MultistagePercentageValue textbox and tab out of it, while the MultiStageCountValue textbox will get updated, the MultistageCount value does not get updated and the query uses the previous value.
Why doesn't MultistageCount get updated when the focus is lost by tabbing out of the textbox versus clicking outside it?
I had to specify the UpdateSourceTrigger property. The Text property defaults to LostFocus, so when I changed to it PropertyChanged, it works as expected.
<TextBox x:Name="MultistageCountValue"
Text="{Binding MultistageCount, UpdateSourceTrigger=PropertyChanged}"
PreviewTextInput="NumericTextBox_PreviewTextInput"
TextChanged="MultistageCountValue_TextChanged"/>