PSSGCSim
PSSGCSim

Reputation: 1287

Raise event TreeViewItem.MouseDoubleClickEvent

I'm having problems with rising event on TreeViewItem. I always get this exception

Unable to cast object of type 'System.Windows.RoutedEventArgs' to type 'System.Windows.Input.MouseButtonEventArgs'

There is my code behind the exception.

private void LibraryTree_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        if (LibraryTree.SelectedItem == null) { return; }
        TreeViewItem tvi = (TreeViewItem)LibraryTree.SelectedItem;
        tvi.RaiseEvent(new RoutedEventArgs(TreeViewItem.MouseDoubleClickEvent));
    }
}

Upvotes: 0

Views: 148

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81348

MouseDoubleClickEvent expects MouseButtonEventArgs so raise event like this:

tvi.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                           { RoutedEvent = TreeViewItem.MouseDoubleClickEvent });

Upvotes: 2

Related Questions