Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm discovering the MVVM-pattern for WPF. I fail to move code from the codebehind to the viewmodel-class.

What I have is the following:

C#
public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
            MouseDown += MouseClickHandler;
        }
        
        private void MouseClickHandler(object sender, MouseButtonEventArgs e)
        {
            //DoSomething
        }
}


And

C#
public class MainViewModel
    {
        public MainViewModel()
        {
            MouseButtonEventHandler mouseButton = MouseClickHandler;
        }

        private void MouseClickHandler(object sender, MouseButtonEventArgs e)
        {
            //DoSomething
        }
    }


This doesn't work... The debugger gets into the constructor of the viewmodel, but not into the MouseClickHandler. When want to do the same thing in de code behind, it works.

Does anyone know how I can fix this?
Posted
Comments
Sergey Alexandrovich Kryukov 18-Aug-14 16:49pm    
Not clear. A view model class is also "code behind", as all the classes. What to "move", where?
—SA
Paulo Zemek 18-Aug-14 17:00pm    
I think that you are really lost... but I will try to explain what I see.
In the MainViewModel constructor you create a mouseButton local variable that points to the MouseClickHandler. Such mouseButton variable is never used and will be collected.
I am pretty sure that you wanted to do something like the MouseDown += MouseClickHandler, but the only thing you did was to create a delegate for that method and let it die.
Then there's the real problem: The ViewModel can't know about the existence of the MouseDown event... it is the View that should know about it.
If you were using the Click event we could create a Command to do the job, but if you really want the MouseDown event, there's no perfect solution, the View will still need to implement that event, even if its only job is to call a method of the ViewModel.
Sergey Alexandrovich Kryukov 19-Aug-14 2:12am    
What are you talking about? What "delegate for this method"? MouseDown is the event instance of the form, its lifetime is the lifetime of the form instance. Moreover, if you add an event handler to its invocation list, the fact that the handler method is referenced by the event instance will keep all objects the handler depends on from GC collection until the end of the form lifetime (effect of closure).

But I agree that OP is lost... :-)

—SA
Paulo Zemek 19-Aug-14 8:21am    
I am talking about the ViewModel constructor, not the form. The ViewModel is the one with problems.
MouseButtonEventHandler mouseButton = MouseClickHandler;
mouseButton is a local variable, and it is never assigned to anyone.
Sergey Alexandrovich Kryukov 19-Aug-14 11:59am    
Oh... Sorry, I was confused by you line "MouseDown += MouseClickHandler", which would work correctly as the event handler is actually added to the event of the window; I you added to "I am pretty sure that you wanted to do something like the MouseDown += MouseClickHandler" something like "which really would work", it would be less confusing.

Of course, the line MouseButtonEventHandler mouseButton = MouseClickHandler; makes no sense at all.
It's pretty apparent that OP does not understand using events, unfortunately.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900