开发者

wpf validation - how can I get this code to trigger a validation as typing occurs (cf when leaving the field)

开发者 https://www.devze.com 2023-01-15 07:49 出处:网络
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 yo

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


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>


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>

0

精彩评论

暂无评论...
验证码 换一张
取 消