简体   繁体   English

MVVM WPF OnButtonClick中的数据绑定

[英]Data binding in MVVM WPF OnButtonClick

I am using MVVM in my WPF application and I have a problem with data binding. 我在WPF应用程序中使用MVVM,但数据绑定出现问题。 I am considering binding user actions to data operations (in my case adding record to database). 我正在考虑将用户操作绑定到数据操作(在我的情况下,将记录添加到数据库)。 If I use heavy coupling between CommandClass and ViewModelClass everything works fine. 如果我在CommandClass和ViewModelClass之间使用重耦合,则一切正常。 My CommandClass in this case looks like this: 在这种情况下,我的CommandClass如下所示:

public class ButtonCommand : ICommand
{
    private readonly UserViewModel _userViewModel;

    public ButtonCommand(UserViewModel viewModel)
    {
        _userViewModel = viewModel;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        _userViewModel.AddUser();
    }

    public event EventHandler CanExecuteChanged;
}

My heavy coupling in ViewModelClass looks like this: 我在ViewModelClass中的沉重耦合看起来像这样:

    private readonly ButtonCommand _buttonCommand;

    public UserViewModel()
    {
        _buttonCommand = new ButtonCommand(this);
    }

    public ICommand btnClick
    {
        get { return _buttonCommand; }
    }

My XAML coupling on button click (take a look on a Command section): 单击按钮时的XAML耦合(请查看“命令”部分):

 <Page.Resources>
    <viewModel:UserViewModel x:Key="UserObj" TxtFirstName="" TxtLastName="" TxtEmail="" TxtPassword=""/>
 </Page.Resources>
 ....
 <Button Content="Submit" HorizontalAlignment="Left" Margin="42,231,0,0" VerticalAlignment="Top" Width="75" Command="{Binding btnClick, Mode=OneWay, Source={StaticResource UserObj}}"/>

And I have such an output (take a look at Submit button): Window . 我有这样的输出(看一下Submit按钮): Window

After I make changes to my CommandClass and ViewModelClass (to make them more general and reusable), but leave my XAML coupling the same the Submit button becomes unavailable after runnig my application. 在对CommandClass和ViewModelClass进行更改(以使其更加通用和可重用)之后,但在运行应用程序后,将XAML耦合保持不变,则Submit按钮变得不可用。 After changes CommandClass looks like this: 更改后,CommandClass如下所示:

public class ButtonCommand : ICommand
{
    private readonly Action _executionMethod;
    private readonly Func<bool> _executeOrNot; 

    public ButtonCommand(Action executionMethod, Func<bool> executeOrNot)
    {
        _executionMethod = executionMethod;
        _executeOrNot = executeOrNot;
    }

    public bool CanExecute(object parameter)
    {
        return _executeOrNot();
    }

    public void Execute(object parameter)
    {
        _executionMethod();
    }

    public event EventHandler CanExecuteChanged;
}

My ViewModelClass after changes: 更改后我的ViewModelClass:

private readonly ButtonCommand _buttonCommand;

public UserViewModel()
{
    _buttonCommand = new ButtonCommand(AddUser, IsValidInputForRegistration);

}
public ICommand btnClick
{
    get { return _buttonCommand; }
}

XAML I leave the same. XAML我保持不变。 The output I have is next (take a look at Submit button): WindowWithChanges . 接下来是我的输出(看一下Submit按钮): WindowWithChanges

Can anyone provide me with some information, why button became unavailable and where do I mess up? 谁能为我提供一些信息,为什么按钮不可用以及我在哪里弄糟?

First, try IsValidInputForRegistration to always return true . 首先,尝试IsValidInputForRegistration始终return true That will prove that your implementation of IButton (ie your ButtonCommand class) works fine. 这将证明你的执行IButton (即你的ButtonCommand类)工作正常。

If that works, what's happening to your program is the IsValidInputForRegistration passes the state for your _buttonCommand during initialization and it will stay on that state since it doesn't query if the IsValidInputForRegistration have changed states. 如果这样IsValidInputForRegistration ,则您的程序正在发生的事情是_buttonCommandinitialization期间传递了IsValidInputForRegistrationstate ,由于它不query IsValidInputForRegistration是否已更改状态,因此它将保持在该状态。

To achieve querying of states, you can implement the EventHandler CanExecuteChanged like so: 为了实现状态查询,您可以像这样实现EventHandler CanExecuteChanged

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

You can look into msdn for what CommandManager.RequerySuggested does. 您可以查看msdnCommandManager.RequerySuggested作用。 But I think the description says it all. 但是我认为描述说明了一切。 :) :)

Occurs when the CommandManager detects conditions that might change the ability of a command to execute. 当CommandManager检测到可能改变命令执行能力的条件时发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM