2015-04-02 84 views
0

我在我的应用程序中有一个字段,意在包含一个交易号码。该文本字段必须提供建议,以什么样的交易数量可以根据用户输入Personnalised Autocomplete Textbox Windows 8 Metro App

例如,让我们说我有交易数字“12345”,“12346”和“53213”

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

看来autocompletebox不会在Windows存在了有8个地铁应用程序和IsTextPredictionEnabled属性仅对常用词

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

如何给文本框赋值,以便它知道自动完成的内容?

+0

如果你在一个单词的一部分键入会发生什么,比如'softw'? – Bolu 2015-04-02 14:07:09

+0

通过简单地将IsTextPredictionEnabled设置为True,它似乎不会执行任何操作。不过,我希望将自己的数据提供给文本框,所以像软件这样的词不应该显示。只有数字我会提供 – micbobo 2015-04-02 14:09:22

+0

我的意思是'IsTextPredictionEnabled'用于提示常用词,不适用于您的自定义列表... – Bolu 2015-04-02 14:13:28

回答

0

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

该代码会从一个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方法也可以实现显示和隐藏列表框

相关问题