4

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

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

<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}" />
4

1 回答 1

2

[将 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();
    }
}
于 2013-03-14T15:13:31.687 回答