[英]How can I place an image inside a DataGridCell when a validation error occurs?
[英]wpf validation - how can I get this code to trigger a validation as typing occurs (cf when leaving the field)
如何进行键入时,我如何获得此代码来触发验证(cf离开字段时)。 下面的代码在验证方面工作正常,但是在离开字段之前它不起作用(而不是在您键入时)。
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>
谢谢
尝试
<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>
请注意绑定中的UpdateSourceTrigger = PropertyChanged 。
UPDATE
正如下面所述的blindmeis,我把UpdateSourceTrigger放在错误的Binding框中..我的错误。 它应该与TextBox.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>
</TextBox>
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.