簡體   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