WPF: Clear SelectedValue of Combo box on delete button press and Handling Other keys and Cut, Copy, Paste Events

You can use following code to Delete the SelectedValue of combobox on delete  button press event with preventing other key inputs. You can also handle cut copy and paste event on the combobox by using below code.

XAML Design of Combo Box


<ComboBox x:Name="comboBox" KeyDown="comboBox_KeyDown"  IsEditable="True" CommandManager.PreviewExecuted="comboBox_PreviewExecuted" Height="30" Width="90">
<ComboBox.Items>
<ComboBoxItem>True</ComboBoxItem>
<ComboBoxItem>False</ComboBoxItem>
</ComboBox.Items>
</ComboBox>

Back End Code(C#)


private void comboBox_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }

        private void comboBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy ||  e.Command ==     ApplicationCommands.Cut ||  e.Command == ApplicationCommands.Paste)
            {
                e.Handled = true;
            }
        }

No comments:

Post a Comment