3

In the code-behind of a WPF application I have a variable containing a GridView. I know for sure that this GridView is the View of a ListView. Is there any way to get a reference to that ListView?

Thanks

J W
  • 1,679
  • 4
  • 20
  • 28
  • 1
    I've been trying to do the same thing recently, without success... I expected the ListView to be accessible through LogicalTreeHelper, but it isn't – Thomas Levesque Sep 14 '09 at 14:39

1 Answers1

-1

http://www.hardcodet.net/2008/02/find-wpf-parent

We've been using these helper classes for a while to find visual elements in the visual tree. In this case, you'd just want to use the method and it will hunt down the visual ancestor.

TryFindParent<ListView>(yourGridView);
Jeff Wain
  • 1,010
  • 8
  • 12
  • 2
    That won't work, because the GridView object is not a visual child of the ListView... – Thomas Levesque Sep 14 '09 at 14:37
  • So you don't have the GridView declared in code? Even if it's part of a template, you should still be able to find it through the visual tree. See this post for a method of declaring it that you could use. http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ed4ab24e-28b0-49f2-986d-38bdae10051d – Jeff Wain Sep 14 '09 at 14:54
  • 1
    As I said, it is not in the visual tree. The GridView isn't even a `Visual`, it's just a kind of "descriptor" that provides a style for the view, and a method to prepare items for display. On the other hand, I would have expected it to be in the logical tree... – Thomas Levesque Sep 14 '09 at 15:04