英文:
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:
<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?
答案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:
<TreeViewItem Header="Csv Logs" MouseLeftButtonUp="TreeViewItem_MouseLeftButtonUp"/>
Code behind:
private void TreeViewItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// handle event...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论