[英]Command Parameter for Datagrid row double clicked
我有一个DataGrid用户控件。 网格限制于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
}
要使此工作正常运行,您有几种选择:
StoreNumber
; {Binding SelectedItem.StoreNumber, ElementName=MyDataGrid}
以将数字作为CommandParamter传递; PassEventArgsToCommand="True"
并稍微更新命令定义。 然后,通过访问发件人,您将获得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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.