Wpf validation - how can I get this code to initiate validation when typing (cf when exiting a field)

how can I make this code initiate a check when it types (cf when exiting a field). The code below works fine from the point of view of verification, however, it does not work until you exit the field (and 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>

thank

+3
source share
2 answers

to 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 indicated below blindly, I put the UpdateSourceTrigger in the wrong binding block .. my mistake. It should go with TextBox.Text. Sorry for this...

<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>
+5
source

, , , TEXT, UpdateSourceTrigger=PropertyChanged TEXT

<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>

+2

Source: https://habr.com/ru/post/1764103/


All Articles