使用ViewModel WPF从Codehind执行方法 [英] Execute Method from Codehind using ViewModel WPF

查看:61
本文介绍了使用ViewModel WPF从Codehind执行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是为了使该应用程序退出而在开发过程中放弃了MVVM。

I've abandoned the MVVM midway through app development just to get this app out.

我已经在后面的代码中编写了一种方法来更新数据库/ datagrid等。

I've written a method in the code behind to update the database/datagrid etc.

我的应用程序导航使用的是向ViewModel发出命令以触发某些事件,但除一次初始化类外,再也没有碰到任何代码。

My application navigation is using Commands to the ViewModel firing some event but never touches the code-behind except one time to initialize the class.

所以基本上我按下按钮一次,它就使用默认的初始设置,但是一旦视图为

So basically I push the button one time and it works with the default initial setting but I can't call my code-behind Update() method anymore once the view as been intialized.

如何从视图模型中调用此代码隐藏方法?

How can I call this code-behind method from the view model?

谢谢!

更新代码

 //Navigation ViewModel
//PaneVm.cs

public CommandExtension NewAssignmentCommand { get; set; }
    private void CreateCommands()
    {
        NewAssignmentCommand = new CommandExtension(NewAssignment, CanNewAssignment);
}
GlobalCommands.NewAssignmentCommand = NewAssignmentCommand;

private bool CanNewGroupAssignment(object obj)
    {
        return true;
    }

    private void NewGroupAssignment(object obj)
    {
        OnPropertyChanged("NewGroupAssignmentCommand");
    }


//MainVM.cs
// [Events]
    void _PaneVm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "NewGroupAssignmentCommand")
            WorkspaceVm.CurrentVm = new NewAssignmentsVm();
}


//NewAssignmentVm.cs
//Constructor
    public NewAssignmentsVm()
    {
        var rc = new RepositoryContext();

        _RoResearchers = new ObservableCollection<Researcher>(rc.ResearcherData.GetAllResearchers());

        _QuarterDateTime = DateTime.Now;

        CreateCommands();
    }

//NewAssignment.cs
//Code-behind
//The method
private void UpdateGrid()
    {
        report_datagrid.ItemsSource = null;

        using (var rc = new RepositoryContext())
        {
            if (quarter_datepicker.SelectedDate != null)
            {
                if (!String.IsNullOrEmpty(reportType))
                    researchers = rc.ResearcherData.GetResearchersWeeksByQuarter(Convert.ToDateTime(quarter_datepicker.SelectedDate), reportType).ToList();

            }
        }
    }

更新2 :

我根据此答案解决了我的问题。我创建了一个Global Action

I solved my problem based off this answer. I created a Global Action

 public static class GlobalCommands 
 { 
 public static Action UpdateGrid { get; set; } 
 } 

然后在我的代码隐藏构造函数中,将值设置为public

Then in my code-behind constructor I set the value public

   MyCodeBehind() 
   { 
       GlobalCommands.UpdateGrid = new Action(() => this.UpdateGrid()); 
   } 

不需要再次绑定到上下文。其他一切都一样。谢谢

Didn't need to bind to the context again. Everything else was the same. Thank you

推荐答案

主要思想是:

class MyCodeBehind
{
   public MyCodeBehind()
   {
      Action action = new Action(()=> this.SomeMethodIWantToCall());
      var myVM = new MyVM(action); // This is your ViewModel
      this.DataContext = myVM;
   }

   private void SomeMethodIWantToCall(){...}
}

class MyVM
{
    private Action action;

    public MyVM(Action someAction)
    {
       this.action = someAction;
    }

    private void SomeMethodInVM()
    {
        this.action(); // Calls the method SomeMethodIWantToCall() in your code behind
    }
}

这篇关于使用ViewModel WPF从Codehind执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆