【问题标题】:Alt+<Key> shortcut for combobox openingAlt<Key> 打开组合框的快捷键
【发布时间】:2013-04-06 05:05:50
【问题描述】:

假设我们有以下 XAML:

    <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">

        <Button Content="_Test" Margin="12" Width="200" Height="30" Click="OnClick" />

        <ComboBox Margin="12" Width="200" Height="30" >
            <ComboBox.Items>
                <ComboBoxItem>First</ComboBoxItem>
                <ComboBoxItem>Second</ComboBoxItem>
                <ComboBoxItem>Third</ComboBoxItem>
            </ComboBox.Items>
        </ComboBox>
    </StackPanel>

Alt+T 快捷键将按下按钮。如何使 Alt+R 快捷键打开组合框下拉菜单?

更新:顺便说一句,我知道 Label 的 Target 属性,并且我知道我可以创建 KeyBinding(或类似的东西)并处理例如 Ctrl+R,但我正在寻找更多简单的方法。

【问题讨论】:

  • 如何使用 keyBinding 做同样的事情?

标签: wpf keyboard-shortcuts


【解决方案1】:

我找到了以下解决方案。没有我想象的那么简单,但我可以忍受。

首先,我需要指定 ComboBox 的名称:

    <ComboBox x:Name="ResourcesComboBox" Margin="12" Width="200" Height="30" >
        <ComboBox.Items>
            <ComboBoxItem>First</ComboBoxItem>
            <ComboBoxItem>Second</ComboBoxItem>
            <ComboBoxItem>Third</ComboBoxItem>
        </ComboBox.Items>
    </ComboBox>

其次,在视图构造函数中注册访问键“R”并在事件处理程序中打开 ComboBox:

    public MainView()
    {
        InitializeComponent();

        AccessKeyManager.Register("R", ResourcesComboBox);
        AccessKeyManager.AddAccessKeyPressedHandler(ResourcesComboBox, AccessKeyPressedEventHandler);
    }

    //...

    private void AccessKeyPressedEventHandler(object sender, AccessKeyPressedEventArgs e)
    {
        ResourcesComboBox.IsDropDownOpen = true;
    }

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2022-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2021-05-26
    • 2021-06-20
    • 2023-03-12
    相关资源
    最近更新 更多