Search code examples
c#wpftreeviewtreeviewitem

Wpf TreeViewItem MouseLeftButtonUp binding error Unable to cast object 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'


I am trying to have a clickable TreeViewItem.

xaml:

<TreeViewItem Header="Csv Logs"  MouseLeftButtonUp="{Binding Path=CsvLocationClick}"/>

CodeBehind:

public MouseButtonEventHandler CsvLocationClick
    { get { return OnCsvLocationClick; } }

But at runtime, I get the error:

System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '18' and line position '54'.'

Inner Exception
InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.

What does this error mean? How is it fixed?


Solution

  • You cannot bind to MouseLeftButtonUp. It's an event to which you are expected to hook up an event handler:

     <TreeViewItem Header="Csv Logs"  MouseLeftButtonUp="TreeViewItem_MouseLeftButtonUp"/>
    

    Code behind:

    private void TreeViewItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        // handle event...
    }