servableCollection绑定到自动完成的select

servableCollection绑定到自动完成的select

本文介绍了如何使用ObservableCollection绑定到自动完成的selecteditem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从syncfusion中实现自动完成框.当前选择模式设置为Token

I'm currently implementing autocomplete box from syncfusion. The current selection mode is set to Token

<autocomplete:SfAutoComplete x:Name="autoComplete"
                                DisplayMemberPath="Location"
                                MultiSelectMode="Token"
                                HeightRequest="120"
                                HorizontalOptions="FillAndExpand"
                                TokensWrapMode="Wrap"
                                SelectedItem="{Binding SelectedLocation}"
                                DataSource="{Binding FilteredLocations}"
                                Text="{Binding SearchLocation, Mode=TwoWay}" >
</autocomplete:SfAutoComplete>

我目前正在使用MVVM方法,如何在不使用对象基础类型的情况下绑定到SelectedLocation.

I'm currently using MVVM approach, how do I bind to SelectedLocation without using object base type.

private ObservableCollection<SearchItem> _filteredLocations;
public ObservableCollection<SearchItem> FilteredLocations
{
    get { return _filteredLocations; }
    set { SetProperty(ref _filteredLocations, value); }
}

我目前有什么用

public object SelectedLocation
{
    get { return _selectedLocation; }
    set
    {
        SetProperty(ref _selectedLocation, value);
    }
}

但是我不希望该类型成为对象,并且我将其更改为ObservableCollection<SearchItem>时,不再选择SelectedLocation.我可以得到一些技巧或建议来正确绑定到选定项的集合.

But I don't want the type to be object, and what I change it to be ObservableCollection<SearchItem>, SelectedLocation is no longer picked up. May I get tips or suggestions to properly bind to the selecteditem when it's a collection.

我尝试的无效的方法

public ObservableCollection<SearchItem> SelectedLocation
{
    get { return _selectedLocation; }
    set
    {
        SetProperty(ref _selectedLocation, value);
    }
}

推荐答案

在经过OP澄清后进行了

您似乎必须使用ObservableCollection<object>使它正常工作.然后,如果您想访问单个SearchItem对象,则需要一种机制来调用另一个方法或属性设置器,并将其中的项目转换为该机制.

It looks like you'll have to make it work by using ObservableCollection<object>. If you then want to access the individual SearchItem objects, you'll need a mechanism to call another method or property setter and cast the items there.

public ObservableCollection<object> SelectedLocation
{
    get { return _selectedLocation; }
    set
    {
        SetProperty(ref _selectedLocation, value);
    }
}

有关如何使用ObservableCollection<object>并将结果强制转换为字符串(或在您的情况下为SearchItem)的完整示例,请查看以下知识库文章:如何从自动完成中获取SelectedText .它不是您的用例的1:1,但应该足以进行操作.

For a full example of how to use the ObservableCollection<object> and then casting the results to strings (or in your case to SearchItem), take a look at this KB article: How to get SelectedText from AutoComplete. It's not 1:1 to your use-case but should be enough to proceed.

这篇关于如何使用ObservableCollection绑定到自动完成的selecteditem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:39