title: WPF Error: Could not create an instance of type... when creating custom control
body: <p>I am trying to create a custom Tag Cloud control. The way it's supposed to work is that the user can give it a collection of strings in the itemsSource and the converted string will be displayed in the UI. The problem is when I try to drop the control into an application I get the following error:"Could not create an instance of type TagCloudControl". Can anyone help?</p>
  
  <p><strong>Code behind</strong></p>
  
  <pre><code>public class TagCloudControl : ListBox
  {
      static TagCloudControl()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(TagCloudControl), new FrameworkPropertyMetadata
              (typeof(TagCloudControl)));
      }
      //tags dependency property
      public static DependencyProperty TagsProperty = DependencyProperty.Register("Tags",
          typeof(IEnumerable&lt;Tag&gt;),
          typeof(TagCloudControl));
  
      public CollectionView GroupedTagsView { get; set; }
  
      public TagCloudControl()
      {
          ItemsSource = Tags;
  
          //group my tags by "name" property
          GroupedTagsView = (ListCollectionView)CollectionViewSource.GetDefaultView(Tags);
          GroupedTagsView.GroupDescriptions.Add(new PropertyGroupDescription("Name")); 
  
      }
  
      public IEnumerable&lt;Tag&gt; Tags
      {
          get { return (IEnumerable&lt;Tag&gt;)GetValue(TagsProperty); }
          set { SetValue(TagsProperty, value); }
      }
  }
  </code></pre>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;Window x:Class="TagCloudDemo.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:src="clr-namespace:TagCloudControlLibrary;assembly=TagCloudControlLibrary"
  Title="Window1" Height="300" Width="300"&gt;
  &lt;Grid&gt;
  
      &lt;src:TagCloudControl HorizontalAlignment="Center" VerticalAlignment="Center" Name="firstTag"/&gt;
  
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>TagControl xaml</strong></p>
  
  <pre><code>&lt;ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:TagCloudControlLibrary"&gt;
  
  &lt;local:CountToBrushConverter x:Key="CountToBrushConverter"/&gt;
  &lt;local:CountToFontSizeConverter x:Key="CountToFontSizeConverter"/&gt;
  
  &lt;Style TargetType="{x:Type local:TagCloudControl}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:TagCloudControl}"&gt;
                  &lt;Grid&gt;
                      &lt;WrapPanel Orientation="Horizontal" 
                                 Margin="2"
                                 IsItemsHost="True"&gt;
                      &lt;/WrapPanel&gt;
  
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;DataTemplate x:Key="TagsTemplate"&gt;
      &lt;WrapPanel&gt;
          &lt;TextBlock Text="{Binding Name}"
                     FontSize="{Binding ItemCount, Converter={StaticResource CountToBrushConverter}}"
                     Foreground="{Binding ItemCount, Converter={StaticResource CountToFontSizeConverter}}"/&gt;
      &lt;/WrapPanel&gt;
  &lt;/DataTemplate&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p><strong>Tag Class</strong></p>
  
  <pre><code>public class Tag
  {
      public Tag(string name)
      {
          Name = name;        
      }
  
      public string Name { get; set;}
  
  }
  </code></pre>