gpt4 book ai didi

c# - 属性更改时未调用按钮命令 CanExecute

转载 作者:太空狗 更新时间:2023-10-29 22:26:12 25 4
gpt4 key购买 nike

我有一个带有文本框和按钮的表单。

当该文本框的值发生变化时,按钮命令不会调用其命令的 CanExecute 方法。

命令参数已设置但似乎没有更改。加载窗口后,该按钮保持禁用状态。

<TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Save" Command="{Binding SaveChangesCommand}" CommandParameter="{Binding Name}" />

我知道绑定(bind)正在工作,因为我创建了一个接收目标绑定(bind)并在绑定(bind)更改时引发 CanExecute 的行为。

有了这个行为,CanExecute 就会被正常调用。

<Button Content="Save" Command="{Binding SaveChangesCommand}">
<i:Interaction.Behaviors>
<behaviors:CallCommandCanExecuteWhenBindingChange Target="{Binding Name}" />
</i:Interaction.Behaviors>
</Button>

View 模型:

public class EditViewModel : INotifyPropertyChanged
{
private string _name;

public EditViewModel()
{
SaveChangesCommand = new DelegateCommand(p => SaveChanges(), p => CanSaveChanges());
}

public string Name
{
get { return _name; }
set
{
if (value == _name) return;
_name = value;
OnPropertyChanged();
}
}

public DelegateCommand SaveChangesCommand { get; private set; }

private void SaveChanges()
{
}
private bool CanSaveChanges()
{
return !string.IsNullOrWhiteSpace(Name);
}
}

委托(delegate)命令:

public interface IBaseCommand : ICommand
{
void OnCanExecuteChanged();
}

public class DelegateCommand : IBaseCommand
{
private readonly Action<object> _execute;
private readonly Func<object, bool> _canExecute;

public DelegateCommand(Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}

public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)
{
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
OnCanExecuteChanged();
}

public void OnCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
}

CallCommandCanExecuteWhenBindingChange:

public class CallCommandCanExecuteWhenBindingChange : Behavior<FrameworkElement>
{
public static readonly DependencyProperty<CallCommandCanExecuteWhenBindingChange, object> TargetProperty;
private ICommandBase _command;

static CallCommandCanExecuteWhenBindingChange()
{
var dependency = new DependencyRegistry<CallCommandCanExecuteWhenBindingChange>();

TargetProperty = dependency.Register(b => b.Target, s => s.OnTargetChange());
}

public object Target
{
get { return TargetProperty.Get(this); }
set { TargetProperty.Set(this, value); }
}

private void OnTargetChange()
{
if (_command == null && AssociatedObject != null)
{
var field = AssociatedObject.GetType().GetProperty("Command");
_command = (IBaseCommand)field.GetValue(AssociatedObject);
}

if (_command != null)
_command.OnCanExecuteChanged();
}
}

有谁知道为什么按钮不调用 CanExecute?

最佳答案

在您的 DelegateCommand 实现中,CanExecuteChanged 应该添加/删除到 CommandManager.RequerySuggested事件

Occurs when the CommandManager detects conditions that might change the ability of a command to execute.

改成

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

关于c# - 属性更改时未调用按钮命令 CanExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28343632/

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