验证错误时禁用按钮 [英] Disabling button on Validation error
问题描述
我有几个带有自定义验证器的文本框:
(我不介意是否将错误"数据发送回对象(属性是字符串),如果出现错误,我只需要阻止按钮的功能,因此如果绑定不是正确的位置请告诉我的验证类型.我只是喜欢我可以使用的 Validation.ErrorTemplate 支持)
I have couple of textboxes with custom validators:
(I don't mind if "wrong" data is sent back to object (the property is string), I just need to prevent the functionality of a button if there is an error, so if the binding is not the right place for that kind of validation please tell. I just like the Validation.ErrorTemplate support that i can use)
<ControlTemplate x:Key="validator" >
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="12pt">!</TextBlock>
<Border BorderBrush="Red" BorderThickness="1.0">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
<TextBox Height="23" Width="150" TextWrapping="Wrap"
Validation.ErrorTemplate="{StaticResource validator}">
<TextBox.Text>
<Binding Path="StringProperty" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<local:NumbersOnly/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
如果出现任何验证错误,我如何禁用特定按钮?
How can I disable specific button if any of the validation error is raised?
<Button Content="DO Work" Height="57" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="234" Click="button1_Click" />
推荐答案
您可以在 Button
的 Style.Triggers
中使用 MultiDataTrigger
属性.假设我们有一个名为txtName"的 TextBox
.我们必须在 TextBox
验证错误时禁用按钮btnSave".
You can use MultiDataTrigger
property in Style.Triggers
of Button
. Let's assume that we have a TextBox
named "txtName". We have to disable button "btnSave" on the validation error of TextBox
.
您可以这样做:
<Button Content="Save"
Grid.Column="1"
Grid.Row="3"
HorizontalAlignment="Right"
Height="23"
Name="btnSave"
Width="75"
IsDefault="True"
Command="{Binding SaveProtocolCommand}"
Margin="3">
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=(Validation.HasError), ElementName=txtName}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="True"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
希望对您有所帮助.
这篇关于验证错误时禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!