如何使用具有固定列的删除键删除datagrid中的选定行

    I have used code with command.manager event but it didn't delete the row if i use key down event it executes but it didn't delete the row in data grid.

<Window x:Class="datagridcheck.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="1336" Width="937">
    <Grid  Name="grid1" Margin="10,10,135,10">
        <DataGrid x:Name="dgUsers" SelectionMode="Single" SelectionUnit="CellOrRowHeader" CommandManager.PreviewExecuted="DriversDataGrid_PreviewDeleteCommandHandler"  Margin="174,0,346,0" GridLinesVisibility="None" MinColumnWidth="50" FontFamily="Times New Roman" HorizontalGridLinesBrush="#FFF70000" VerticalGridLinesBrush="#FF0017FF" RowBackground="#FFF5F2D4" FontSize="18" TextOptions.TextHintingMode="Animated" TextOptions.TextFormattingMode="Display" RowDetailsVisibilityMode="Visible" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Top" Width="264" Height="483" IsSynchronizedWithCurrentItem="False" SelectedIndex="0" PreviewKeyDown="dgUsers_PreviewKeyDown"   >
                        <DataGrid.Columns>
                    <DataGridTextColumn Header="FoodName" Width="90" Binding="{Binding FoodName}" MinWidth="90" Foreground="#FFFB1005" FontFamily="Times New Roman"/>
                <DataGridTextColumn Header="Quantity" Width="90" Binding="{Binding Birthday}" MinWidth="90" Foreground="Red" FontFamily="Times New Roman" />
                <DataGridTextColumn Binding="{Binding Id}" ClipboardContentBinding="{x:Null}" Header="Price" MinWidth="90" Width="90"/>
                </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" Value="LightBlue" />
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Blue"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="443,378,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <WrapPanel Height="355" Margin="443,0,0,0" VerticalAlignment="Top" Name="wrap1" HorizontalAlignment="Left" Width="331">
            <WrapPanel Height="100" Width="100"/>
        </WrapPanel>
        <Button Content="Button" HorizontalAlignment="Left" Margin="443,428,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
                <GridView>
                    <GridViewColumn Header="FoodName" Binding="{Binding FoodName}"/>
                    <GridViewColumn Header="Birthday" />
                </GridView>
    </Grid>
</Window>

命名空间datagridcheck {公共部分类MainWindow:窗口{公共MainWindow(){InitializeComponent(); } int i = 0; private void Button_Click(object sender,RoutedEventArgs e){Button bt = new Button(); bt.Name =“test”; bt.Content =“eachtime”+ i.ToString(); bt.Width = 50; bt.Height = 50; 我++; wrap1.Children.Add(BT); bt.Click + = new RoutedEventHandler(this.bt_Click); } void bt_Click(object sender,RoutedEventArgs e){Button buts = sender as Button; dgUsers.Items.Add(new User(){Id = 1,FoodName = Convert.ToString(buts.Content),Birthday = Convert.ToString(buts.Name)}); } private void DriversDataGrid_PreviewDeleteCommandHandler(object sender,ExecutedRoutedEventArgs e){if(e.Command == DataGrid.DeleteCommand){if(!(MessageBox.Show(“您确定要删除吗?”,“请确认。”,MessageBoxButton .YesNo)== MessageBoxResult.Yes)){e.Handled = true; }}}} public class User {public int Id {get; 组; }公共字符串FoodName {get; 组; } public string Birthday {get; 组; }}}


快速MVVM =模型(数据)+视图(屏幕)+ ViewModel(代码+逻辑)

第一个XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="gd"  CanUserDeleteRows="False"  ItemsSource="{Binding DataVMs}" SelectedItem="{Binding SelectedItemInVM, Mode=TwoWay}" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True">

            <DataGrid.InputBindings>
                <KeyBinding Gesture="Delete" Key="Delete" Command="{Binding DeleteCommand, Mode=OneWay}"  CommandParameter="{Binding Path=SelectedItem, ElementName=gd, Mode=OneWay}"/>
            </DataGrid.InputBindings>

            <DataGrid.Columns>
                <DataGridTextColumn Header="One" Binding="{Binding Un}" />
                <DataGridTextColumn Header="Two" Binding="{Binding Deux}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

在MainWindow的代码背后添加这个,假设DataViewModel包装你的模型:

 public MainWindow()
        {
            InitializeComponent();

            //Initialize DataContext with some Data
            var dataContext = new DataViewCollectionModel
            {
                DataVMs = new ObservableCollection<DataViewModel>()

            };
            dataContext.DataVMs.Add(new DataViewModel { Un = 1, Deux = 2 });
            dataContext.DataVMs.Add(new DataViewModel { Un = 10, Deux = 20 });
            dataContext.DataVMs.Add(new DataViewModel { Un = 100, Deux = 200 });

            //Assign
            DataContext = dataContext;
        }

现在创建您ViewModels:

1)BindableObject处理通知,它是所有Viewmodels的基类:

 public class BindableObject : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

    }

DataViewCollectionModel是你传递给View的DataContext的东西(在这种情况下是MainWindow)。 它必须通知更改。 这就是为什么它继承了BindableObject。 它有一个DataViewModel的列表。

class DataViewCollectionModel : BindableObject
{
    private ObservableCollection<DataViewModel> dataVMs;
    private DataViewModel selectedItemInVM;
    public ObservableCollection<DataViewModel> DataVMs
    {
        get { return dataVMs; }
        set
        {
            if (dataVMs != value)
            {
                dataVMs = value;
                NotifyPropertyChanged("DataVMs");
            }
        }
    }

    public DataViewModel SelectedItemInVM
    {
        get { return selectedItemInVM; }
        set
        {
            if (selectedItemInVM != value)
            {
                selectedItemInVM = value;
                NotifyPropertyChanged("SelectedItemInVM");
            }
        }
    }

    private RelayCommand<DataViewModel> deleteCommand;
    public RelayCommand<DataViewModel> DeleteCommand
    {
        get { return deleteCommand ?? (deleteCommand = new RelayCommand<DataViewModel>(d => Delete(d))); }
    }

    private void Delete(DataViewModel d)
    {
        DataVMs.Remove(selectedItemInVM);
    }

}

最后,DataViewModel代表DataGrid中的每一行。

 public class DataViewModel : BindableObject
    {

        public int Un { get; set; }
        public int Deux { get; set; }

    }

因为我非常友好,我给你一个处理命令的助手:

   public class RelayCommand<T> : ICommand
    {

        readonly Action<T> _execute = null;
        readonly Predicate<T> _canExecute = null;


        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute((T)parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

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

    }

当您选择一个行时,只需单击Delete键即可进行测试。

希望能帮助到你。

链接地址: http://www.djcxy.com/p/46579.html

上一篇: how to delete selected row in datagrid using delete key with fixed column

下一篇: WPF Binding not updating from DispatcherTimer