简体   繁体   English

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

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

I'm binding a ListBox to an ICollectionView defined on the ViewModel (VM from now). 我将一个ListBox绑定到在ViewModel(从现在开始的VM)上定义的ICollectionView上。 I have a property for the selected item ( SelectedFoo ) that is set to be the first in the VM constructor. 我具有所选项目的属性( SelectedFoo ),该属性设置为VM构造函数中的第一个属性。

When someone enters text in a textbox, I filter the collection based on that input (so far, so good). 当有人在文本框中输入文本时,我会根据该输入过滤集合(到目前为止,很好)。

How can I set the selected index to be the first item in the collection after some filtering was applied? 应用某些过滤后,如何将所选索引设置为集合中的第一项? I can't bind to the first object in the ICollectionView from the code. 我无法从代码绑定到ICollectionView的第一个对象。

Any ideas? 有任何想法吗?

Here's some stripped down code, including the List , the ICollectionView that I bind to in the XAML, and the code that filters is in the FooFilterString which is updated as the user types into the text box. 这是一些简化的代码,包括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;

To sum up comments to select first item from ICollectionView you can use ICollectionView.MoveCurrentToFirst() 要汇总注释以从ICollectionView选择第一项,可以使用ICollectionView.MoveCurrentToFirst()

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

you'll also need to enable IsSynchronizedWithCurrentItem 您还需要启用IsSynchronizedWithCurrentItem

<ListBox ... IsSynchronizedWithCurrentItem="True">

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

相关问题 当我尝试在 Xamarin Forms - Only the first item in a list is rendered when I try to output a collectionview inside a collectionview in Xamarin Forms 如何将嵌套 collectionView 的 ItemsSource 属性绑定到其中包含的 CollectionView 的项目? - How to bind the ItemsSource property of a nested collectionView to the item of the CollectionView in which is contained? Xamarin - CollectionView 第一项和最后一项的额外空间 - Xamarin - CollectionView extra space for first and last item 如何在 CollectionView(水平)中直观地使我的选定项目显示在我的屏幕中心? - How can I visually make my Selected Item in a CollectionView (Horizontal), show at the center of my screen? WPF CollectionView / DataGrid获取第一个可见项的索引 - WPF CollectionView/DataGrid get index of first visible item Xamarin.CommunityToolkit.MauiCompat TouchEffect 未应用于 CollectionView 的第一项 - Xamarin.CommunityToolkit.MauiCompat TouchEffect is not applied to the first item of the CollectionView 如何从ComboBox的Selected Item绑定到属性? - How can I bind to a property from the Selected Item of a ComboBox? 如何绑定ctrl + 1来选择第一个标签? - How can I bind ctrl+1 to select first tab? 我怎样才能在 CollectionView 中拥有相应的 object? - How can I have the respective object in a CollectionView? 如何获取XAML中定义的CollectionView - How can I get the CollectionView that is defined in XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM