使用带有首字母的文本框进行搜索

时间:2014-05-03 14:47:58

标签: vb.net windows-phone-8

我正在为WP8制作一个包含带地址的列表框的应用程序。可以使用文本框搜索地址。 我的问题是文本框搜索随机字母后,而不是在开头字母。这是我的代码:

VB.net

Private Sub txtSearch_TextChanged(sender As Object, e As TextChangedEventArgs)
        If lstRestaurants IsNot Nothing Then
            Me.listBox.ItemsSource = lstRestaurants.Where(Function(w) w.Restaurantnaam.ToUpper().Contains(txtSearch.Text.ToUpper()))
        End If
    End Sub

XAML

<phone:PhoneApplicationPage
    x:Class="PhoneApp1.LijstRestaurants"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28"/>

        <!--ContentPanel - place additional content here-->
        <TextBox x:Name="txtSearch" TextChanged="txtSearch_TextChanged" Text="" Margin="0,-500,0,0" Height="80" ></TextBox>
        <ListBox x:Name="listBox" FontSize="26" Height="580" Margin="0,100,0,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Restaurantnaam}" Width="440" Margin="10,15,0,0" Height="80"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

</phone:PhoneApplicationPage>
是的,有人能帮帮我吗? 感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用StartsWith()代替Contains()来搜索开头的字母:

Me.listBox.ItemsSource = lstRestaurants.Where(Function(w) w.Restaurantnaam.ToUpper().StartsWith(txtSearch.Text.ToUpper()))