[将 DataGrid 的 SelectedItems 属性绑定到 ViewModel 属性,然后在您的 launchCommand 中访问该属性,而不是使用 CommandParameter 传回您的 SelectedItem。]
编辑:抱歉,我忘记了 SelectedItems 属性是只读的,未经修改就无法绑定。
您提到的 Sam 的解决方法应该可以完成这项工作,否则以下方法也可以:
想法是将 DataRow 的 IsSelected 绑定到 DataItem 的属性(在我的示例中为 MyClass),然后在命令处理程序中,遍历集合并检查 IsSelected 属性。
xml:
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Window.Resources>
<local:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding MyRows}" >
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Visibility="Visible" Header="Launch"
Command="{Binding Data.LaunchCommand, Source={StaticResource DataContextProxy}}" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
</DataGrid>
</Grid>
视图模型:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<MyClass> _myRows = new ObservableCollection<MyClass>();
public ObservableCollection<MyClass> MyRows { get { return _myRows; } set { _myRows = value; OnPropertyChanged("MyRuns"); } }
private ICommand _launchCommand;
public ICommand LaunchCommand { get { return _launchCommand; } private set { _launchCommand = value; OnPropertyChanged("LaunchCommand"); } }
public ViewModel()
{
MyRows = new ObservableCollection<MyClass>()
{
new MyClass() { Text = "Example Line 1" },
new MyClass() { Text = "Example Line 2" },
new MyClass() { Text = "Example Line 3" }
};
LaunchCommand = new ActionCommand(Launch);
}
private void Launch()
{
foreach (var row in MyRows)
{
if (row.IsSelected)
{
//...
}
}
}
}
public class MyClass : INotifyPropertyChanged
{
private string _text;
public string Text { get { return _text; } set { _text = value; OnPropertyChanged("Text"); } }
private bool _isSelected;
public bool IsSelected { get { return _isSelected; } set { _isSelected = value; OnPropertyChanged("IsSelected"); } }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
public class BindingProxy : Freezable
{
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
public object Data { get { return (object)GetValue(DataProperty); } set { SetValue(DataProperty, value); } }
public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
public class ActionCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public bool CanExecute(object parameter) { return true; }
public void Execute(object parameter)
{
if (_action != null)
_action();
}
}