wpf 空文本框的验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11761644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:48:34  来源:igfitidea点击:

Validation for empty text box

c#wpfvalidationmultidatatrigger

提问by user1240679

I have three text boxes on my wpf application. When the user enters the value in these text boxes and clicks on Add, these are added to the list and displayed in data grid.

我的 wpf 应用程序上有三个文本框。当用户在这些文本框中输入值并单击 时Add,这些值将添加到列表中并显示在数据网格中。

The text boxes are not bound to anything as such and I add these text box values to an observale collection bound to data grid. I want to prevent the user to enter empty values in text boxes. How is that done?

文本框未绑定到任何内容,我将这些文本框值添加到绑定到数据网格的观察集合。我想阻止用户在文本框中输入空值。这是怎么做的?

I saw some examples but those all had the text box boudn to values and then used Binding.Validation. How will this be done in my case when there's binding to text box?

我看到了一些示例,但这些示例都将文本框绑定到值,然后使用Binding.Validation. 当绑定到文本框时,这将如何在我的情况下完成?

I also have a button which has to be freezed when the empty values are being entered. for that, I sed the following approach by making a class and binding the class in the following manner;

我还有一个按钮,当输入空值时必须冻结该按钮。为此,我通过创建一个类并以下列方式绑定该类来实现以下方法;

<Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="IsEnabled" Value="false" />
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=textBox1, Path=(Validation.HasError)}" Value="false" />
                                <Condition Binding="{Binding ElementName=textBox2, Path=(Validation.HasError)}" Value="false" />
                                <Condition Binding="{Binding ElementName=TextBoxAge, Path=(Validation.HasError)}" Value="false" />                                
                            </MultiDataTrigger.Conditions>
                            <Setter Property="IsEnabled" Value="true" />
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>

.cs class

.cs 类

 public class TextBoxNotEmptyValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            string str = value as string;
            if (str != null)
            {
                if (str.Length > 0)
                    return ValidationResult.ValidResult;
            }
            return new ValidationResult(false, Message);
        }
        public string Message { get; set; }
    }

采纳答案by LPL

If I understand you right you're looking for something like this:

如果我理解你是对的,你正在寻找这样的东西:

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Setter Property="IsEnabled" Value="True" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Text, ElementName=textBox1}" Value="{x:Static s:String.Empty}" />
                    <Condition Binding="{Binding Text, ElementName=textBox2}" Value="{x:Static s:String.Empty}" />
                    <Condition Binding="{Binding Text, ElementName=TextBoxAge}" Value="{x:Static s:String.Empty}" />
                </MultiDataTrigger.Conditions>
                <Setter Property="IsEnabled" Value="False" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</Button.Style>

Add this namespace

添加这个命名空间

xmlns:s="clr-namespace:System;assembly=mscorlib"

Update

更新

Then this will work:

然后这将起作用:

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Style.Resources>
            <local:MyTextValidationConverter x:Key="MyTextValidationConverter" />
        </Style.Resources>
        <Setter Property="IsEnabled">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource MyTextValidationConverter}">
                    <Binding Path="Text" ElementName="textBox1" />
                    <Binding Path="Text" ElementName="textBox2" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</Button.Style>

And this Converter code

而这个转换器代码

public class MyTextValidationConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string text1 = values[0] as string;
        if (string.IsNullOrEmpty(text1)) return false;

        string text2 = values[1] as string;
        if (string.IsNullOrEmpty(text2)) return false;

        return true;
    }

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