繁体   English   中英

我可以绑定到CollectionView的第一项吗?

[英]Can I bind to the first item of a CollectionView?

我将一个ListBox绑定到在ViewModel(从现在开始的VM)上定义的ICollectionView上。 我具有所选项目的属性( SelectedFoo ),该属性设置为VM构造函数中的第一个属性。

当有人在文本框中输入文本时,我会根据该输入过滤集合(到目前为止,很好)。

应用某些过滤后,如何将所选索引设置为集合中的第一项? 我无法从代码绑定到ICollectionView的第一个对象。

有任何想法吗?

这是一些简化的代码,包括List ,我在XAML中绑定到的ICollectionView以及用于过滤的代码在FooFilterString ,随着用户在文本框中键入内容,该代码将更新。

// This is the underlying list
public List<Foo> FooList
{
    get { return _fooList; }
    set
    {
        if (Equals(value, _fooList)) return;
        _fooList = value;
        RaisePropertyChanged();
    }
}
private  List<Foo> _fooList;


// This is what the list box binds to
public ICollectionView FooListView
{
    get { return _fooListView; }
    set
    {
        if (Equals(value, _fooListView)) return;
        _fooListView = value;
        RaisePropertyChanged();
    }
}
private ICollectionView _fooListView ;


// This is bound to from the XAML, as user types, it will filter the list.
// I want to bind to the first item of the filtered list.
public string FooFilterString
{
    get { return _fooFilterString; }
    set
    {
        _fooFilterString = value;

        FooListView.Filter = (s => some_logic); // <-- filters the list

        /*
         * How can I set the selected index here ?!
         */
    }
}
private string _fooFilterString;

// I Bind to this, and want to set this after filtering. First time, I just 
// set it to the first item from the FooList, but after filtering, I'm loosing
// the selection
public Foo SelectedFoo { 
    get { /*...*/ }
    set { /*...*/ }
}
private Foo _selectedFoo;

要汇总注释以从ICollectionView选择第一项,可以使用ICollectionView.MoveCurrentToFirst()

FooListView.Filter = s => some_logic;
FooListView.MoveCurrentToFirst() 

您还需要启用IsSynchronizedWithCurrentItem

<ListBox ... IsSynchronizedWithCurrentItem="True">

当我尝试在 Xamarin Forms

[英]Only the first item in a list is rendered when I try to output a collectionview inside a collectionview in Xamarin Forms

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM