[英]Button command binding doesn't work
我創建了一個新的UserContol,里面有一個按鈕。 想要像這樣將button命令綁定到新用戶控件的依賴性屬性。
<Grid>
<Button Name="Button1" Command="{Binding Button1Command}" />
</Grid>
這是包含UserControl的DP:
public ICommand Button1Command
{
get { return (ICommand)GetValue(Button1CommandProperty); }
set { SetValue(Button1CommandProperty, value); }
}
public static readonly DependencyProperty Button1CommandProperty =
DependencyProperty.Register("Button1Command", typeof(ICommand), typeof(BptCellTemplate), new FrameworkPropertyMetadata(null));
當我嘗試使用它時,當我按下按鈕時什么也沒有發生。 它無法識別該命令。 如果我添加一個事件,它將起作用。 像這樣:
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
)
您的綁定可能無法正常工作,因為沒有任何內容表明Button1Command
屬性是UserControl
的成員。
在Visual Studio中調試程序時,可以通過在“輸出”窗口中查看來確認這是問題所在。 您可能會看到未找到成員Button1Command
綁定錯誤。
通常的解決方法是在UserControl
的根元素中添加名稱屬性,例如x:Name="root"
(您可以選擇自己的名稱,或者使用已有的名稱)。 然后,更改對命令的綁定以引用新名稱:
<Button Name="Button1" Command="{Binding Button1Command, ElementName=root}" />
您需要類實現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 }
現在綁定非常簡單。 在DataContext(MVVM ... ect。)中定義命令。別記得設置DataContext ...,例如DataContext = this; (這是您的窗口)
RelayCommand _btnCommand; public ICommand Button1Command { get { if (_btnCommand == null) { _btnCommand = new RelayCommand(param => this.ExecuteButton1(), param => this.CanButton1()); } return _btnCommand; } } public void ExecuteButton1() { } public bool CanButton1() { return true; }
而已 ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.