title: Using Generics on Classes with Static DependencyProperty
body: <p>We are looking at creating a helper class for validation on a DevExpress WPF application single view model.</p>
  
  <p>In our xaml, we want to add a reference to our ValidationServiceHelper class:</p>
  
  <pre><code>&lt;dxlc:DataLayoutControl x:Name="layoutControlMyObject" Style="{StaticResource EntityView.DataLayoutControl}"
      viewmodel:ValidationServiceHelper.HasErrors="{Binding RelativeSource={RelativeSource Self}, Path=(dxe:ValidationService.HasValidationError)}"&gt;
  </code></pre>
  
  <p>The ValidationServiceHelper class looks like this:</p>
  
  <pre><code>namespace MyApplication.ViewModels
  {
      public partial class MyObjectViewModel : 
          SingleObjectViewModel&lt;MyObject, int, IMyEntityUnitOfWork&gt;
      {
          // ...
      }
  
      public class ValidationServiceHelper
      {
          public static bool GetHasErrors(DependencyObject obj)
          {
              return (bool)obj.GetValue(HasErrorsProperty);
          }
  
          public static void SetHasErrors(DependencyObject obj, bool value)
          {
              obj.SetValue(HasErrorsProperty, value);
          }
  
          public static readonly DependencyProperty HasErrorsProperty = 
              DependencyProperty.RegisterAttached("HasErrors", typeof(bool), 
              typeof(ValidationServiceHelper), new PropertyMetadata(false, OnHasErrorsChanged));
  
          private static void OnHasErrorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              try
              {
                  FrameworkElement element = (FrameworkElement)d;
                  element.Dispatcher.BeginInvoke(new Action(() =&gt; 
                      ((MyObjectViewModel)element.DataContext).ViewHasErrors = (bool)e.NewValue));
                  var err = ValidationService.GetValidationErrors(d);
                  if (err != null)
                      element.Dispatcher.BeginInvoke(new Action(() =&gt; 
                          ((MyObjectViewModel)element.DataContext).ViewErrors = 
                      err.Select(p =&gt; p.ErrorContent).Distinct().Aggregate(
                      (j, i) =&gt; string.Format("{0}{1}{2}", i, Environment.NewLine, j)).ToString()));
              }
              catch (Exception ex)
              {
                  throw new Exception(ex.Message);
              }
          }
      }
  }
  </code></pre>
  
  <p>Looking at the two Dispatcher.BeginInvoke calls in OnHasErrorsChanged, you will see I hard-coded a cast to MyObjectViewModel. </p>
  
  <pre><code>element.Dispatcher.BeginInvoke(new Action(() =&gt;
      ((MyObjectViewModel)element.DataContext).ViewHasErrors = (bool)e.NewValue));
  </code></pre>
  
  <p>Written like this, I would need to create a different helper class. Is there a way to make this generic, so I can only use one class for all my view models? </p>