2017-04-04 41 views
2

我正在使用MVVM執行我的第一個應用程序。我在「視圖」中聲明瞭Datagrid。代碼XAML如下:如何使用MVVM從DataGrid傳遞有關所選項目的信息

  <DataGridTemplateColumn Header="delete"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <Button 
          Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type> UserControl},Mode=FindAncestor}, Path=DataContext.ClickCommand}">         Content="X" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns>> 
    </DataGrid> 

在我的ViewModel類我可以通過代碼的一部分來運行,我想點擊按鈕後, 「刪除」 功能:

public ICommand ClickCommand => _clickCommand ?? (_clickCommand = new CommandHandler(Delete, _canExecute)); 
    public void Delete() 
    { 
    // DataTable.Rows.RemoveAt(); 
    } 

我有問題,因爲我無法獲取selectet行的索引。 datagrid中的數據源是dataTable。

你有什麼想法如何做到這一點?

我試過用按鈕的命令傳遞參數的東西,但我cound't使它的工作。

+0

它變得更有趣:你可以選擇第1行的同時單擊行2中的按鈕 - 所以你最好不要依賴選定行對每行存在一個按鈕。 – grek40

回答

3

Xmal位碼

<Button Command="{Binding Path=DataContext.ViewCommand,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding Id}" Content="X" Background="Chocolate"/> 

代碼隱藏代碼

public RelayCommand DeleteCommand 
{ 
    get 
    { 
     return new RelayCommand(p => Delete(p)); 
    } 
} 

public void Delete(string id) 
{ 
// DataTable.Rows.RemoveAt(); 
} 

這是例子,你可以通過任何你在CMD參數希望。

繼電器CMD

public class RelayCommand : ICommand 
{ 
    private Action<object> action; 
    private Func<bool> canFuncPerform; 
    public event EventHandler CanExecuteChanged; 
    public RelayCommand(Action<object> executeMethod) 
    { 
     action = executeMethod; 
    } 
    public RelayCommand(Action<object> executeMethod, Func<bool> canExecuteMethod) 
    { 
     action = executeMethod; 
     canFuncPerform = canExecuteMethod; 
    } 
    public void RaiseCanExecuteChanged() 
    { 
     CanExecuteChanged(this, EventArgs.Empty); 
    } 
    public bool CanExecute(object parameter) 
    { 
     if (canFuncPerform != null) 
     { 
      return canFuncPerform(); 
     } 

     if (action != null) 
     { 
      return true; 
     } 

     return false; 
    } 


    public void Execute(object parameter) 
    { 
     if (action != null) 
     { 
      action(parameter);     
     } 
    } 
} 
+0

我是否應該在ViewModel中實現Icommand ID屬性? – Vades

+0

謝謝你的時間。我將它實現到我的項目中。我不得不將Delete函數更改爲 'public void Delete(object id) { // DataTable.Rows.RemoveAt(); } 並將按鈕描述爲: '

+0

Id屬性屬於行對象。你只需要驗證dataGrid的itemsSource。例如: - 應該是這樣的。 –

1

您不應該依賴選定的項目。相反,通過當前行項目作爲CommandParameter

<DataGridTemplateColumn Header="delete"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <Button 
       Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor}, Path=DataContext.ClickCommand}" 
       CommandParameter="{Binding}" 
       Content="X" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

那當然,使用ICommand實現,不丟棄命令參數,並用它來確定要刪除的行。

+0

你能幫我實現嗎? 我想嘗試類似indentiy方法Delete()但它不工作如此好。 – Vades

+0

@Vades我不能,因爲你沒有提供關於'CommandHandler'的詳細信息,它不是一個標準的.Net類。也看看raghava答案,它應該非常接近使用id值作爲參數。這一切都取決於你的viewmodel數據表包含的列... – grek40

相關問題