gpt4 book ai didi

c# - Key Input Bindings 与 CommandParameter 用法伯顿绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:03 26 4
gpt4 key购买 nike

以下两个元素在我的 ICommand 实现中以不同方式触发并导致问题。当实现为TextBox输入CanExecuteChanged(object parameter)时,parameter的值为null。当它为Button 进入同样的方法时,parameter 的值等于CommandParameter。

理想情况下,在这两种情况下,我都希望 CommandParameter 值不发送到 CanExecuteChanged,而只发送到 Execute。

ICommand 的实现

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


public bool CanExecute(object parameter)
{
if (parameter is bool)
{
this.canExecute = (bool)parameter;
}

return this.canExecute;
}

public void Execute(object parameter)
{
this.executeAction((T)parameter);
}


internal void RaiseCanExecuteChanged()
{
this.OnCanExecuteChanged();
}


private void OnCanExecuteChanged()
{
if (this.canExecuteChanged != null)
{
this.canExecuteChanged(this, EventArgs.Empty);
}
}

文本框

<TextBox Width="80" Margin="2,2,2,2" Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}" MaxLength="25">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding SearchCommand}">
<KeyBinding.CommandParameter>
<s:Boolean>True</s:Boolean>
</KeyBinding.CommandParameter>
</KeyBinding>
</TextBox.InputBindings>
</TextBox>

按钮

<Button Margin="2,2,2,2" Padding="10,0,10,0" Content="Search">
<Button.InputBindings>
<MouseBinding Command="{Binding SearchCommand }" MouseAction="LeftClick">
<MouseBinding.CommandParameter>
<s:Boolean>True</s:Boolean>
</MouseBinding.CommandParameter>
</MouseBinding>
</Button.InputBindings>
</Button>

最佳答案

在这种情况下,尝试@JoshSmithICommand 实现,对我来说这两个选项都很好:

中继命令

public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;

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

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

_execute = execute;
_canExecute = canExecute;
}

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);
}
}

搜索命令

private RelayCommand _searchCommand = null;

public ICommand SearchCommand
{
get
{
if (_searchCommand == null)
{
_searchCommand = new RelayCommand(param => this.Search(param), param => true);
}

return _searchCommand;
}
}

private void Search(object param)
{
bool parameter = (bool)param;

if (parameter)
{
MessageBox.Show("Pressed the Enter Key");
}
}

关于c# - Key Input Bindings 与 CommandParameter 用法伯顿绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22262986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com