I created a TextBox that implements ICommandSource, for the most part I have followed Microsoft's example using Slider . This TextBox will execute the associated command after pressing the Enter key. This part of the behavior is working correctly, the problem I am facing is that IsEnabled can no longer be installed in XAML? I'm not sure why this is so, you can still install IsEnabled on native Microsoft classes such as Button.
public class CommandTextBox : TextBox, ICommandSource
{
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTextBox), new PropertyMetadata(OnCommandChanged));
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(CommandTextBox));
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); }
}
private static void OnCommandChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
CommandTextBox ctb = (CommandTextBox)dp;
ctb.HookUpCommand((ICommand)e.OldValue, (ICommand)e.NewValue);
}
private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
{
if (oldCommand != null)
RemoveCommand(oldCommand);
AddCommand(newCommand);
}
private void RemoveCommand(ICommand command)
{
EventHandler handler = CanExecuteChanged;
command.CanExecuteChanged -= handler;
}
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 (command != null)
{
if (command.CanExecute(CommandParameter, CommandTarget))
this.IsEnabled = true;
else
this.IsEnabled = false;
}
else
{
if (Command.CanExecute(CommandParameter))
this.IsEnabled = true;
else
this.IsEnabled = false;
}
}
}
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?
Any feedback would be appreciated.
source
share