WPF ListView get GridViewColumn from control inside DataTemplate

Posted by ragi on Stack Overflow See other posts from Stack Overflow or by ragi
Published on 2011-11-22T17:35:37Z Indexed on 2011/11/22 17:51 UTC
Read the original article Hit count: 834

Example:

<ListView Name="lvAnyList" ItemsSource="{Binding}">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="xx" DisplayMemberBinding="{Binding XX}" CellTemplate="{DynamicResource MyDataTemplate}"/>
      <GridViewColumn Header="yy">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <TextBlock Text="{Binding YY}" />
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView> 

If I access the TextBlock inside DataTemplate I will have access to information about the bound path. But I don't know how to find (starting from the TextBlock) its containing GridViewColumn (which is in the GridViewRowPresenter columns list) and compare it to the grid's GridViewColumn (from GridViewHeaderRowPresenter column list) to get the header's name.

At the end I should have the pairs: xx->XX, yy->YY

I can find a list of all TextBlocks inside ListView by using this:

GridViewRowPresenter gvrp = ControlAux.GetVisualDescendants<GridViewRowPresenter>(element).FirstOrDefault();
IEnumerable<TextBlock> entb = GetVisualDescendants<TextBlock>(gvrp);

I can find a list of all GridViewColumnHeaders:

GridViewHeaderRowPresenter gvhrp = ControlAux.GetVisualDescendants<GridViewHeaderRowPresenter>(element).FirstOrDefault();

And I cannot find the connection between the TextBlocks and the GridViewColumns...

I hope this is understandable.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about listview