What should you do o ensure that the MenuItem control is disabled and the command is not executable?

You are creating a Windows Presentation Foundation application by using Microsoft .NET Framework 3.5.
You add a CommandBinding element to the Window element. The command has
a keyboard gesture CTRL+H. The Window contains the following MenuItem control.
<MenuItem Header="Highlight Content" Command="local:CustomCommands.Highlight" />
You need to ensure that the MenuItem control is disabled and the command is not executable when the focus shifts to a TextBox control that does not contain any text.
What should you do?

You are creating a Windows Presentation Foundation application by using Microsoft .NET Framework 3.5.
You add a CommandBinding element to the Window element. The command has
a keyboard gesture CTRL+H. The Window contains the following MenuItem control.
<MenuItem Header=”Highlight Content” Command=”local:CustomCommands.Highlight” />
You need to ensure that the MenuItem control is disabled and the command is not executable when the focus shifts to a TextBox control that does not contain any text.
What should you do?

A.
Set the IsEnabled property for the MenuItem control in the GotFocus event handler for the TextBox controls.

B.
Set the CanExecute property of the command to Highlight_CanExecute. Add the following method to the code-behind file for the window.
private void Highlight_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
TextBox txtBox = (TextBox)sender;
e.CanExecute = (txtBox.Text.Length > 0);
}

C.
Set the CanExecute property of the command to Highlight_CanExecute. Add the following method to the code-behind file for the window.
private void Highlight_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
TextBox txtBox = (TextBox)e.Source;
e.CanExecute = (txtBox.Text.Length > 0);
}

D.
Set the CanExecute property of the command to Highlight_CanExecute. Add the following method to the code-behind file for the window.
private void Highlight_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
MenuItem Menu = (MenuItem)e.Source ;
TextBox txtBox = (TextBox)Menu.CommandTarget;
Menu.IsEnabled = (txtBox.Text.Length > 0);
}



Leave a Reply 2

Your email address will not be published. Required fields are marked *