WPF DataGrid中的删除行按钮需要两次单击而不是一个



我有一个带有每个行删除按钮的数据,挂在删除命令上。需要单击两次按钮才能删除该行,这不是我想要的。

根据Snoop最初,根据按钮的IsEnabled == false,第一个点击可以启用它。这似乎是问题所在,那么如何在用户点击之前启用按钮?

我尝试使用触发器更改IsEnabled,在下面的代码中注释,它无效。

window1.xaml

<Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        <DataGrid HorizontalAlignment="Left" Height="399" Margin="10,10,0,0" 
                  VerticalAlignment="Top" Width="772"
                  x:Name="dataGrid1" 
                  ItemsSource="{Binding ProxyServers}" 
                  CanUserAddRows="True" 
                  CanUserDeleteRows="True"
                  >
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="SizeToCells">
                    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}" 
                            BasedOn="{StaticResource {x:Type DataGridCell}}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                                        <Button Command="DataGrid.DeleteCommand"  
                                            IsEnabled="True" x:Name="deleteButton" 
                                            Content="X">
                                           <!-- Make the button enable on mouse over?
                                                Didn't work.
                                                <Button.Style>
                                                <Style TargetType="Button">
                                                    <Style.Triggers>
                                                        <Trigger Property="IsMouseOver" 
                                                           Value="true">
                                                            <Setter Property="IsEnabled" 
                                                               Value="true" />
                                                        </Trigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Button.Style>-->
                                        </Button>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="IsEnabled" Value="True"/>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

window1.xaml.cs

using System.ComponentModel;
using System.Windows;

namespace WpfApp1
{
    public partial class Window1 : Window
    {
        BindingList<Proxy> proxyServers;
        public Window1()
        {
            InitializeComponent();
            dataGrid1.DataContext = this;
            proxyServers = new BindingList<Proxy>();
            proxyServers.Add(new Proxy() { LocalURL = "http://localhost" });
        }
        public BindingList<Proxy> ProxyServers { get => proxyServers; set => proxyServers = value; }
    }
    public class Proxy
    {
        string localURL;
        public string LocalURL { get => localURL; set => localURL = value; }
    }
}

您是否考虑过类似的东西:

    <DataGridTemplateColumn Header="Delete">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Command="{Binding Deletecommand}"

,如果您希望在父视图的daracontext中使用该命令,则可以使用私人。用命令参数传递绑定的行对象。

详细说明 @andy的答案。这就是我要做的。

  1. 添加relayCommand(此实现在互联网周围漂浮,我相信它来自mvvmlite(:

    using System;
    using System.Windows.Input;
    namespace WpfApp1
    {
    public class RelayCommand<T> : ICommand
    {
        #region Fields
        readonly Action<T> _execute = null;
        readonly Predicate<T> _canExecute = null;
        #endregion
        #region Constructors
        /// <summary>
        /// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
        /// </summary>
        /// <param name="execute">Delegate to execute when Execute is called on the command.  This can be null to just hook up a CanExecute delegate.</param>
        /// <remarks><seealso cref="CanExecute"/> will always return true.</remarks>
        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }
        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion
        #region ICommand Members
        ///<summary>
        ///Defines the method that determines whether the command can execute in its current state.
        ///</summary>
        ///<param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        ///<returns>
        ///true if this command can be executed; otherwise, false.
        ///</returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute((T)parameter);
        }
        ///<summary>
        ///Occurs when changes occur that affect whether or not the command should execute.
        ///</summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
        ///<summary>
        ///Defines the method to be called when the command is invoked.
        ///</summary>
        ///<param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to <see langword="null" />.</param>
        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }
        #endregion
    }
    }
    
  2. 定义我自己的DeleteCommand,而不是使用DataGrid的Build-In-In-In。我在代码方面做到了这一点,但是在我的真实项目中,它正在视图模型中。

    private RelayCommand<object> _DeleteCommand;
    public RelayCommand<object> DeleteCommand => _DeleteCommand ?? (_DeleteCommand = new RelayCommand<object>((object o) => {
        var proxy = o as Proxy;
        proxyServers.Remove(proxy);
    }, (object o) => {
        if (o is Proxy)
        {
            return true;
        }
        else
        {
            return false;
        }
    }));
    

我无法使用RelayCommand<Proxy>,因为新项目行不是代理对象。如果我没有新项目行,我可以使用RelayCommand<Proxy>

  1. 在XAML中删除此模板定义(因为它是ControlTemplate而不是DataTemplate,并且如果要以CommandParameter的形式将网格项目传递给命令,则似乎需要DataTemplate(

                    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate >
                                        <Button Command="{Binding DeleteCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                                CommandParameter="{Binding  RelativeSource={RelativeSource AncestorType={x:Type local:Proxy}}}"
                                                IsEnabled="True" x:Name="deleteButton" Content="X">
                                        </Button>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="IsEnabled" Value="True"/>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
    
  2. 添加此改为

            <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Command="{Binding DeleteCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                            CommandParameter="{Binding}"
                                            IsEnabled="True" x:Name="deleteButton" Content="X">
                        </Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
    

最新更新