【发布时间】:2020-02-05 15:06:00
【问题描述】:
我有一个视图和一个视图模型。
Viewmodel 有一个 ICollectionView 类型的属性,我将 CollectionViewSource 与 Source 实例化为 ObservableCollection<Foo>
现在,在我看来,我有一个 Datagrid 来显示此信息,在我的 Datagrid.Columns 中有一个 DataGridTemplateColumn,在 DataGridTemplateColumn.HeaderTemplate 内,然后是 DataGridTemplateColumn.CellTemplate
现在在我的第一个标题的单元格模板中,我想显示我的集合中每个元素的索引或元素编号,单元格模板是这样的:
<DataGridTemplateColumn.CellTemplate>
<ItemContainerTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path= DataContext.CurrentPosition}"></TextBlock>
</ItemContainerTemplate>
</DataGridTemplateColumn.CellTemplate>
现在我的DataContext.CurrentPosition 返回空,我不知道这是否是返回索引的对象的正确属性。
【问题讨论】:
-
你指的是this property吗?如果是这种情况,那么只能获取
CollectionViewSource本身的当前元素的索引,而不能获取每个元素的单独索引。