【问题标题】:Command Parameter for Datagrid row double clicked双击 Datagrid 行的命令参数
【发布时间】:2026-02-06 06:10:01
【问题描述】:

我有一个带有 DataGrid 的用户控件。 Grid 绑定到 StoreSales 的可观察集合列表。其中 StoreSales 是一个具有商店名称、编号等属性的类。 所有这些属性都是数据网格上的列。

想要实现: 在任何行的双击触发 ViewModel(Click_command) 上的中继命令是我检索该行的商店编号。我能够触发 RelayCommand Click_command。

我应该执行RelayCommand<string> 之类的操作并从 CommandParamter 传递商店编号,但不知道如何。

提前致谢。

这就是我所拥有的

Xaml:

<UserControl x:Class="MyProject.StoreList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
             xmlns:vm="clr-namespace:Charlie.UI.ViewModel"
             xmlns:ch="clr-namespace:Charlie.UI"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
 <Grid>
     <DataGrid  IsReadOnly="True" ItemsSource="{Binding Path=StoreList}" AutoGenerateColumns="False" Name="StoreList" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="#"  Binding="{Binding Path=StoreNumber}" />
                <DataGridTextColumn Header="StoreName"  Binding="{Binding Path=StoreName}"/>
                <DataGridTextColumn Header="Total" Binding="{Binding Path=Total, StringFormat=C}"/>


        </DataGrid.Columns>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=Click_command, Mode=OneWay}" PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>
        </Grid>

视图模型:

 public RelayCommand Click_command
{
      get;
      private set;
}

private ObservableCollection<StoreSales> _StoreList;
    public ObservableCollection<StoreSales> StoreList 
    {
        get { return _StoreList; }
        set
        {
            _StoreList = value;
            RaisePropertyChanged("StoreList");
        }
    }

//构造函数

public StoreList()
{
      this.Click_command = new RelayCommand(() => Execute_me());
}

public void Execute_me()
{
   //Do something with store number           
}

【问题讨论】:

  • 另一种方法是使用不带参数的命令,但在数据网格中使用 SelectedItem 绑定。那么当您处理双击时,您的视图模型就会知道“双击”行

标签: c# xaml mvvm-light wpfdatagrid


【解决方案1】:

要使其正常工作,您有多种选择:

  1. 在您的 ViewModel 中定义 SelectedStore 属性。从 DataGrid 将其绑定到 SelectedItem。不要将任何东西作为参数传递给命令并使用创建的属性来获取StoreNumber
  2. 指定绑定:{Binding SelectedItem.StoreNumber, ElementName=MyDataGrid} 将数字作为 CommandParamter 传递;
  3. 使用PassEventArgsToCommand="True" 并稍微更新命令定义。然后通过访问 sender 你会得到 SelectedItem (我不喜欢这种方法,但它仍然存在);

对于第三个选项(从 MVVM 角度来看不太可取)命令应如下所示:

public RelayCommand Click_command
{
      get
      {
          if (this.click_command == null)
          {
                this.click_command = new RelayCommand<MouseButtonEventArgs>((args) => this.Execute_me(args));
          }
          return this.click_command;
      }
}

【讨论】: