2017-06-21 30 views
1

在我的視圖中,我有一個DataGrid,它存儲2個遞減類型的對象。每一行都有一個帶有連接到ViewModel的命令的按鈕。在ViewModel中,我需要找出哪種類型的對象已被選中。WPF MVVM - 通過ViewModel訪問視圖中DataGrid的DependencyProperty

問題是什麼是在ViewModel中從Execute命令方法訪問的SelectedItem屬性的最好和簡單方法?

到目前爲止,我這樣做是這樣的:

var window = Application.Current.Windows.OfType<Window>() 
    .SingleOrDefault(x => x.IsActive); 

var dataGrid = (DataGrid) window.FindName("MyGridName"); 
... 

更新 - XAML中:

<DataGrid Name="MyGridName" ItemsSource="{Binding Elements}" 
      AutoGenerateColumns="False" CanUserAddRows="False" 
      CanUserDeleteRows="False" IsReadOnly="True"> 
    <DataGrid.Columns> 
    <DataGridTemplateColumn Width="auto"> 
     <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <Button Name="OptionsBtn" Margin="5" Width="auto" 
        Height="30" Content="Options" 
        Command="{Binding ElementName=ElementsViewWindow, 
         Path=DataContext.ShowOptionsMenuCommand}"/> 
     </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 
+3

綁定了'你的網格SelectedItem'一些VM屬性或使用'CommandParameters'和停止不管你正在嘗試做... – user3292642

+1

顯示您的Xaml中的命令以及如何綁定它。我很高興能向您展示如何使用該命令參數方法! – CHS

+0

'

回答

2

如果採取正確的方法MVVM這是很容易做到的。您只需要定義將綁定到您的DataGridItemsSource的某個實體的項目集合,並將其綁定到您的的SelectedItem。然後在您的命令中,只需引用您的模型的選定項目屬性即可訪問DataGrid中的選定項目。

以下是MVVM Light的示例實現。首先,你定義實體的可觀察集合:

public const string ItemsCollectionPropertyName = "ItemsCollection"; 
private ObservableCollection<DataItem> _itemsCollection = null; 
public ObservableCollection<DataItem> ItemsCollection 
{ 
    get 
    { 
     return _itemsCollection; 
    } 
    set 
    { 
     if (_itemsCollection == value) 
     { 
      return; 
     } 

     _itemsCollection = value; 
     RaisePropertyChanged(ItemsCollectionPropertyName); 
    } 
} 

然後你定義一個屬性來保存所選的項目:

public const string SelectedItemPropertyName = "SelectedItem"; 
private DataItem _selectedItem = null; 
public DataItem SelectedItem 
{ 
    get 
    { 
     return _selectedItem; 
    } 
    set 
    { 
     if (_selectedItem == value) 
     { 
      return; 
     } 

     _selectedItem = value; 
     RaisePropertyChanged(SelectedItemPropertyName); 
    } 
} 

後您定義的命令來處理業務邏輯:

private ICommand _doWhateverCommand; 
public ICommand DoWhateverCommand 
{ 
    get 
    { 
     if (_doWhateverCommand == null) 
     { 
      _doWhateverCommand = new RelayCommand(
       () => { /* do your stuff with SelectedItem here */ }, 
       () => { return SelectedItem != null; } 
      ); 
     } 
     return _doWhateverCommand; 
    } 
} 

最後,您創建視圖元素並將它們綁定到ViewModel

<DataGrid ItemsSource="{Binding ItemsCollection}" SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="True" /> 
<Button Content="Do stuff" Command="{Binding DoWhateverCommand}" /> 
1

問題是什麼是從ViewModel中的Execute命令函數訪問DataGrid的SelectedItem屬性的最佳和簡單的方法?

只是一個屬性添加到其中ShowOptionsMenuCommand屬性定義的視圖模型類和綁定DataGrid這一個的SelectedItem屬性:

<DataGrid Name="MyGridName" ItemsSource="{Binding Elements}" SelectedItem="{Binding SelectedElement}" ... > 

然後你就可以訪問源屬性(SelectedElement或無論你選擇怎麼稱呼它)直接從Execute方法。

另一種選擇將是該項目通過爲CommandParameter的命令:

<Button Name="OptionsBtn" ... Content="Options" 
     Command="{Binding ElementName=ElementsViewWindow, Path=DataContext.ShowOptionsMenuCommand}" 
     CommandParameter="{Binding}" />