组合框打开的Alt + 快捷方式

时间:2022-10-31 05:52:32

Let's assume that we have the following XAML:

我们假设我们有以下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 shortcut will press the button. How can I make Alt+R shortcut opening the combobox dropdown?

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

Update: BTW, I'm aware about Label's Target property and I know that I can create KeyBinding (or something similar) and handle for example Ctrl+R, but I'm looking for more simple way.

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

1 个解决方案

#1


0  

I found the following solution. Not so simple as I expected, but I can live with it.

我找到了以下解决方案。并不像我预期的那么简单,但我可以忍受它。

First, I need to specify the name of the ComboBox:

首先,我需要指定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>

Second, register in the view constructor the access key 'R' and open ComboBox in the event handler:

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

    public MainView()
    {
        InitializeComponent();

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

    //...

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

#1


0  

I found the following solution. Not so simple as I expected, but I can live with it.

我找到了以下解决方案。并不像我预期的那么简单,但我可以忍受它。

First, I need to specify the name of the ComboBox:

首先,我需要指定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>

Second, register in the view constructor the access key 'R' and open ComboBox in the event handler:

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

    public MainView()
    {
        InitializeComponent();

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

    //...

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