using EventToCommand & PassEventArgsToCommand :: how to get sender, or better metaphor?

Posted by JoeBrockhaus on Stack Overflow See other posts from Stack Overflow or by JoeBrockhaus
Published on 2011-01-07T16:25:22Z Indexed on 2011/01/07 18:53 UTC
Read the original article Hit count: 271

Filed under:
|

The point of what I'm doing is that there are a lot of things that need to happen in the viewmodel, but when the view has been loaded, not on constructor. I could wire up event handlers and send messages, but that just seems kinda sloppy to me. I'm implementing a base view and base viewmodel where this logic is contained so all my views get it by default, hopefully.

Perhaps I can't even get what I'm wanting: the sender. I just figured this is what RoutedEventArgs.OriginalSource was supposed to be? [Edit] In the meantime, I've hooked up an EventHandler in the xaml.cs, and sure enough, OriginalSource is null there as well. So I guess really I need to know if it's possible to get a reference to the view/sender in the Command as well? [/Edit]

My implementation requires that a helper class to my viewmodels which is responsible for creating 'windows' knows of the 'host' control that all the windows get added to. i'm open to suggestions for accomplishing this outside the scope of using eventtocommand. :)

(the code for Unloaded is the same)

    #region ViewLoadedCommand
    private RelayCommand<RoutedEventArgs> _viewLoadedCommand = null;
    /// <summary>
    /// Command to handle the control's Loaded event. 
    /// </summary>
    public RelayCommand<RoutedEventArgs> ViewLoadedCommand
    {
        get
        {
            // lazy-instantiate the RelayCommand on first usage
            if (_viewLoadedCommand == null)
            {
                _viewLoadedCommand = new RelayCommand<RoutedEventArgs>(
                    e => this.OnViewLoadedCommand(e));
            }
            return _viewLoadedCommand;
        }
    }
    #endregion ViewLoadedCommand


    #region View EventHandlers
    protected virtual void OnViewLoadedCommand(RoutedEventArgs e)
    {
        EventHandler handler = ViewLoaded;
        if (handler != null)
        {
            handler(this, e);
        }
    }
    #endregion

© Stack Overflow or respective owner

Related posts about mvvm-light

Related posts about eventargs