Reputation: 34818
how can I get this code to trigger a validation as typing occurs (cf when leaving the field). The code below works OK in terms of validation, however it does not work until leaving the field (not as you type).
XAML
<Grid.Resources>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter
Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
. . .
<TextBox IsEnabled="{Binding ElementName=ProxyModeRadioButton, Path=IsChecked}"
Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150" >
<TextBox.Text>
<Binding Path="Proxy" >
<Binding.ValidationRules>
<local:SpecialCharactersRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
thanks
Upvotes: 3
Views: 4868
Reputation: 22445
Dave is nearly right but i think you want your validation occur when your TEXT property change, so you have to add the UpdateSourceTrigger=PropertyChanged
to the TEXT binding
<TextBox IsEnabled="{Binding ElementName=ProxyModeRadioButton, Path=IsChecked}"
Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150">
<TextBox.Text>
<Binding Path="Proxy" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:SpecialCharactersRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
Upvotes: 2
Reputation: 15016
try
<TextBox IsEnabled="{Binding ElementName=ProxyModeRadioButton, UpdateSourceTrigger=PropertyChanged, Path=IsChecked}" Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150" >
<TextBox.Text>
<Binding Path="Proxy" >
<Binding.ValidationRules>
<local:SpecialCharactersRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Note the UpdateSourceTrigger=PropertyChanged in the binding.
UPDATE
As blindmeis stated below, I put the UpdateSourceTrigger in the wrong Binding box.. my mistake. It should go with the TextBox.Text. Sorry about that...
<TextBox IsEnabled="{Binding ElementName=ProxyModeRadioButton, Path=IsChecked}" Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150" >
<TextBox.Text>
<Binding Path="Proxy" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<local:SpecialCharactersRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Upvotes: 5