簡體   English   中英

如何從Windows Phone 8.1中的AutoSuggestBox獲取所選項目

[英]How to get selected item from AutoSuggestBox in Windows Phone 8.1

在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);
        }
    }

Windows Phone 8.1隨附的AutoSuggestBox中沒有“ SelectedItem”,Windows 10的開發人員工具中也沒有“ SelectedItem”。AutoSuggestBox的工作方式與常規TextBox相似,唯一的好處是可以顯示一個面板/彈出式窗口基於您傳遞的ItemsSource的建議。 實際上,只有在ItemsSource是字符串集合的情況下它才有效,因為DisplayMemberPath不起作用,至少對我而言。 因此,檢索“ SelectedItem”的唯一方法應該使用Text屬性。 我知道這實際上並不相同,但是AutoSuggestBox並不是ComboBox。

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(棱鏡)

TextSearchChangedCommand = new DelegateCommand<Object>((Object) =>
{
    method(Object);
});

public void method(Object adr)
{
    AutoSuggestBoxSuggestionChosenEventArgs a = (AutoSuggestBoxSuggestionChosenEventArgs)adr;
    Address selected = (Address)a.SelectedItem;
}

我花了一整天才意識到:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM