Search code examples
wpfxamldatatemplateselector

Type initialization of DataTemplateSelector failed in XAML Designer


I'm using a DataTemplateSelector in my WPF Application. At runtime it works perfectly but when using the XAML Editor an exception is thrown and no preview is displayed.

enter image description here

My Selector looks like this:

public class DynamicTemplateSelector : DataTemplateSelector
{
    public static readonly DependencyProperty TemplatesProperty = DependencyProperty.RegisterAttached("Templates", typeof(TemplateCollection), typeof(DataTemplateSelector), new FrameworkPropertyMetadata(new TemplateCollection(), FrameworkPropertyMetadataOptions.Inherits));

    public static TemplateCollection GetTemplates(UIElement element)
    {
        return (TemplateCollection)element.GetValue(TemplatesProperty);
    }

    public static void SetTemplates(UIElement element, TemplateCollection collection)
    {
        element.SetValue(TemplatesProperty, collection);
    }

    /// <summary>
    /// Overriden base method to allow the selection of the correct DataTemplate
    /// </summary>
    /// <param name="item">The item for which the template should be retrieved</param>
    /// <param name="container">The object containing the current item</param>
    /// <returns>The <see cref="DataTemplate"/> to use when rendering the <paramref name="item"/></returns>
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        ////This should ensure that the item we are getting is in fact capable of holding our property
        ////before we attempt to retrieve it.
        if (!(container is UIElement))
        {
            return base.SelectTemplate(item, container);
        }

        ////First, we gather all the templates associated with the current control through our dependency property
        TemplateCollection templates = GetTemplates(container as UIElement);
        if (templates == null || templates.Count == 0)
        {
            base.SelectTemplate(item, container);
        }

        ////Then we go through them checking if any of them match our criteria
        foreach (Template template in templates)
        {
            ////In this case, we are checking whether the type of the item
            ////is the same as the type supported by our DataTemplate
            if (template.Value.IsInstanceOfType(item))
            {
                ////And if it is, then we return that DataTemplate
                return template.DataTemplate;
            }
        }

        ////If all else fails, then we go back to using the default DataTemplate
        return base.SelectTemplate(item, container);
    }
}

If tried to use the DesignerProperties.IsInDesignTool Flag in my Selector but without success...

  public class TemplateCollection : List<Template>
  {
  }




  public class Template : DependencyObject
  {
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(Type), typeof(Template));

    public static readonly DependencyProperty DataTemplateProperty = DependencyProperty.Register("DataTemplate", typeof(DataTemplate), typeof(Template));

public Type Value
{
  get
  {
    return (Type)this.GetValue(ValueProperty);
  }

  set
  {
    this.SetValue(ValueProperty, value);
  }
}

public DataTemplate DataTemplate
{
  get
  {
    return (DataTemplate)this.GetValue(DataTemplateProperty);
  }

  set
  {
    this.SetValue(DataTemplateProperty, value);
  }
}

}


Solution

  • Here is the issue:

    public static readonly DependencyProperty TemplatesProperty = 
    DependencyProperty.RegisterAttached("Templates", typeof(TemplateCollection), 
    typeof(DataTemplateSelector), new FrameworkPropertyMetadata(
    new TemplateCollection(), FrameworkPropertyMetadataOptions.Inherits));
    

    You are registering the attached property for the base class DataTemplateSelector instead of the class you are using the property in XAML DynamicTemplateSelector which seems to break the designer.