Skip to content

Commit 93d4713

Browse files
committed
Fix validation not persisting when switching tabs.
1 parent 6d13fe1 commit 93d4713

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

CommanderMonitor/ConfigurationWindow.xaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,18 @@
5353
VerticalAlignment="Bottom"
5454
VerticalContentAlignment="Bottom"
5555
TextChanged="PhoneticName_TextChanged"
56+
Loaded="PhoneticNameTextBox_Loaded"
5657
Margin="0, 5"
5758
Grid.Row="1"
5859
Grid.Column="1"
5960
Grid.ColumnSpan ="3"
6061
Height="20" >
6162
<TextBox.Text>
62-
<Binding Path="PhoneticName" Mode="TwoWay" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged" TargetNullValue="">
63+
<Binding Path="PhoneticName"
64+
Mode="TwoWay"
65+
NotifyOnTargetUpdated="True"
66+
UpdateSourceTrigger="PropertyChanged"
67+
TargetNullValue="" >
6368
<Binding.ValidationRules>
6469
<utility:ValidIPARule />
6570
</Binding.ValidationRules>

CommanderMonitor/ConfigurationWindow.xaml.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,42 @@ public ConfigurationWindow ()
2424
SquadronSystemComboBox.Text = config.squadronSystemName;
2525
}
2626

27+
private void PhoneticNameTextBox_Loaded ( object sender, RoutedEventArgs e )
28+
{
29+
if ( sender is TextBox textBox )
30+
{
31+
// Force validation to reapply when the control is loaded
32+
var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
33+
if ( bindingExpression != null )
34+
{
35+
bindingExpression.UpdateSource();
36+
// Explicitly refresh the validation state
37+
var errors = Validation.GetErrors(textBox);
38+
if ( errors.Count > 0 )
39+
{
40+
Validation.ClearInvalid( bindingExpression );
41+
Validation.MarkInvalid( bindingExpression, errors[ 0 ] );
42+
}
43+
else
44+
{
45+
Validation.ClearInvalid( bindingExpression );
46+
}
47+
// Force WPF to refresh the visual state
48+
textBox.InvalidateVisual();
49+
textBox.UpdateLayout();
50+
}
51+
}
52+
}
53+
2754
private void PhoneticName_TextChanged ( object sender, TextChangedEventArgs e )
2855
{
29-
// Replace any spaces, maintaining the original caret position
30-
var caretIndex = phoneticNameTextBox.CaretIndex;
31-
phoneticNameTextBox.Text = phoneticNameTextBox.Text.Replace( " ", "ˈ" );
32-
phoneticNameTextBox.CaretIndex = Math.Max( caretIndex, phoneticNameTextBox.Text.Length );
56+
if ( sender is TextBox textBox )
57+
{
58+
// Replace any spaces, maintaining the original caret position
59+
var caretIndex = textBox.CaretIndex;
60+
textBox.Text = textBox.Text.Replace( " ", "ˈ" );
61+
textBox.CaretIndex = Math.Max( caretIndex, textBox.Text.Length );
62+
}
3363
}
3464

3565
private void phoneticNameTestButtonClicked ( object sender, RoutedEventArgs e )

0 commit comments

Comments
 (0)