我有一個DataGrid用戶控件。網格被綁定到可觀察的StoreSales集合的列表。 StoreSales是具有諸如商店名稱,號碼等屬性的類別。 所有這些屬性都是數據網格上的列。雙擊Datagrid行的命令參數
想要實現: 雙擊任意行時,觸發ViewModel(Click_command)上的中繼命令是我檢索該行的存儲號。我能夠觸發RelayCommand的Click_command。
我應該做一些像RelayCommand<string>
並從CommandParamter傳遞storenumber,但不知道如何。
在此先感謝。
這是我
的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結合在你的數據網格中。那麼你的viewmodel知道「doubleclicked」行,當你只處理雙擊 – blindmeis