I want to make ComboBoxItem with ICommandSource.
However, not possible to automatic of IsEnabled by CanExecute set it.
I want to reflect the result of CanExecute in IsEnabled like Button.
How should I do
class MyComboBoxItem : ComboBoxItem, ICommandSource
{
void OnCanExecuteChanged(object sender, EventArgs e)
{
IsEnable = command.CanExecute(null); // Here is not called.
}
ICommand command;
public ICommand Command
{
get { return command; }
set {
command = value;
if ( command != null ) {
command.CanExecuteChanged += OnCanExecuteChanged;
}
}
}
public object CommandParameter { return null; }
public IInputElement CommandTarget { return null; }
}

ComboBox was tried.
ethanpo
ComboBox was tried.
However, the result of the intention was not obtained though it tried by the
following source.
Even if the ChangeEnabled button is pushed, Enabled of ComboBox is not changed.
The Button was changed.
I want to behave as much as this Button in MyComboBox.
How should I do
<Window x:Class="MyProj.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProj"
>
<StackPanel>
<Button Click="OnEnabledChangeClick">ChangeEnabled</Button>
<Button Command="local:Window1.TestCommand01">Button</Button>
<local:MyComboBox Command="local:Window1.TestCommand02">
<ComboBoxItem>Item</ComboBoxItem>
</local:MyComboBox>
</StackPanel>
</Window>
public partial class Window1 : Window
{
public readonly static RoutedUICommand TestCommand01 =
new RoutedUICommand("TestCommand0_1", "TestCommand01", typeof(Window1));
public readonly static RoutedUICommand TestCommand02 =
new RoutedUICommand("TestCommand0_2", "TestCommand02", typeof(Window1));
public Window1()
{
InitializeComponent();
CommandBindings.Add( new CommandBinding( TestCommand01, OnTestCommand01, OnCanTestCommand01 ) );
CommandBindings.Add( new CommandBinding( TestCommand02, OnTestCommand02, OnCanTestCommand02 ) );
}
bool canExecute1 = true;
bool canExecute2 = true;
void OnCanTestCommand01(object sender, CanExecuteRoutedEventArgs e)
{ e.CanExecute = canExecute1; }
void OnCanTestCommand02(object sender, CanExecuteRoutedEventArgs e)
{ e.CanExecute = canExecute2; }
void OnTestCommand01(object sender, ExecutedRoutedEventArgs e)
{}
void OnTestCommand02(object sender, ExecutedRoutedEventArgs e)
{}
void OnEnabledChangeClick(object sender, RoutedEventArgs e)
{
canExecute1 = !canExecute1;
canExecute2 = !canExecute2;
CommandManager.InvalidateRequerySuggested();
}
}
class MyComboBox : ComboBox, ICommandSource
{
void OnCanExecuteChanged(object sender, EventArgs e)
{
UpdateCanExecute();
}
void UpdateCanExecute()
{
RoutedCommand routedCommand = (RoutedCommand)command;
IsEnabled = routedCommand.CanExecute(CommandParameter, CommandTarget);
}
ICommand command;
public ICommand Command
{
get { return command; }
set {
command = value;
if ( command != null ) {
command.CanExecuteChanged += OnCanExecuteChanged;
UpdateCanExecute();
}
}
}
public object CommandParameter { get {return null; } }
public IInputElement CommandTarget { get {return null;} }
}
arunagopal
It solved it by sample (ImplementICommandSource) that existed in WinFX SDK beta2.
// add the command
private void AddCommand(ICommand oldCommand, ICommand newCommand)
{
EventHandler handler = new EventHandler(CanExecuteChanged);
canExecuteChangedHandler = handler;
if (newCommand != null)
{
newCommand.CanExecuteChanged += canExecuteChangedHandler;
}
}
//keep a copy of the handler so it doesn't get garbage collected
private static EventHandler canExecuteChangedHandler;