gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:54:01 25 4
gpt4 key购买 nike

我有一个带有上下文菜单的数据网格,它使用多个菜单选项的命令。目前它只允许我一次在 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}" />

最佳答案

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

编辑:抱歉,我忘记了 SelectedItems 属性是只读的,未经修改就无法绑定(bind)。

山姆的 workaround您提到的应该完成这项工作,否则以下工作也可以:

想法是将 DataRow 的 IsSelected 绑定(bind)到 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>

View 模型:
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();
}
}

关于wpf - 如何在 DataGrid 中的多个选定行上执行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15412854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com