[英]How to get selected item from AutoSuggestBox in Windows Phone 8.1
In CS File "SelectedItem" is not working what is the alternative to "SelectedItem" for AutoSuggestBox in WP8.1 在CS文件中,“ SelectedItem”无法正常工作,WP8.1中的AutoSuggestBox可以替代“ SelectedItem”
In XAML File: 在XAML文件中:
<AutoSuggestBox x:Name="tblkpersonname" Width="380" Margin="0,-7,0,0" ItemsSource="{Binding}" TextChanged="tblkpersonname_TextChanged">
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"
Tag="{Binding PersonID}"/>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
In Cs File: 在Cs文件中:
private void tblkpersonname_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
try
{
if (tblkpersonname.SelectedItem != null)
{
tblkdes.Text = ((values)tblkpersonname.SelectedItem).Description;
persononlineimg.Source = new BitmapImage(new Uri(((values)tblkpersonname.SelectedItem).FlickrPersonImageUrl, UriKind.RelativeOrAbsolute));
}
}
catch (Exception ex)
{
Exceptions.SaveOrSendExceptions("Exception in tblkpersonname_SelectionChanged_1 Method In AddCast.cs file.", ex);
}
}
There's no "SelectedItem" in the AutoSuggestBox provided with Windows Phone 8.1 and neither there's one in the developer tools for Windows 10. The AutoSuggestBox works like a regular TextBox, the only plus here's the possibility to have a panel/popup that show ups for giving suggestions based on the ItemsSource you passed. Windows Phone 8.1随附的AutoSuggestBox中没有“ SelectedItem”,Windows 10的开发人员工具中也没有“ SelectedItem”。AutoSuggestBox的工作方式与常规TextBox相似,唯一的好处是可以显示一个面板/弹出式窗口基于您传递的ItemsSource的建议。 Actually it only works if the ItemsSource is a collection of string, since the DisplayMemberPath doesn't work, at least for me.
实际上,只有在ItemsSource是字符串集合的情况下它才有效,因为DisplayMemberPath不起作用,至少对我而言。 So the only way to retrieve the "SelectedItem" you should use the Text property.
因此,检索“ SelectedItem”的唯一方法应该使用Text属性。 I know it's not actually the same, but the AutoSuggestBox it's not a ComboBox.
我知道这实际上并不相同,但是AutoSuggestBox并不是ComboBox。
Xaml XAML
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
<AutoSuggestBox
Text="{Binding EnteredAddress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding AddressAutoComplete}"
ItemTemplate="{StaticResource Autocomplete}"
TextMemberPath="name">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SuggestionChosen">
<core:InvokeCommandAction Command="{Binding TextSearchChangedCommand}" CommandParameter="{Binding this}">
</core:InvokeCommandAction>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
ViewModel (Prism) ViewModel(棱镜)
TextSearchChangedCommand = new DelegateCommand<Object>((Object) =>
{
method(Object);
});
public void method(Object adr)
{
AutoSuggestBoxSuggestionChosenEventArgs a = (AutoSuggestBoxSuggestionChosenEventArgs)adr;
Address selected = (Address)a.SelectedItem;
}
I spent a whole day to realize it:-) 我花了一整天才意识到:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.