title: WPF Get the associated ListView in a GridView
body: <p>I'm trying to implement a custom <code>GridView</code> for the <code>ListView</code>, but I need to find a way to get the <code>ListView</code> that is associated with the <code>GridView</code>, since I need to access some of the <code>ListView</code> properties (<code>Width</code>, <code>Items</code>, <code>Template</code>...). </p>
  
  <p>I found an old post that was asking the same question <a href="https://stackoverflow.com/questions/1420742/get-the-parent-listview-from-a-gridview-object">Get the parent listview from a gridview object</a> but it never got an answer...</p>
  
  <p>If anyone has an idea, I would be glad :)</p>
  
  <p><strong>EDIT:</strong> Here some basic code from the custom <code>GridView</code></p>
  
  <pre><code>public class GridViewEx : GridView
  {
      public ListView Owner {get; set;}     // This is what I need to get
  
      public GridViewEx()
      {
  
      }
  
  }
  </code></pre>
  
  <p><strong>EDIT2:</strong> I found another solution than the one presented by mm8. Since I also needed a custom <code>GridViewHeaderRowPresenter</code>, which is used in the <code>ListView Scrollviewer Style</code>, here is what I came up with (as for now):</p>
  
  <pre><code>public class GridViewHeaderRowPresenterEx : GridViewHeaderRowPresenter
  {
      private GridViewEx _GridView;
  
      public GridViewHeaderRowPresenterEx()
      {
          Loaded += OnLoaded;
          Unloaded += OnUnLoaded;
      }
  
      private void OnLoaded(object sender, RoutedEventArgs e)
      {
          if (this.GetVisualParent&lt;ListView&gt;() is ListView lv &amp;&amp; lv.View is GridViewEx gridView)
          {
              _GridView = gridView;
              _GridView.Owner = lv;
          }
      }
  
      private void OnUnLoaded(object sender, RoutedEventArgs e)
      {
          if (_GridView != null)
              _GridView.Owner = null;
      }
  }
  </code></pre>
  
  <p>And here is the extension method to get the <code>ListView</code> from the custom <code>GridViewHeaderRowPresenter</code>:</p>
  
  <pre><code>public static class DependencyObjectExtensions
  {
      public static T GetVisualParent&lt;T&gt;(this DependencyObject depObj) where T : DependencyObject
      {
          if (VisualTreeHelper.GetParent(depObj) is DependencyObject parent)
          {
              var result = (parent as T) ?? GetVisualParent&lt;T&gt;(parent);
              if (result != null)
                  return result;
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p>The <code>GridViewHeaderRowPresenter</code> <code>Loaded</code> event is called when a <code>GridView</code> is added to a <code>ListView</code>, and the <code>Unloaded</code> event is called when the <code>GridView</code> is removed from the <code>ListView</code>.</p>
  
  <p>I prefer this solution over the one from mm8, since it required (if I'm not mistaken) the <code>ListView</code> to have <code>Items</code> in order to work.</p>
  
  <p>Thanks for the suggestions :) </p>