在CS文件中“SelectedItem”不起作用WP8.1中AutoSuggestBox的“SelectedItem”的替代方法
在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>
在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);
}
}
答案 0 :(得分:0)
Windows Phone 8.1附带的AutoSuggestBox中没有“SelectedItem”,Windows 10的开发者工具中也没有。 AutoSuggestBox的工作方式类似于常规TextBox,这里唯一的优点是可以使用面板/弹出窗口显示基于您传递的ItemsSource的建议。 实际上它只有在ItemsSource是一个字符串集合时才有效,因为DisplayMemberPath不起作用,至少对我来说是这样。 因此,检索“SelectedItem”的唯一方法是使用Text属性。 我知道它实际上并不相同,但AutoSuggestBox它不是ComboBox。
答案 1 :(得分:0)
的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)
TextSearchChangedCommand = new DelegateCommand<Object>((Object) =>
{
method(Object);
});
public void method(Object adr)
{
AutoSuggestBoxSuggestionChosenEventArgs a = (AutoSuggestBoxSuggestionChosenEventArgs)adr;
Address selected = (Address)a.SelectedItem;
}
我花了一整天才意识到: - )