gpt4 book ai didi

c# - 使用索引号从 CollectionViewSource 获取项目

转载 作者:行者123 更新时间:2023-12-03 10:32:31 24 4
gpt4 key购买 nike

我在 DataGrid 中使用 CollectionViewSource 作为 ItemSource

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

并且 CollectionViewSource 绑定(bind)到 ViewModel 中的 ObservableCollection
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
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Command="{Binding DataContext.ViewCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="{Binding}"
Content="View" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>

我想要什么,当我单击按钮时。即 ViewCommand 它应该通过索引号获取 Level_Title 或其他项目
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 个索引上的项目

注:我不想涉及 SelectedItem

最佳答案

在您的 CollectionView 上尝试以下操作:

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

关于c# - 使用索引号从 CollectionViewSource 获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52089081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com