【问题标题】:Personnalised Autocomplete Textbox Windows 8 Metro App个性化自动完成文本框 Windows 8 Metro App
【发布时间】:2015-04-02 13:56:36
【问题描述】:

我的应用程序中有一个字段用于包含交易编号。此文本框字段必须根据用户输入提供交易编号的建议

例如,假设我有交易编号“12345”“12346”和“53213”

如果用户在文本框中键入“123”,我希望文本框显示“12345”和“12346”。如果用户点击“12346”,则 texbox 的值将变为“12346”

Windows 8 Metro 应用程序中似乎不再存在自动完成框,并且 IsTextPredictionEnabled 属性仅适用于常用词

我的问题如下:我似乎找不到任何类似于列表框的 ItemSource 属性的东西。

我如何给文本框赋值,以便它知道用什么来自动完成?

【问题讨论】:

  • 如果你输入一个单词的一部分会发生什么,比如softw
  • 通过简单地将 IsTextPredictionEnabled 设置为 True 它似乎没有做任何事情。但是,我希望将自己的数据提供给文本框,因此不应显示软件之类的词。我只会提供数字
  • 我的意思是IsTextPredictionEnabled是给常用词的建议,不是给你的自定义列表...
  • 知道什么可以用来制作自定义列表吗?我认为它可以定制,我的错。

标签: .net wpf vb.net xaml windows-store-apps


【解决方案1】:

我最终改变了主意,我没有使用自动完成文本框,而是使用链接到列表框的常规文本框。如果找到项目,则列表框呈现可见,如果没有,则折叠。

此代码将获取来自 Web 服务的字符串值(值可能来自其他任何地方)并通过验证过程来查看它们是否可以显示在自动完成列表框中

这是我的 XAML 代码:

<StackPanel Orientation="Vertical" Grid.Column="1">
    <TextBox x:Name="txtNumeroBon"
             Width="{Binding ElementName=PanelBon, Path=ActualWidth}"
             Style="{StaticResource TextBoxStyles}"
             Margin="8,0,8,0"
             TextChanged="txtNumeroBon_TextChanged"/>

    <ListBox x:Name="lstNumeroBon"
             Visibility="Collapsed"
             Margin="8,0,8,0"
             Height="150"
             SelectionChanged="lstNumeroBon_SelectionChanged"/>
</StackPanel>

这是我的 onTextChanged 方法:

    Private Async Function txtNumeroBon_TextChanged(sender As Object, e As TextChangedEventArgs) As Task
        Dim lstSearch As List(Of String) = New List(Of String)
        lstNumeroBon.Visibility = Windows.UI.Xaml.Visibility.Collapsed

        //Only start searching if there are more than 3 characters
        If txtNumeroBon.Text.Length > 2 Then
            //Get the values for the autocomplete into a list
            Dim lstResult = (From p In Await DataService.InstPatronController.GetInstPatron).ToList()
            For i = 0 To lstResult.Count - 1
                // If the item being searched is bigger than the textbox value
                If lstResult(i).Type.ToString.Length > txtNumeroBon.Text.Length Then
                    //Compare items in lstResult to the textbox value and add if they fit
                    If lstResult(i).Type.Substring(0, txtNumeroBon.Text.Length).ToLower() = txtNumeroBon.Text.ToLower() Then
                        lstSearch.Add(lstResult(i).Type.ToString())
                    End If
                End If
            Next
            //If 1 or more items fit the search
            If lstSearch.Count > 0 Then
                lstNumeroBon.ItemsSource = lstSearch
                lstNumeroBon.Visibility = Windows.UI.Xaml.Visibility.Visible
            End If
        End If
    End Function

这是我的 listboxSelectionChanged 方法:

Private Sub lstNumeroBon_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
    // Verify index to avoid Argument out of range exception
    If lstNumeroBon.SelectedIndex < lstNumeroBon.Items.Count - 1 And lstNumeroBon.SelectedIndex > -1 Then
        txtNumeroBon.Text = lstNumeroBon.Items(lstNumeroBon.SelectedIndex).ToString()
        lstNumeroBon.Visibility = Windows.UI.Xaml.Visibility.Collapsed
    End If
End Sub

还可以实现GotFocusLostFocus方法来显示和隐藏列表框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2010-09-22
    相关资源
    最近更新 更多