如何在DataGrid中的多个选定行上执行命令?

时间:2013-03-14 15:04:38

标签: wpf command

我有一个带有上下文菜单的数据网格,使用命令输入多个菜单选项。目前它只允许我一次在一行上运行命令,我希望能够在单击菜单项一次时运行命令所有选定的行。我想在使用CanExecute启用/禁用命令时使用命令,但如果没有其他选项,我将使用Click

这是当前实现,一次只允许一行。

<DataGrid.RowStyle>
   <Style TargetType="{x:Type DataGridRow}">
      <Setter Property="ContextMenu">
         <Setter.Value>
            <ContextMenu>
               <MenuItem Visibility="Visible" 
                         Header="{Binding Path=ApplicationStrings.LaunchItemLabel,
                                          Source={StaticResource ResourceWrapper}}" 
                         Command="{StaticResource launchCommand}" 
                         CommandParameter="{Binding}" />

1 个答案:

答案 0 :(得分:1)

[将DataGrid的SelectedItems属性绑定到ViewModel属性,然后在launchCommand中访问它,而不是使用CommandParameter传回SelectedItem。 ]

编辑:道歉,我忘记了SelectedItems属性是只读的,没有修改就无法绑定。

你提到的Sam workaround应该做的工作,否则以下工作也是如此:

我们的想法是将DataRow的IsSelected绑定到DataItem的属性(在我的示例中为MyClass),然后在命令处理程序中迭代集合并检查IsSelected属性。

的Xaml:

<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();
    }
}