【问题标题】:Change button color using keyboard shortcut C# WPF使用键盘快捷键 C# WPF 更改按钮颜色
【发布时间】:2015-06-04 13:17:38
【问题描述】:

我正在研究一些 C# WPF,并正在创建一个简单的文字处理器来将普通文本转换为 wiki 标记。我是 WPF 的新手,遇到了一些看似微不足道的问题,希望可以轻松解决。

我的主窗体上有一个粗体按钮。当按下它时,它会做我需要它做的事情,即将选定的文本变为 bold,再次按下时反之亦然。 粗体按钮在按下时也会变成漂亮的浅蓝色,然后再次按下时变回灰色。太甜了……

//Make Bold MAIN method
    static bool isBold = false;
    public static void boldText()
    {
        if (isBold == false)
        {

            TextSelection ts = MainWindow.thisForm.rtbMain.Selection;
           MainWindow.thisForm.btnBold.Background = Brushes.LightBlue;
            if (!ts.IsEmpty)
            {

                ts.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            }
            isBold = !isBold;

        }
        else
        {

            MainWindow.thisForm.btnBold.Background = Brushes.LightGray;
            TextSelection ts = MainWindow.thisForm.rtbMain.Selection;
            ts.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
            isBold = !isBold;

        }

    }

我现在的问题是由于某种原因,当我使用 InputBinding 调用上面的代码时,选定的文本变为粗体但按钮颜色没有改变...... whaaaaA?我在下面创建了一个自定义 CommandExecuteCanExecute 命令:

  public class ToolBar
   {
     //Custom Command
           public static RoutedCommand boldShortCut = new RoutedCommand();

        //For use with Keybindings for BOLD command
            static bool canExecute = true;
            public static void myCommandExecute(object sender, ExecutedRoutedEventArgs e)
            {
                boldText();
                canExecute = !canExecute;
            }
            public static void myCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
            }

然后我在主窗体的构造函数中创建 KeyGestureInputBindings

public MainWindow()
        {
            InitializeComponent();
            thisForm = this;
            initializeFeatures();

            KeyGesture kg = new KeyGesture(Key.B, ModifierKeys.Control);
            InputBinding ib = new InputBinding(ToolBar.boldShortCut, kg);
            this.InputBindings.Add(ib);
        }

所以这一切都有效,但由于某种原因,当我使用按键手势 (CTRL+B) 时,粗体 按钮没有改变颜色。我需要在 XAML 中做些什么吗?任何帮助将不胜感激,如果有任何不清楚或需要其他信息,请告诉我。谢谢大家!

【问题讨论】:

  • 只是一个建议......不要重复代码,将isBold = !isBold; 放在if else 语句之外并删除其中一个。另外,由于某种原因,当我使用按键手势时,粗体按钮没有改变颜色...这是因为您的 boldText 方法与 Button 无关。
  • 好点,感谢谢里登的提示
  • 如果CtrlB被按下并且PreviewKeyUp处理程序以在之后将其恢复正常。
  • 哦,crikey 没有意识到你第一条评论的后半部分 Sheridan,也感谢使用 PreviewKeyDown 处理程序的建议,我也会看看.当您说boldText() 方法与按钮无关时,我将如何解决?我的意思是它有这个:MainWindow.thisForm.btnBold.Background = Brushes.LightBlue;
  • 好吧,我已经为此奋斗了几个小时了。无论如何,您是否可以给我一个示例,说明我将如何在 boldText() 方法中给出与按钮的关系,或使用 PreviewKeyDown 处理程序?

标签: wpf colors controls inputbinding


【解决方案1】:

您正在创建的命令 (boldShortCut) 永远不会被设置为执行任何操作。 RoutedCommand 仅在激活时触发事件。需要在树的某处绑定一个命令来监听事件并执行一些逻辑。

查看此页面以了解路由命令的工作原理:
How to: Create a RoutedCommand

此外,当您按下 Ctrl+B 时,文本变为粗体的唯一原因是因为这是 RichTextBox 的内置功能。事实上,RichTextBox 有很多功能,您可以很容易地连接工具栏按钮。在尝试重新实现其现有功能之前,学习如何使用RichTextBox 可以节省大量时间。

这是一个很好的页面,可以帮助您入门:RichTextBox Overview

(有关可以连接的现有命令的完整列表,请参阅EditingCommands Class 文档。)

【讨论】:

    【解决方案2】:

    嘿 Xavier,你完全正确,我没有在构造函数中将 Command 添加到 CommandBindings

    请参阅下面的更正代码,该代码在构造函数中调用boldShortcut 命令:

    //Constructor
    public MainWindow()
        {
            InitializeComponent();
            thisForm = this;
            initializeFeatures();
    
            ToolBar.boldShortCut.InputGestures.Add(new KeyGesture(Key.M, ModifierKeys.Control));
            CommandBindings.Add(new CommandBinding(ToolBar.boldShortCut, ToolBar.myCommand, ToolBar.myCommandCanExecute));
    
        }
    

    【讨论】:

      猜你喜欢
      • 2011-12-12
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2014-03-11
      • 2020-07-27
      相关资源
      最近更新 更多