【问题标题】:Clear selected row data in wpf datagrid?清除wpf datagrid中的选定行数据?
【发布时间】:2014-05-13 10:09:12
【问题描述】:

大家好,我是 wpf 的新手,我曾一度陷入困境。我想通过选择该行来删除 wpf 行中的数据网格行。直到现在我已经搜索了很多并编写了以下代码,但都是徒劳的。 代码是:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        var g1 = dg;
        var g2 = dg;

        if (g1.SelectedIndex >= 0)
        {
            for (int i = 0; i <= g1.SelectedItems.Count; i++)
            {
                g2.Items.Remove(g1.SelectedItems[i]);
            }
        }
        g1 = g2;
    }

任何建议都会非常受欢迎。

【问题讨论】:

  • Datagrid的来源是什么。
  • ItemsSource="{Binding vlan}"

标签: c# wpf datagrid


【解决方案1】:

我看到您正在处理按钮单击事件,大概是在窗口/控件后面的代码中。这会在 UI 元素和背后的逻辑之间产生强烈的耦合,从而使测试变得更加困难。

我会考虑使用MVVM pattern 并在视图模型中执行此类操作。使用这种方法可以减少 UI(视图)和逻辑(视图模型)之间的耦合。

我已经组装了一个非常简单的应用程序(针对 .Net 4.5)来演示将集合绑定到数据网格以及如何删除选定的行。

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="350">
    <Grid>
        <StackPanel>
            <DataGrid ItemsSource="{Binding People}" SelectedItem="{Binding SelectedItem}" />
            <Button Command="{Binding DeleteCommand}">Delete Selected Row</Button>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new MainViewModel();
        }
    }

Person.cs

using System;

namespace WpfApplication1
{
    public class Person
    {
        public string Forename { get; set; }

        public string Surname { get; set; }
    }
}

DelegateCommand.cs

using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public class DelegateCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        public event EventHandler CanExecuteChanged;

        public DelegateCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public DelegateCommand(Action<object> execute,
                       Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }
            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
}

MainViewModel.cs

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace WpfApplication1
{
    internal class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public MainViewModel()
        {
            People = new ObservableCollection<Person>();
            DeleteCommand = new DelegateCommand(x => DeleteSelectedItem(null));

            People.Add(new Person { Forename = "Bob", Surname = "Smith" });
            People.Add(new Person { Forename = "Alice", Surname = "Jones" });
        }

        private void DeleteSelectedItem(object obj)
        {
            People.Remove(SelectedItem);
            SelectedItem = null;
        }

        public ICommand DeleteCommand { get; set; }

        public ObservableCollection<Person> People { get; set; }

        private Person selectedItem;

        public Person SelectedItem
        {
            get { return selectedItem; }
            set
            {
                if (selectedItem == value)
                    return;

                selectedItem = value;
                OnPropertyChanged();
            }
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

背后的代码

MainWindow.xaml.cs

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();

        DeleteCommand = new DelegateCommand(x => DeleteSelectedItem(null));
        People = new ObservableCollection<Person>();
        DataContext = this;

        Loaded += (sender, args) => PopulateCollection();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void PopulateCollection()
    {
        People.Add(new Person { Forename = "Bob", Surname = "Smith" });
        People.Add(new Person { Forename = "Alice", Surname = "Jones" });
    }

    private void DeleteSelectedItem(object obj)
    {
        People.Remove(SelectedItem);
        SelectedItem = null;
    }

    public ICommand DeleteCommand { get; set; }

    public ObservableCollection<Person> People { get; set; }

    private Person selectedItem;

    public Person SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (selectedItem == value)
                return;

            selectedItem = value;
            OnPropertyChanged();
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

【讨论】:

  • 是否有可能在后面的代码中执行此操作,因为我是 wpf 的新手,所以我对 MVVM 没有广泛的了解!
  • 当然可以。我已经编辑了答案以显示方法背后的代码。只需使用我刚刚添加到答案末尾的 MainWindow.xaml.cs 代码,不要使用 MainViewModel.cs。
猜你喜欢
  • 2014-12-17
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2023-03-24
  • 2012-11-04
  • 2020-10-03
  • 1970-01-01
  • 2013-11-29
相关资源
最近更新 更多