public static CommandBinding CreateCommandBinding(TextBox textBox, TextCommandType textCommandType) { var key = new Key<TextBox, TextCommandType>(textBox, textCommandType); if (_commandBindingDictionary.ContainsKey(key)) { return _commandBindingDictionary[key]; } var commandBinding = new CommandBinding(GetCommand(textCommandType), GetExecuteEventHandler(textBox, textCommandType), GetCanExecuteEventHandler(textBox, textCommandType)); _commandBindingDictionary[key] = commandBinding; return commandBinding; }
private static RoutedUICommand GetCommand(TextCommandType textCommandType) { switch (textCommandType) { case TextCommandType.TrimAll: return TextCommands.Trim; case TextCommandType.TrimEnd: return TextCommands.TrimEnd; case TextCommandType.TrimStart: return TextCommands.TrimStart; default: throw new ArgumentOutOfRangeException(nameof(textCommandType), textCommandType, null); } }
private static CanExecuteRoutedEventHandler GetCanExecuteEventHandler(TextBox textBox, TextCommandType textCommandType) { switch (textCommandType) { case TextCommandType.TrimAll: case TextCommandType.TrimEnd: case TextCommandType.TrimStart: return delegate(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = !string.IsNullOrEmpty(textBox.Text); }; default: throw new ArgumentOutOfRangeException(nameof(textCommandType), textCommandType, null); } }
private static ExecutedRoutedEventHandler GetExecuteEventHandler(TextBox textBox, TextCommandType textCommandType) { switch (textCommandType) { case TextCommandType.TrimAll: return delegate { textBox.Text = TextProcessor.TrimAllLines(textBox.Text, TrimmingPosition.TrimAll); }; case TextCommandType.TrimEnd: return delegate { textBox.Text = TextProcessor.TrimAllLines(textBox.Text, TrimmingPosition.TrimEnd); }; case TextCommandType.TrimStart: return delegate { textBox.Text = TextProcessor.TrimAllLines(textBox.Text, TrimmingPosition.TrimStart); }; default: throw new ArgumentOutOfRangeException(nameof(textCommandType), textCommandType, null); } }
private static void CheckCommandBinding( TextBox textBox, TextCommandFlags textCommandFlags, IEnumerable<CommandBinding> commandBindings, CommandBindingCollection commandBindingCollection, RoutedUICommand command, TextCommandFlags commandFlag, TextCommandType commandType) { var commandBinding = commandBindings.FirstOrDefault(cb => cb.Command == command); if (textCommandFlags.HasFlag(commandFlag) && commandBinding == null) { commandBindingCollection.Add(TextCommandBindings.CreateCommandBinding(textBox, commandType)); } else if (!textCommandFlags.HasFlag(commandFlag) && commandBinding != null) { commandBindingCollection.Remove(commandBinding); } }