2

我有这个 XAML 代码

<ListView Name="_lvContacts" ItemsSource="{Binding AccountPhonesList}"
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="{Binding Path=ColumnHeader1}" DisplayMemberBinding="{Binding Path=Home}" Width="70" />
                        <GridViewColumn Header="{Binding Path=ColumnHeader2}" DisplayMemberBinding="{Binding Path=Office}" Width="70" />
                        <GridViewColumn Header="{Binding Path=ColumnHeader3}" DisplayMemberBinding="{Binding Path=Mobile}" Width="70" />
                        <GridViewColumn Header="{Binding Path=ColumnHeader4}" DisplayMemberBinding="{Binding Path=Other}" Width="70" />
                        <GridViewColumn Header="{Binding Path=ColumnHeader5}" DisplayMemberBinding="{Binding Path=Email}" Width="70" />
                    </GridView>
                </ListView.View>
            </ListView>

这是背后的代码

            for (int i = 0; i < _lvContacts.Items.Count; i++)
            {
                string header = _lvContacts.Items[i].ToString();
                MyFunction(header);
            }

如何从 ListView 获取 GridViewColumn 标题?我试图从 ItemSources 获取 GridView,但这已被集合使用!我不知道我还能说什么。

我希望这是可以理解的。

4

4 回答 4

12

在您的情况下,您可以执行以下操作:

((GridView)_lvContacts.View).Columns[i].Header
于 2013-09-11T17:25:03.787 回答
9

要获取所有标题,您可以使用以下代码:

List<GridViewColumnHeader> headers = GetVisualChildren<GridViewColumnHeader>(_lvContacts).ToList();

public static IEnumerable<T> GetVisualChildren<T>(DependencyObject parent) where T : DependencyObject
    {
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (child is T)
                yield return (T)child;

            foreach (var descendant in GetVisualChildren<T>(child))
                yield return descendant;
        }
    }
于 2013-09-11T17:26:50.423 回答
1

您可以通过GridView以下方式获取 , 和列:

GridView gridView = (GridView)_lvContacts.View;
foreach(GridViewColumn column in gridView.Columns)
    // do something with column.Header. Your example:
    if (ItsTheColumnHeaderIWant(column.Header))
    {
        MyFunction(column.Header);
    }

或者,您可以在 xaml 中命名 GridView:

<GridView x:Name="_gridView"...>

然后你可以直接从代码中访问它:_gridView.Columns.

于 2013-09-11T17:25:25.060 回答
0

使用 LINQ!!!

  var headers = ((GridView) _lvContacts.View).Columns.ToDictionary(column => column.Header.ToString());
于 2017-03-02T21:51:53.257 回答