C# 按钮命令绑定不';行不通

C# 按钮命令绑定不';行不通,c#,.net,wpf,C#,.net,Wpf,我创建了一个新的UserControl,里面有一个按钮。我希望像这样将button命令绑定到新用户控件的dependency属性 <Grid> <Button Name="Button1" Command="{Binding Button1Command}" /> </Grid> 当我尝试使用它时,当我按下按钮时,什么也没有发生。它无法识别该命令。如果我添加了一个事件,它就会工作。像这样: public static readonly Dependenc

我创建了一个新的UserControl,里面有一个按钮。我希望像这样将button命令绑定到新用户控件的dependency属性

<Grid>
 <Button Name="Button1" Command="{Binding Button1Command}" />
</Grid>
当我尝试使用它时,当我按下按钮时,什么也没有发生。它无法识别该命令。如果我添加了一个事件,它就会工作。像这样:

 public static readonly DependencyProperty Button1CommandProperty =
    DependencyProperty.Register("Button1Command", typeof(ICommand), typeof(BptCellTemplate), new FrameworkPropertyMetadata(null, OnButton1CommandChanged));

private static void OnButton1CommandChanged(DependencyObject dependencyObject,
                                              DependencyPropertyChangedEventArgs args)
{
  var bptCellTemplate = dependencyObject as BptCellTemplate;
  if (bptCellTemplate == null || !(args.NewValue is ICommand))
  {
    return;
  }
  (bptCellTemplate.DataContext as BptCellTemplateViewModel).Button1Command = (ICommand)args.NewValue;

}
有没有一种方法可以在没有事件的情况下绑定它?因为它与其他按钮属性一起工作,我也用同样的方法(例如,
Visibility

  • 您需要类实现ICommand接口

    public class RelayCommand : ICommand
    {
        #region Fields
    
        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;
    
        #endregion // Fields
    
        #region Constructors
    
        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<object> 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<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
    
            _execute = execute;
            _canExecute = canExecute;
        }
    
        #endregion // Constructors
    
        #region ICommand Members
    
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    
        #endregion // ICommand Members
    }
    
  • 就这样

  • 您需要类实现ICommand接口

    public class RelayCommand : ICommand
    {
        #region Fields
    
        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;
    
        #endregion // Fields
    
        #region Constructors
    
        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<object> 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<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
    
            _execute = execute;
            _canExecute = canExecute;
        }
    
        #endregion // Constructors
    
        #region ICommand Members
    
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    
        #endregion // ICommand Members
    }
    

  • 就这样

    您的绑定可能不起作用,因为没有说明
    Button1Command
    属性是
    UserControl
    的成员

    在VisualStudio中调试程序时,通过查看输出窗口可以确认这是问题所在。您可能会看到未找到成员
    Button1Command
    的绑定错误

    典型的修复方法是将name属性添加到
    UserControl
    的根元素中,例如
    x:name=“root”
    (您可以选择自己的名称或使用现有名称(如果有的话)。然后,将绑定更改为引用新名称的命令:

    <Button Name="Button1" Command="{Binding Button1Command, ElementName=root}" />
    

    您的绑定可能不起作用,因为没有说明
    按钮1命令
    属性是
    用户控件
    的成员

    在VisualStudio中调试程序时,通过查看输出窗口可以确认这是问题所在。您可能会看到未找到成员
    Button1Command
    的绑定错误

    典型的修复方法是将name属性添加到
    UserControl
    的根元素中,例如
    x:name=“root”
    (您可以选择自己的名称或使用现有名称(如果有的话)。然后,将绑定更改为引用新名称的命令:

    <Button Name="Button1" Command="{Binding Button1Command, ElementName=root}" />
    
    
    
    Button1Command的值是在哪里设置的?Button1Command的值是在哪里设置的?值得注意的是,在大多数情况下,您可以简单地使用
    RoutedCommand
    :您不需要创建自己的
    ICommand
    类。值得注意的是,在大多数情况下,您可以简单地使用
    RoutedCommand
    创建自己的
    ICommand
    类。