1

假设我们有以下 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 快捷键打开组合框下拉菜单?

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

4

1 回答 1

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;
    }
于 2013-04-14T13:06:55.700 回答