Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表单验证禁用提交按钮,直到所有字段都填入WPF_C#_Wpf_Data Binding_Forms - Fatal编程技术网

C# 表单验证禁用提交按钮,直到所有字段都填入WPF

C# 表单验证禁用提交按钮,直到所有字段都填入WPF,c#,wpf,data-binding,forms,C#,Wpf,Data Binding,Forms,鉴于: WPF 4.0基于桌面的应用程序。带有两个文本框字段和提交按钮的基本输入表单 XAML代码: <Label Content="Username" /> <TextBox x:Name="Form_UserName" /> <Label Content="Password" /> <TextBox x:Name="Form_Password" /> <Button x:Name="Submit" Click=

鉴于: WPF 4.0基于桌面的应用程序。带有两个
文本框
字段和提交按钮的基本输入表单

XAML代码:

<Label Content="Username" />
    <TextBox x:Name="Form_UserName" />

<Label Content="Password" />
    <TextBox x:Name="Form_Password" />

<Button x:Name="Submit"
    Click="Form_Submit_Button_Click"
    Content="Submit" />

任务: 当且仅当两个
文本框
字段已填充时,才启用提交按钮

解决此问题的经典方法是使用事件处理程序,如
onLostFocus()
或类似的方法,每次用户从字段切换焦点时,我们都可以控制此字段的条件

但由于我的项目是基于WPF的,所以我更喜欢使用原生方式来处理表单-数据绑定机制。我在这个网站和MSDN上也读了一些关于表单验证的文章,但在几乎所有的例子中,都建议使用MVVM框架,我希望在没有任何框架的情况下实现它

另外,我尝试使用
IMultiValueConverter
,但没有收到任何工作结果


请告诉我(代码)建议如何尽可能简单地使用数据绑定解决此问题(我仅从WPF开始)。

这可以使用WPF验证机制轻松完成。首先,由于您希望遵循WPF体系结构,我建议您使用WPF

现在,为了实现您的功能,您可以向窗口/用户控件或
按钮本身添加
CommandBinding

<Button Content="Save" Command="Save">

<Button.CommandBindings>
    <CommandBinding Command="Save" 
                    Executed="Save_Executed" CanExecute="Save_CanExecute" />
    </Button.CommandBindings>
</Button>

代码隐藏

public partial class Window1 : Window,INotifyPropertyChanged
{
    public Window1()
    {
        InitializeComponent();
        DataContext = this;
    }

    private string userName;
    public string Username
    {
        get
        {
            return userName;
        }
        set
        {
            userName = value;
            OnPropertyChanged("UserName");
        }
    }

    private string password;
    public string Password
    {
        get
        {
            return password;
        }
        set
        {
            password = value;
            OnPropertyChanged("Password");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        //Your code
    }

    private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = !(string.IsNullOrEmpty(Username) && string.IsNullOrEmpty(Password));

    }
}

希望这有帮助。

MVVM不是一个框架,而是一种模式。在代码更干净的地方应该使用模式。有很多框架,但您只需要一个类来实现
INotifyPropertyChanged
,并有一个命令来实现
ICommand
。然而,框架只是让编写这些更容易,我怀疑这仍然比您可能遇到的另一个解决方案更容易
public partial class Window1 : Window,INotifyPropertyChanged
{
    public Window1()
    {
        InitializeComponent();
        DataContext = this;
    }

    private string userName;
    public string Username
    {
        get
        {
            return userName;
        }
        set
        {
            userName = value;
            OnPropertyChanged("UserName");
        }
    }

    private string password;
    public string Password
    {
        get
        {
            return password;
        }
        set
        {
            password = value;
            OnPropertyChanged("Password");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        //Your code
    }

    private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = !(string.IsNullOrEmpty(Username) && string.IsNullOrEmpty(Password));

    }
}