[英]wpf validation - how can I get this code to trigger a validation as typing occurs (cf when leaving the field)
how can I get this code to trigger a validation as typing occurs (cf when leaving the field). 如何进行键入时,我如何获得此代码来触发验证(cf离开字段时)。 The code below works OK in terms of validation, however it does not work until leaving the field (not as you type). 下面的代码在验证方面工作正常,但是在离开字段之前它不起作用(而不是在您键入时)。
XAML 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. 请注意绑定中的UpdateSourceTrigger = PropertyChanged 。
UPDATE UPDATE
As blindmeis stated below, I put the UpdateSourceTrigger in the wrong Binding box.. my mistake. 正如下面所述的blindmeis,我把UpdateSourceTrigger放在错误的Binding框中..我的错误。 It should go with the TextBox.Text. 它应该与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 Dave几乎是正确的,但我认为您希望在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.