【问题标题】:Show Index of each element of an CollectionViewSource显示 CollectionViewSource 的每个元素的索引
【发布时间】: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本身的当前元素的索引,而不能获取每个元素的单独索引。

标签: wpf xaml


【解决方案1】:

好的,我已经按照安迪在这个帖子中的回答解决了这个问题:How to show row-number in first column of WPF Datagrid

【讨论】:

    猜你喜欢
    • 2015-09-16
    • 2014-03-10
    • 1970-01-01
    • 2014-05-02
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多