验证错误时禁用按钮

时间:2011-06-28 16:06:59

标签: wpf xaml validation

我有几个带有自定义验证器的文本框:
(我不介意将“错误的”数据发送回对象(属性是字符串),如果出现错误,我只需要阻止按钮的功能,所以如果绑定不是正确的地方请告诉我。我只是喜欢我可以使用的Validation.ErrorTemplate支持)

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

如果出现任何验证错误,如何禁用特定按钮?

<Button Content="DO Work"  Height="57" HorizontalAlignment="Left"  Name="button1" VerticalAlignment="Top" Width="234" Click="button1_Click" />

5 个答案:

答案 0 :(得分:46)

您可以在MultiDataTrigger的{​​{1}}中使用Style.Triggers媒体资源。假设我们有Button名为“txtName”。我们必须在TextBox的验证错误上禁用“btnSave”按钮。

以下是您可以做的事情:

TextBox

希望这会对你有所帮助。

答案 1 :(得分:4)

MVVM中的

CanExecute用于授权管理,但人们使用它进行验证。最好的方法是在XAML中完成。如果要验证多个字段,则需要转换器(InverseAndBooleansToBooleanConverter是我对多个布尔值的实现)。以下是如何操作:

XAML代码(我很抱歉,如果XAML代码确实显示,因为即使我尝试也可以显示它):

<Button Name="Button_Test" Content="Test">
    <Button.IsEnabled>
        <MultiBinding Converter="{StaticResource InverseAndBooleansToBooleanConverter}" Mode="TwoWay">
            <Binding ElementName="TextBox_Field1" Path="(Validation.HasError)" />
            <Binding ElementName="TextBox_Field2" Path="(Validation.HasError)" />
            <Binding ElementName="TextBox_Field3" Path="(Validation.HasError)" />
        </MultiBinding>
    </Button.IsEnabled>
</Button>

转换器

public class InverseAndBooleansToBooleanConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values.LongLength > 0)
        {
            foreach (var value in values)
            {
                if (value is bool && (bool)value)
                {
                    return false;
                }
             }
        }    
        return true;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
            throw new NotImplementedException();
    }
}

答案 2 :(得分:3)

将此添加到您的TextBlock:

Validation.Error="Save_Error"

CodeBehind(xaml.cs):

public partial class MyView : Window
{
    private int _noOfErrorsOnScreen = 0;

    public MyView()
    {
        InitializeComponent();
    }


    private void Save_Error(object sender, ValidationErrorEventArgs e)
    {
        if (e.Action == ValidationErrorEventAction.Added)
            _noOfErrorsOnScreen++;
        else
            _noOfErrorsOnScreen--;

        Save.IsEnabled = _noOfErrorsOnScreen > 0 ? false : true;

    }
}

答案 3 :(得分:0)

答案 4 :(得分:0)

如果您使用MVVM,那么只需实现接口ICommand的方法CanExecute。 Button doesn't become disabled when command CanExecute is false

如果你在代码隐藏中编写你的逻辑,那么只需使用按钮的属性IsEnabled:

xaml:

<Button Name=_btn/>

SomeForm.xaml.cs:

_btn.IsEnabled=false;
相关问题