Calling a Method of a UserControl in MVVM

前端 未结 7 443
温柔的废话
温柔的废话 2021-01-17 14:26

I\'m having an issue with calling a method on a UserControl. Hear me out:

  1. I have a UserControl someControl in SomeView.xaml

  2. Som

相关标签:
7条回答
  • 2021-01-17 15:00

    In MVVM design, the idea is generally not to have ANY code in your UserControl (the xaml.cs file) (in an ideal world). All interaction between the UI and the ViewModel should be handled via Commands and Bindings...so why do you need DoStuff in your user control?

    You might have something like

    <Button Command="{Binding myCommandOnTheDataContextViewModel}" Content="{Binding somePropertyOnTheViewModel}" />
    
    0 讨论(0)
  • 2021-01-17 15:09

    You're probably not going to like the answer, but your ViewModel should have no knowledge of your UI. If you have a non-UI method on your UserControl, it's probably in the wrong place.

    The only thing I could think of is that you may want to implement some type of messaging (like they have in MVVM Light) that could trigger the execution.

    It's either that, or rethink how you've architected your code.

    0 讨论(0)
  • 2021-01-17 15:16

    If the method DoStuff(); does some UI specific logic then the method is in the right place. When not then it should be in another object (e.g. SomeViewModel).

    SomeViewModel is allowed to call a method on SomeView when it is separated via an interface. How this can be accomplished is shown by the WPF Application Framework (WAF).

    0 讨论(0)
  • 2021-01-17 15:19

    Maybe your UserControl should really be a View, which then should have a ViewModel, which would contain the DoStuff() method. SomeViewModel will have instantiated (are at leased casused to be instantiated) SomeControlViewModel, and so be able to call a method on it.

    0 讨论(0)
  • 2021-01-17 15:20

    One the SO answer to Achive this by decouples the ViewModel's knowledge about View is by using the Action delegates answered by Mert here

    Pasted his code here, if the link breaks by any chance.

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-17 15:25

    It sounds like you want DoStuff to happen in response to some data or logic in your VM, in which case the cleanest method would probably be to use an event originating in your VM and handled by DoStuff. If the trigger is more like a state change you could also bind the appropriate VM data to a new Dependency Property on your UserControl and call DoStuff from the DP's change handler.

    0 讨论(0)
提交回复
热议问题