Event Handling WPF

I’m using the VC-Scene Graph Panel WPF project. In addition I would like to focus a specific component by clicking the corresponding node on the WPF panel.
Currently I have two classes:

  1. public partial class SceneGraphPanelView : UserControl which is the partial class for the WPF UserControl
  2. public class SceneGraphPanelViewModel : DockableScreen, IPlugin which accesses the VC simulation

What I’ve tried yet is to add an EventHandler to the WPF class which will be triggert on the MouseDown Event when clicking on a node in the WPF Treeview. I try to listen to that event than in the SceneGraphPanelViewModel class in order to focus the component afterwards.

 public partial class SceneGraphPanelView : UserControl
    {
public event EventHandler<string> FokusCompEvent;

 void MouseLeftDown(object sender, MouseEventArgs e)
{
...
 FokusCompEvent?.Invoke(this, nodeName);
}

}

public class SceneGraphPanelViewModel : DockableScreen, IPlugin
    {
 public SceneGraphPanelViewModel()
        {
            SceneGraphPanelView sG = new SceneGraphPanelView();
            sG.FokusCompEvent += SG_FokusCompEvent;
        }
 private void SG_FokusCompEvent(object sender, string clickedNodeName)
        {
            MS.AppendMessage(clickedNodeName, MessageLevel.Warning);
        }
}

I would expect, that the name of the node will be printed, but it seems that there is a problem. When I listen to the event in any other class in my namespace it works, but not in the SceneGraphPanelViewModel class.

Could there be an issue because of IPlugin or DockableScreen?
Any suggestion on how to solve that or an other approach?

Thanks.

I’m guessing that you end up having 2 or more SceneGraphPanelView objects. The one you create manually and register the event handler to probably isn’t actually shown in the UI.

VC uses Caliburn.Micro and other framework stuff that essentially creates the views and binds the viewmodels automatically behind the scenes.

I wouldn’t mix the View with the ViewModel. But if you somehow want to get a reference to the view in your view model then I see at least 1 error in your code:

SceneGraphPanelView sG = new SceneGraphPanelView();

I would remove that and instead try to override the OnViewLoadedMethod() in your ViewModel

private SceneGraphPanelView _view = null;
protected override void OnViewLoaded(object view)
{
	base.OnViewLoaded(view);
	_view = view as SceneGraphPanelView;
}

But as Tsy mentions, is better to use bindings between the View and ViewModel.