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

huangapple go评论75阅读模式
英文:

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

问题

我正在尝试创建一个可点击的TreeViewItem。

xaml:

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

CodeBehind:

public MouseButtonEventHandler CsvLocationClick
{ get { return OnCsvLocationClick; } }

但在运行时,我收到了以下错误消息:

System.Windows.Markup.XamlParseException: 'System.Windows.Data.Binding' 上的提供值引发了异常。行号 '18' 和行位置 '54'。

内部异常
InvalidCastException: 无法将类型 'System.Reflection.RuntimeEventInfo' 的对象强制转换为类型 'System.Reflection.MethodInfo'。

这个错误是什么意思?如何修复它?

英文:

I am trying to have a clickable TreeViewItem.

xaml:

&lt;TreeViewItem Header=&quot;Csv Logs&quot;  MouseLeftButtonUp=&quot;{Binding Path=CsvLocationClick}&quot;/&gt;

CodeBehind:

public MouseButtonEventHandler CsvLocationClick
    { get { return OnCsvLocationClick; } }

But at runtime, I get the error:

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

Inner Exception
InvalidCastException: Unable to cast object of type &#39;System.Reflection.RuntimeEventInfo&#39; to type &#39;System.Reflection.MethodInfo&#39;.

What does this error mean? How is it fixed?

答案1

得分: 0

你不能绑定到 MouseLeftButtonUp。这是一个你应该连接事件处理程序的事件:

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

代码后台:

private void TreeViewItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    // 处理事件...
}
英文:

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

 &lt;TreeViewItem Header=&quot;Csv Logs&quot;  MouseLeftButtonUp=&quot;TreeViewItem_MouseLeftButtonUp&quot;/&gt;

Code behind:

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

huangapple
  • 本文由 发表于 2023年6月9日 04:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定