我在页面加载事件中将默认值分配给自动完成框。如果我分配了默认值,则下拉列表将以指定的值打开。任何人都可以帮助如何关闭自动完成框中的下拉列表
答案 0 :(得分: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>
以下是源代码。在这里,我在页面启动时设置了SelectedItem
AutoCompleteBox
但未在.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;
}
}
}