2014-10-07 50 views
0

我在頁面加載事件中將默認值分配給自動完成框。如果我分配了默認值,則下拉菜單將打開並顯示分配的值。任何人都可以請幫助如何關閉下拉在自動完成框下在wp7中隱藏自動完成框下拉

回答

0

查找下面XAML代碼:

<phone:PhoneApplicationPage 
x:Class="SampleAutoCompleteBox.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:local="clr-namespace:SampleAutoCompleteBox" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
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" 
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"> 

<phone:PhoneApplicationPage.Resources> 
    <local:SampleWords 
    x:Key="AutoCompletions" /> 
</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="Sample Auto Complete Box" Style="{StaticResource PhoneTextNormalStyle}"/> 
     <TextBlock x:Name="PageTitle" Text="Example Page" 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 HorizontalAlignment="Left" Width="450" Grid.Row="0"          
           Name="autoCompleteBox1" VerticalAlignment="Top" TextChanged="autoCompleteBox1_TextChanged"/> 
    </Grid> 
</Grid> 

及以下的源代碼的代碼。在這裏我設置AutoCompleteBoxSelectedItem開始頁面,但沒有在.xaml和textChanged中設置ItemSource我正在設置它的Item源。

public partial class MainPage : PhoneApplicationPage 
{ 
    private string defaultText; 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
     defaultText = "condimentum"; 

     autoCompleteBox1.SelectedItem = defaultText; 

    } 

    private void autoCompleteBox1_TextChanged(object sender, System.Windows.RoutedEventArgs e) 
    { 
     string searchText = ((AutoCompleteBox)sender).SearchText; 
     if (!String.IsNullOrEmpty(searchText) && !defaultText.Equals(searchText)) 
     { 
      autoCompleteBox1.ItemsSource = SampleWords.AutoCompletions; 
      autoCompleteBox1.TextChanged -= autoCompleteBox1_TextChanged; 
     } 


    } 


} 
+0

我試過了,但自動完成框並未顯示默認文本。 – Rev 2014-10-07 13:22:33

+0

我已經創建了一個相同的示例,它爲我工作..我已更新完整的xaml並完成C#代碼..現在檢查是否缺少任何東西。 – 2014-10-08 07:11:15

+0

好的,謝謝Pratik Goyal – Rev 2014-10-08 07:34:52