如何设置自动完成textbox.suggetionssoure自动完成值来自类c#

时间:2013-11-29 04:38:16

标签: c# class windows-phone-7 autocomplete

我试图从另一个类文件中获取textbox.suggetionsource,所以如何实现这个

我想在类file.my中使用此代码片段。自动完成文本源代码如下所示:

 string[] stringArray =     {car,jeep,..... etc};

然后我想使用上面创建的类文件设置textbox.suggetionsource = stringarray 那么如何,请给出建议

1 个答案:

答案 0 :(得分:1)

See sample here

namespace TestCase
{
  public class CarNames : IEnumerable
  {
    public IEnumerable AutoCompletions = new List<Car>
        {
            new Car { Name = "Astra" },
            new Car { Name = "Audi" },
            new Car { Name = "Avant" },
            new Car { Name = "Bugatti" },
            new Car { Name = "Bentley" },
            new Car { Name = "BMW" },
            new Car { Name = "Bond" },
            new Car { Name = "Buckler" },
            new Car { Name = "Burney" },
            new Car { Name = "Chrysler " },
            new Car { Name = "Citroën" },
            new Car { Name = "Crossley" },
        };

    public IEnumerator GetEnumerator()
    {
        return AutoCompletions.GetEnumerator();
    }
}

public class Car
{
    public string Name { get; set; }
}

}

的Xaml

<phone:PhoneApplicationPage 
x:Class="TestCase.MainPage"
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"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:local="clr-namespace:TestCase"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
    <local:CarNames x:Key="CarListKey" />
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<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 x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <toolkit:AutoCompleteBox ValueMemberPath="Name" VerticalAlignment="Top" Name="CarModelNames" ItemsSource="{StaticResource CarListKey}">
            <toolkit:AutoCompleteBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Margin="8,0" Text="{Binding Name}"/>
                </DataTemplate>
            </toolkit:AutoCompleteBox.ItemTemplate>
            </toolkit:AutoCompleteBox>
    </Grid>
</Grid>