【问题标题】:Select the item in combobox with key/up in WPf在 WPf 中使用键/向上选择组合框中的项目
【发布时间】:2009-06-08 07:25:49
【问题描述】:

如何使用上/下键选择组合框中的项目?

我试图在 PreviewKeyDown 事件中处理它。但它不起作用。

【问题讨论】:

    标签: .net wpf combobox


    【解决方案1】:

    从Selector 派生的任何控件都已经存在键盘导航,就像 CoboBox 一样。控件必须先获得焦点,然后才能捕获关键事件。 您是否尝试做其他事情然后选择/滚动项目?因为它已经这样做了。

    【讨论】:

      【解决方案2】:

      我完全同意上述答案。首先你必须选择控件。

      例如

      这是一个示例xaml 代码

      <Page x:Class="CascadingDropDown.Page1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="Page1" Loaded="Page_Loaded">
          <Grid>
              <ComboBox Height="23" Margin="60,32,120,0" Name="comboBox1" VerticalAlignment="Top" DisplayMemberPath="CountryNames" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"/>
          </Grid>
      </Page>
      

      在 .CS 文件中添加以下内容

      private void Page_Loaded(object sender, RoutedEventArgs e)
      {
          comboBox1.DataContext = CountryTable();
          comboBox1.Focus();
      }
      
      private DataTable CountryTable()
      {
         DataTable dt = new DataTable();
      
         dt.Columns.Add("CountryNames");
      
         dt.Rows.Add("India");
         dt.Rows.Add("Australia");
         dt.Rows.Add("Canada");
         dt.Rows.Add("USA");
      
         return dt;
      }
      

      所以加载之后,你会发现combo被选中了。使用向上/向下箭头键选择要选择的项目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-14
        • 2011-01-13
        • 2018-01-23
        • 2012-04-28
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 2012-05-16
        相关资源
        最近更新 更多