Home > Software engineering >  WPF - Assign a command to a control's event
WPF - Assign a command to a control's event

Time:10-04

I've a Telerik.RadGanttView component that implements some custom behavior, namely being able to adjust the zoom level of the timeline by using the scrollwheel over said timeline. This is done in the code-behind for the control with the following lines:

        _gantt = GetTemplateChild("Gantt") as RadGanttView;
        _gantt.PreviewMouseWheel  = _gantt_PreviewMouseWheel;

_gantt_PreviousMouseWheel is the method that contains the logic to adjust the zoom level. While this works fine, I wanted to move the zooming logic to my view model and, if possible, bind the PreviousMouseWheel event to a command in my ViewModel rather than a method in my code-behind. I've tried using the Interaction.Triggers xaml tags, but while they did register the event, they only registered it when my mouse was NOT hovering over the timeline, which is the opposite of what I'm looking for. Is there any way to achieve what is done in my code-behind and use it to call a command rather than a method in my code-behind?

Thanks a lot for your help!

CodePudding user response:

Is there any way to achieve what is done in my code-behind and use it to call a command rather than a method in my code-behind?

Just invoke your command from the event handler programmatically:

_gantt = GetTemplateChild("Gantt") as RadGanttView;
_gantt.PreviewMouseWheel  = (ss, ee) =>
{
    var viewModel = DataContext as YourViewModel;
    if (viewModel != null)
        viewModel.YourCommand.Execute(null);
}

This is just as good as using an interaction trigger in the very same view to invoke the very same command as far as MVVM is concerned.

CodePudding user response:

I see Gantt Chart, I upvote ! May I share with you one of our articles that talks about it, hope it helps :) https://zenkit.com/en/blog/the-importance-and-benefits-of-project-management-software/

  • Related