title: Cast Generic<Derived> to Generic<Base>
body: <p>I have a base WPF UserControl that handles some common functionality for derived UserControls. In the code-behind of any derived UserControl I call an event</p>
  
  <pre><code>private void SomeClick(object sender, RoutedEventArgs e) {
      HandleClick(sender);
      MyDataGrid.Items.Refresh();
  }
  </code></pre>
  
  <p>In my base UserControl I do</p>
  
  <pre><code>public class BaseUserControl : UserControl {
     protected void HandleClick(object sender) {
         var vm = (BaseViewModel&lt;Part&gt;)DataContext;
         ...
     }
  }
  </code></pre>
  
  <p>This throws an <strong>InvalidCastException</strong> since <code>DataContext</code> is of type <code>BaseViewModel</code> but of a derived type like <code>BaseViewModel&lt;Wire&gt;</code> or <code>BaseViewModel&lt;Connector&gt;</code>.</p>
  
  <p>How can I cast that? </p>