如何根据验证

时间:2017-10-08 15:02:44

标签: c# wpf vb.net material-design-in-xaml

验证部分 `

Imports System.Globalization
Namespace Validation
Public Class ISBNLength
            Inherits ValidationRule
        Public Overrides Function Validate(value As Object, cultureInfo As CultureInfo) As ValidationResult
        If value.ToString.Trim.Length = 10
                        Return New ValidationResult(True,"")
              Else 
            Return New ValidationResult(False,"Must be a 10 digit number")
        End If              
        End Function
End Class
    End Namespace

**XAML**

<materialDesign:PackIcon Kind="BarcodeScan"
                                 Grid.Row="1" Margin="0,0,16,10" />
        <TextBox x:Name="TxtISBN"
                 Grid.Row="1"
                 materialDesign:HintAssist.Hint="ISBN Number" MaxLength="10" >
            <TextBox.Text>
                <Binding ElementName="TxtISBN" Path="Text" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <validation:NotEmpty/>
                        <validation:ISBNLength/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>

        </TextBox>

            <Button
                x:Name="BtnAccept"
                IsDefault="True"
                Command="materialDesign:DialogHost.CloseDialogCommand"
                Content="ADD"
                IsEnabled="False"
                Height="36" Width="90" />

现在我想在函数返回false时禁用该按钮。

注意:我无法在不创建新实例的情况下访问对话框。

2 个答案:

答案 0 :(得分:2)

您可以使用Style DataTrigger绑定Validation.HasError的{​​{1}}附加属性:

TextBox

另请注意,<Button x:Name="BtnAccept" IsDefault="True" Command="materialDesign:DialogHost.CloseDialogCommand" Content="ADD" Height="36" Width="90"> <Button.Style> <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=(Validation.HasError), ElementName=TxtISBN}" Value="True"> <Setter Property="IsEnabled" Value="False" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> 应绑定到源属性。它无法绑定到自己的TextBox属性。

答案 1 :(得分:0)

将Button`s Enabled绑定到输入文本并使用值转换器:(没有VB,抱歉)

public static class Extensions
{
    public static bool IsIsbn(this string s)
    {
        if ((s?.Trim() ?? "") is string isbn)
            return s.Length == 10 /* && whatever */ ;

        return false;
    }
}

public class ValidIsbnToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string s)
            return s.IsIsbn();

        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => throw new NotImplementedException();
}

public class ValidationRuleIsbn : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        return ((value as string)?.IsIsbn() ?? false)
            ? ValidationResult.ValidResult
            : new ValidationResult(false, "Must be a 10 digit number");
    }
}