2015-04-02 86 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方法也可以實現顯示和隱藏列表框

相關問題