2013-03-10 50 views
0

我的命令在我的ViewModel中。我需要传递我的Menuitem嵌套的DataGrid。 这怎么可能?将CommandParameter绑定到本地项目,而DataContext是ViewModel

<MenuItem Header="Delete item/s" Command="{Binding RemoveHistoryItem}" CommandParameter="{Binding ElementName=HistoryDataGrid}"> 
+1

数据网格是一个UI元素,你不能在你的视图模型的UI元素进行交互。你想在CommandParameter中发送一个'HistoryItem'。 – Khan 2013-03-10 19:28:20

+0

也许... SelectedItem集合。对于不同的MenuItem项目的总数。但它仍然是同样的问题。 – 2013-03-10 19:38:01

+1

也许这可能工作。 ... = {{Binding ElementName = HistoryDataGrid,Path = DataContext}“... – failedprogramming 2013-03-10 20:51:22

回答

1

您绝对不应该将UIElement发回您的ViewModel(如Jeffery所述)。 尝试将所需的DataGrid属性绑定到ViewModel,然后在命令处理程序中访问它们。

我用一个按钮来举例说明,但是这对你的MenuItem也同样适用。

视图模型:

public class ViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    public ICommand RemoveHistoryItemCommand { get; private set; } 

    private HistoryItem _selectedHistoryItem; 
    public HistoryItem SelectedHistoryItem { get { return _selectedHistoryItem; } set { _selectedHistoryItem = value; OnPropertyChanged("SelectedHistoryItem"); } } 

    private ObservableCollection<HistoryItem> _historyItems = new ObservableCollection<HistoryItem>(); 
    public ObservableCollection<HistoryItem> HistoryItems { get { return _historyItems; } set { _historyItems = value; OnPropertyChanged("HistoryItems"); } } 


    public ViewModel() 
    { 
     this.RemoveHistoryItemCommand = new ActionCommand(RemoveHistoryItem); 

     this.HistoryItems.Add(new HistoryItem() { Year = "1618", Description = "Start of war" }); 
     this.HistoryItems.Add(new HistoryItem() { Year = "1648", Description = "End of war" }); 
    } 

    // command handler 
    private void RemoveHistoryItem() 
    {    
     if (this.HistoryItems != null) 
     { 
      HistoryItems.Remove(this.SelectedHistoryItem); 
     } 
    } 
} 

public class HistoryItem 
{ 
    public string Year { get; set; } 
    public string Description { get; set; } 
} 

public class ActionCommand : ICommand 
{ 
    public event EventHandler CanExecuteChanged; 
    public Action _action; 
    public ActionCommand(Action action) 
    { 
     _action = action; 
    } 

    public bool CanExecute(object parameter) { return true; } 

    public void Execute(object parameter) 
    { 
     if (CanExecute(parameter)) 
      _action(); 
    } 
} 

的XAML:

<Window.DataContext> 
    <local:ViewModel /> 
</Window.DataContext> 


<StackPanel> 
    <DataGrid ItemsSource="{Binding HistoryItems}" SelectedItem="{Binding SelectedHistoryItem, Mode=TwoWay}" /> 
    <Button Content="Remove selected item" Command="{Binding RemoveHistoryItemCommand}" HorizontalAlignment="Left" /> 
</StackPanel> 
+0

只是为了完成答案 - @ abc这是正确的方法 - 即使用相同的'视图模型'来支持两个控件(或同一个“大”视图模型的一部分),然后在幕后进行沟通。如果您确实需要发送一些gui元素 - 请使用[[Binding ElementName = Path = DataContext.Property ...]]作为@failedprogramming提及的内容。 – NSGaga 2013-03-10 21:12:17