简体   繁体   English

如何从Windows Phone 8.1中的AutoSuggestBox获取所选项目

[英]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.

相关问题 如何从选定的项目ListPicker Windows Phone 8.1 Silverlight获取内容文本? - How to get the content text from the selected item ListPicker Windows Phone 8.1 Silverlight? 如何在Windows Phone 8.1运行时中消除对AutoSuggestBox的关注? - How to dismiss focused of AutoSuggestBox in Windows Phone 8.1 Runtime? Windows Phone 8.1中的AutoSuggestBox出现奇怪的结果 - Strange results in AutoSuggestBox in Windows Phone 8.1 获取ListPicker Windows Phone 8.1 Silverlight中所选项目的价值 - Get Value for selected Item in ListPicker Windows Phone 8.1 Silverlight 如何选择Windows Phone 8.1下拉所选项目 - how to select windows Phone 8.1 Dropdown selected item 如何从Windows Phone 8.1中的ListView(SQLite数据库)中删除所选项目 - How to delete the selected item from listview(sqlite database) in windows phone 8.1 Windows 8.1或Windows Phone 8.1中的ComboBox所选项目 - ComboBox selected item in windows 8.1 or windows phone 8.1 如何从LoopingSelector Windows Phone中获取所选项目 - How to get selected item from LoopingSelector Windows Phone 在Windwos Phone 8.1 Silverlight中,如何从列表框中删除选定的项目 - How to remove selected item from ListBox in windwos phone 8.1 Silverlight 通过fileopenpicker获取所选图像的路径Windows Phone 8.1 C# - get path of selected image by fileopenpicker windows phone 8.1 C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM