wpf - 验证按钮单击事件 wpf c#

标签 wpf validation combobox textbox

我有一个 WPF 应用程序,我必须在其中检查 TextBox值和 ComboBox .如果它是空的或者不是格式,按钮点击事件应该触发一个错误,如果在 ComboBox 中选择的索引是 0再次它应该触发一个错误。(就像在错误提供者中一样)。

我在互联网上做了很多研究,发现了 IDataErrorInfo 的解决方案。但问题是我如何在按钮单击事件上执行此操作。所有的例子都是在表单加载上做的。

我对 WPF 很陌生。以下是我的代码

public class ClientMap : IDataErrorInfo
{
    public string CDSNo { get; set; }

    public ClientMap(int ID)
    {
        Id = ID;
    }
    public ClientMap()
    {

    }

    public string Error
    {
        get { throw new NotImplementedException(); }
    }

    public string this[string columnName]
    {
        get
        {
            string result = null;
            if (columnName == "CDSNo")
            {
                if (string.IsNullOrEmpty(CDSNo))
                    result = "Please enter a CDS No";
                else
                {
                    string regEx = "[A-Z]{3}-\\d{9}-[A-Z]{2}-\\d{2}";
                    if (!Regex.IsMatch(CDSNo, regEx))
                    {
                        result = "Invalid CDS No";
                    }
                }
            }

            return result;
        }
    }

    public int Id { get; set; }
    public CE.Data.Customer Customer { get; set; }
    public CE.Data.Institute Institute { get; set; }
    public bool Archived { get; set; }
    public DateTime DateCreated { get; set; }

}

XAML 是
<Window.Resources>
    <validation:ClientMap x:Key="data"/>
</Window.Resources>

<control:AutoCompleteTextBox Style="{StaticResource textBoxInError}">
    <TextBox.Text>
        <Binding Path="CDSNo" Source="{StaticResource data}"
                ValidatesOnDataErrors="True"   
                UpdateSourceTrigger="Explicit">

            <Binding.ValidationRules>
                <ExceptionValidationRule/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</control:AutoCompleteTextBox>

请帮我。
谢谢

最佳答案

这是修改后的代码 article .您需要从该站点的下载中获取引用资料和其他类。

Window1.xaml

<Window x:Class="SOTCBindingValidation.Window1" x:Name="This"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SOTCBindingValidation"
    Title="SOTC Validation Test" Height="184" Width="390">
    <Window.Resources>
        <local:ErrorsToMessageConverter x:Key="eToMConverter" />
    </Window.Resources>
    <StackPanel Margin="5">
        <TextBlock Margin="2">Enter An IPv4 Address:</TextBlock>
            <TextBox x:Name="AddressBox">
                <TextBox.Text>
                    <Binding ElementName="This" Path="IPAddress" 
                             UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <local:IPv4ValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
        <TextBlock Margin="2" Foreground="Red" FontWeight="Bold" 
            Text="{Binding ElementName=AddressBox, 
                          Path=(Validation.Errors),
                          Converter={StaticResource eToMConverter}}" />
            <Button Name="Btn1" Height ="30" Width="70" Click="Btn1_Click"></Button>
    </StackPanel>

</Window>

Window1.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace SOTCBindingValidation
{

    public partial class Window1 : Window
    {
        public static readonly DependencyProperty IPAddressProperty =
            DependencyProperty.Register("IPAddress", typeof(string),
            typeof(Window1), new UIPropertyMetadata(""));

        public string IPAddress
        {
            get { return (string)GetValue(IPAddressProperty); }
            set { SetValue(IPAddressProperty, value); }
        }

        public Window1()
        { InitializeComponent(); }

        private void Btn1_Click(object sender, RoutedEventArgs e)
        {
            ForceValidation();
            if (!Validation.GetHasError(AddressBox))
            {
                // Put the code you want to execute if the validation succeeds here
            }
        }
        private void ForceValidation()
        {
            AddressBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

    }
}

关于wpf - 验证按钮单击事件 wpf c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004913/

相关文章:

WPF:获取 GroupBox 的内容以填充可用空间

wpf - 在 MVVM 中的 TextBox.GotFocus() 事件上显示日历

.net - 创建了可绑定(bind)的 WindowsFormsHost,但子更新未反射(reflect)到控件

javascript - 多个输入的验证 | HTML-jQuery

带有部分的 QtQuick2 ComboBox

java - 在 JavaFX 中使用枚举类填充组合框?

c# - 主窗口中的静态成员与实例成员

asp.net-mvc-3 - 如何在.NET MVC3中注入(inject)用于验证的依赖项?

asp.net-mvc - 类 mvc 上的多个自定义验证属性

WPF DataGridComboBoxColumn 按键时编辑