使用索引号从 CollectionViewSource 获取项目 [英] Get item from CollectionViewSource using index number

查看:27
本文介绍了使用索引号从 CollectionViewSource 获取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 DataGrid 中使用 CollectionViewSource 作为 ItemSource

I'm using CollectionViewSource as ItemSource in DataGrid

<DataGrid
    ItemsSource="{Binding CollViewSource.View}"
    SelectedIndex="{Binding IndexNumber}"
    ...

并且 CollectionViewSource 绑定到 ViewModel 中的 ObservableCollection

and the CollectionViewSource is bound to ObservableCollection in ViewModel

private ObservableCollection<LevelModel> mLevelSource;
public ObservableCollection<LevelModel> LevelSource
{
     get
     {
         mLevelSource = mLevelSource ?? new ObservableCollection<LevelModel>();
         return mLevelSource;
     }
}


public CollectionViewSource CollViewSource { get; set; }

型号

public class LevelModel : BaseViewModel
{
    public string Level_Title { get; set; }
    ...

在构造函数中

CollViewSource = new CollectionViewSource();
CollViewSource.Source = LevelSource;

我在 DataGrid 中有 Button

I have Button inside DataGrid

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Button
            Command="{Binding DataContext.ViewCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
            CommandParameter="{Binding}"
            Content="View" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

我想要的是,当我单击按钮 .i.e ViewCommand 时,它应该通过索引号获取 Level_Title 或其他一些项目

What I want, when i click the Button .i.e ViewCommand it should fetch Level_Title or some other item by Index Number

private ICommand mViewCommand;
public ICommand ViewCommand
{
    get
    {
        if (mViewCommand == null)
        {
            mViewCommand = new DelegateCommand(delegate ()
            {
                int indexNumber = IndexNumber;
                //string title = // logic should go here


            });
        }
        return mViewCommand;
    }
}

例如,当索引号为 3 时,它应该获取存在于第 3 个索引上的项目

For example when index number is 3 then it should fetch the item that exist on 3rd index

注意:我不想涉及 SeletedItem

Note: I don't want to involve SeletedItem

推荐答案

在 CollectionView 上尝试以下操作:

Try the following on your CollectionView:

LevelModel lm = CollViewSource.View.Cast<LevelModel>().ToArray()[indexNumber];
string title = lm.Level_Title;

这篇关于使用索引号从 CollectionViewSource 获取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆