gpt4 book ai didi

c# - WPF:在不破坏 IsEnabled 的情况下实现 ICommandSource

转载 作者:太空狗 更新时间:2023-10-30 01:18:27 24 4
gpt4 key购买 nike

我已经创建了一个实现 ICommandSource 的 TextBox,在大多数情况下,我使用 Slider 遵循 Microsoft 的示例。 .此 TextBox 将在按下“Enter”键时执行绑定(bind)的命令。这部分行为正常工作,我遇到的问题是无法再在 XAML 中设置 IsEnabled?我不确定这是为什么,您仍然可以在 Button 等 native Microsoft 类上设置 IsEnabled。

public class CommandTextBox : TextBox, ICommandSource
{
// The DependencyProperty for the Command.
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTextBox), new PropertyMetadata(OnCommandChanged));

// The DependencyProperty for the CommandParameter.
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(CommandTextBox));

// The DependencyProperty for the CommandTarget.
public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandTextBox));

public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}

public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}

public bool CommandResetsText { get; set; }

public IInputElement CommandTarget
{
get { return (IInputElement)GetValue(CommandTargetProperty); }
set { SetValue(CommandTargetProperty, value); }
}

// Command dependency property change callback.
private static void OnCommandChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
CommandTextBox ctb = (CommandTextBox)dp;
ctb.HookUpCommand((ICommand)e.OldValue, (ICommand)e.NewValue);
}

// Add a new command to the Command Property.
private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
{
// If oldCommand is not null, then we need to remove the handlers.
if (oldCommand != null)
RemoveCommand(oldCommand);

AddCommand(newCommand);
}

// Remove an old command from the Command Property.
private void RemoveCommand(ICommand command)
{
EventHandler handler = CanExecuteChanged;
command.CanExecuteChanged -= handler;
}

// Add the command.
private void AddCommand(ICommand command)
{
var handler = new EventHandler(CanExecuteChanged);
canExecuteChangedHandler = handler;
if (command != null)
command.CanExecuteChanged += canExecuteChangedHandler;
}

private void CanExecuteChanged(object sender, EventArgs e)
{
if (Command != null)
{
RoutedCommand command = Command as RoutedCommand;

// If a RoutedCommand.
if (command != null)
{
if (command.CanExecute(CommandParameter, CommandTarget))
this.IsEnabled = true;
else
this.IsEnabled = false;
}
// If a not RoutedCommand.
else
{
if (Command.CanExecute(CommandParameter))
this.IsEnabled = true;
else
this.IsEnabled = false;
}
}
}

// If Command is defined, pressing the enter key will invoke the command;
// Otherwise, the textbox will behave normally.
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);

if (e.Key == Key.Enter)
{
if (Command != null)
{
RoutedCommand command = Command as RoutedCommand;

if (command != null)
command.Execute(CommandParameter, CommandTarget);
else
Command.Execute(CommandParameter);

if (CommandResetsText)
this.Text = String.Empty;
}
}
}

private EventHandler canExecuteChangedHandler;
}

XAML

<Button Command="{Binding GenericCommand}" IsEnabled="False" /> // This is disabled(desired).
<CommandTextBox Command="{Binding GenericCommand}" IsEnabled="False" /> // This is enabled?

如有任何反馈,我们将不胜感激。

最佳答案

首先,您需要创建一个私有(private)属性或一个字段,如果命令可以执行将被设置:

private bool _canExecute;

其次,您需要覆盖IsEnabledCore 属性:

protected override bool IsEnabledCore
{
get
{
return ( base.IsEnabledCore && _canExecute );
}
}

最后,您只需将 IsEnabled 替换为 _canExecute 并强制执行 IsEnabledCore 即可修复 CanExecuteChanged 方法:

private void CanExecuteChanged(object sender, EventArgs e)
{
if (Command != null)
{
RoutedCommand command = Command as RoutedCommand;

// If a RoutedCommand.
if (command != null)
{
_canExecute = command.CanExecute(CommandParameter, CommandTarget);
}
// If a not RoutedCommand.
else
{
_canExecute = Command.CanExecute(CommandParameter);
}
}

CoerceValue(UIElement.IsEnabledProperty);
}

关于c# - WPF:在不破坏 IsEnabled 的情况下实现 ICommandSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409988/

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