是否可以用鼠标右键选择没有后面代码的Datagrid Row(即在xaml.cs中)? [英] Is it possible to select Datagrid Row with right mouse button with no code behind ( i.e in xaml.cs)?

查看:87
本文介绍了是否可以用鼠标右键选择没有后面代码的Datagrid Row(即在xaml.cs中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里检查了问题的答案:(https://social.msdn.microsoft.com/Forums/vstudio/en-US/63fa1e10-1050-4448-a2bc-62dfe0836f25/selecting- datagrid行何时按下鼠标右键?forum = wpf) 
但是我的问题是,每当我执行"MouseRightButtonUp"时,都会触发所选DataGridRow的事件.但是它不能作为"SelectionChanged"使用.在"MouseRightButtonUp"之后发生事件.并且尚未选择该行.

I have already checked the answer to the question here: ( https://social.msdn.microsoft.com/Forums/vstudio/en-US/63fa1e10-1050-4448-a2bc-62dfe0836f25/selecting-datagrid-row-when-right-mouse-button-is-pressed?forum=wpf ) 
But my problem is that whenever I do "MouseRightButtonUp", I trigger an event for the selected DataGridRow. But it doesn't work as "SelectionChanged" event occurs after "MouseRightButtonUp" and the row is not selected yet.

有人告诉我不要在后面使用代码,而只能使用ViewModel.所有事件都将转换为命令并绑定到ViewModel属性.如何在MouseRightButtonUp上获得选定的项目

I am told not to use code behind, but use ViewModel only. All events are converted to commands and bound to ViewModel properties. How can I get selected item on MouseRightButtonUp 

推荐答案


NitinSC,您好


Hi NitinSC,

>>如何在MouseRightButtonUp上获得选定的项目

在视图模型中为MouseRightButtonUp事件添加一个ICommand.在执行事件中,单击鼠标右键可以获取选定的项目.

Add a ICommand in your view model for MouseRightButtonUp event. In the Execute event, you can get the selected item when you Mouse Right Button Up.

以下核心代码供您参考.

The following core code for your reference.

XAML:

 <DataGrid x:Name="testdg" ItemsSource="{Binding PersonsInfo}"    AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
                <DataGridTextColumn Header="Age" Binding="{Binding Age}"></DataGridTextColumn>
                <DataGridTextColumn Header="Address" Binding="{Binding Address}"></DataGridTextColumn>
            </DataGrid.Columns>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseRightButtonUp">
                    <i:InvokeCommandAction  Command="{Binding Path=DataContext.RemoveCommand,RelativeSource={RelativeSource AncestorType={x:Type  DataGrid}}}" CommandParameter="{Binding Path=SelectedItem ,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"  />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </DataGrid>

XAML.CS:

 ICommand _command;

  public ICommand RemoveCommand
        {
            get
            {
                if (_command == null)
                {
                    _command = new DelegateCommandTT(CanExecute, Execute);
                }
                return _command;
            }
        }

        private void Execute(object parameter)
        {
            RecordInfo rinfo = parameter as RecordInfo;
            int index = PersonsInfo.IndexOf(parameter as RecordInfo);
            if (index > -1 && index < PersonsInfo.Count)
            {
                PersonsInfo.RemoveAt(index);
            }
        }

    public class DelegateCommandTT : ICommand
    {}



最好的问候,

吕汉楠


这篇关于是否可以用鼠标右键选择没有后面代码的Datagrid Row(即在xaml.cs中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆