【问题标题】:Cast Error occured in WFP DataGrid CurrentCellChanged While on Binding a RelayCommand绑定 RelayCommand 时 WPF DataGrid CurrentCellChanged 中发生强制转换错误
【发布时间】:2016-01-12 10:33:39
【问题描述】:

内部异常消息: 无法将“System.Reflection.RuntimeEventInfo”类型的对象转换为“System.Reflection.MethodInfo”类型

<DataGrid CurrentCellChanged="{Binding CallCommand}" AutoGenerateColumns="False" ItemsSource="{Binding MobileList, UpdateSourceTrigger=PropertyChanged}" SelectionUnit="FullRow" IsReadOnly="True">
    <DataGrid.InputBindings>
        <KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding Path=DataContext.CopyToClipBoardCommand}" CommandParameter="{Binding }" />
    </DataGrid.InputBindings>
    <DataGrid.Columns>
        <!--Column 1-->
        <DataGridTextColumn Binding="{Binding MobileName}" Header="Name" />
        <!--Column 2-->
        <DataGridTextColumn Binding="{Binding MobileOS}" Header="OS" />
    </DataGrid.Columns>
</DataGrid>

我的 RelayCommand 源代码:

public RelayCommand<KeyboardEventArgs> CallCommand
{
    get
    {
        return new RelayCommand<KeyboardEventArgs>((selectedItem) =>
        {

        });
    }
}

请帮助我,如何使用 MVVM 方法将 RelayCommand 绑定到 DataGrid 中的 CurrentCellChanged 属性?

请将事件命令绑定到CurrentCellChanged 属性。

【问题讨论】:

  • @Sinatr - 在您的链接中,上下文相同但问题不同,这里我面临错误。让我知道它为什么会抛出错误?
  • 它会抛出错误,因为您试图将ICommand 绑定到事件。这是不可能的。有一些特殊的框架使这成为可能(例如 MVVMlight,参见 here 示例)。
  • 如何为上述属性 CurrentCellChanged 链接交互触发器
  • 什么是交互触发?在 MVVM 中,您可以使用绑定来完成大部分工作(请参阅 this),而不是使用命令,您可以绑定属性(在您的情况下可能是 CurrentCell)并在属性设置器中执行一些操作。

标签: c# wpf mvvm datagrid relaycommand


【解决方案1】:

使用 System.Windows.Interactivity.dll 进行交互,它用于将命令绑定到事件,因此您的 MVVM 模式不会违反。

在 Window/Usercontrol 的顶部,您必须编写命名空间以使用交互,

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

这里,我放了一些xaml代码。

 <DataGrid  AutoGenerateColumns="False" 
                   ItemsSource="{Binding MobileList, UpdateSourceTrigger=PropertyChanged}" 
                   SelectionUnit="FullRow" IsReadOnly="True" >

            <DataGrid.InputBindings>
                <KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding Path=DataContext.CopyToClipBoardCommand}" CommandParameter="{Binding }" />
            </DataGrid.InputBindings>

            <i:Interaction.Triggers>
                <i:EventTrigger  EventName="CurrentCellChanged">
                    <i:InvokeCommandAction Command="{Binding Path=DataContext.CallCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <DataGrid.Columns>

                <!--Column 1-->
                <DataGridTextColumn Binding="{Binding MobileName}" Header="Name" />
                <!--Column 2-->
                <DataGridTextColumn Binding="{Binding MobileOS}" Header="OS" />
            </DataGrid.Columns>
        </DataGrid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-09
    • 2013-07-24
    • 2014-12-02
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2011-09-18
    相关资源
    最近更新 更多