【问题标题】:selecting first item in autocompletebox silverlight在 autocompletebox silverlight 中选择第一项
【发布时间】:2011-12-04 17:37:56
【问题描述】:

我有一个 Silverlight 自动完成框,里面装满了物品。有谁知道如何突出显示列表中的第一项,这样,如果用户按下回车键,就会选择该项并且用户不必使用鼠标?

在其他windows控件中,可以使用selectedindex = 0;

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    对于那些感兴趣的人,您需要获取对 AutoCompleteBox 的子 ListBox 控件的引用并在其上使用 SelectedIndex。

    【讨论】:

      【解决方案2】:

      在 XAML 集中

      IsTextCompletionEnabled="True"

      【讨论】:

        【解决方案3】:

        我认为您正在寻找的是 SelectedItem。如果您在代码中执行此操作,则只需要类似 autoCompleteControl.SelectedItem = listUsedToPopulate[0];

        【讨论】:

        • 不幸的是,这不起作用。当用户键入时,列表中只出现一项
        【解决方案4】:

        只是为了详细说明已经给出的好的答案。

        首先,杰西的回答——set IsTextCompletionEnabled="True", simple——在每次击键后用列表中的第一项填充文本框。然后,当您按 Enter 键时,弹出窗口将关闭。我最终没有使用这种方法的原因是它会立即更新SelectedItem,而无需等待用户按下回车键。

        Sico 的答案是我使用的。它需要继承AutoCompleteBox 控件才能访问GetTemplateChild 方法。代码如下:

        public class ExtendedAutoCompleteBox : AutoCompleteBox
        {
            protected override void OnKeyDown(KeyEventArgs e)
            {
                if (e.Key == Key.Enter)
                {
                    UpdateSelection();
                }
            }
        
            private void UpdateSelection()
            {
                // get the source of the ListBox control inside the template
                var enumerator = ((Selector)GetTemplateChild("Selector")).ItemsSource.GetEnumerator();
        
                // update Selecteditem with the first item in the list
                enumerator.Reset();
                if (enumerator.MoveNext())
                {
                    var item = enumerator.Current;
                    SelectedItem = item;
        
                    // close the popup, highlight the text
                    IsDropDownOpen = false;
                    (TextBox)GetTemplateChild("Text").SelectAll();
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-21
          • 2017-05-23
          • 1970-01-01
          • 2011-06-24
          相关资源
          最近更新 更多