Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4020476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:13:59+00:00 2026-05-20T10:13:59+00:00

I am trying to create a custom Tag Cloud control. The way it’s supposed

  • 0

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?

Code behind

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<Tag>),
        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<Tag> Tags
    {
        get { return (IEnumerable<Tag>)GetValue(TagsProperty); }
        set { SetValue(TagsProperty, value); }
    }
}

XAML

<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">
<Grid>

    <src:TagCloudControl HorizontalAlignment="Center" VerticalAlignment="Center" Name="firstTag"/>

</Grid>
</Window>

TagControl xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TagCloudControlLibrary">

<local:CountToBrushConverter x:Key="CountToBrushConverter"/>
<local:CountToFontSizeConverter x:Key="CountToFontSizeConverter"/>

<Style TargetType="{x:Type local:TagCloudControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TagCloudControl}">
                <Grid>
                    <WrapPanel Orientation="Horizontal" 
                               Margin="2"
                               IsItemsHost="True">
                    </WrapPanel>

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<DataTemplate x:Key="TagsTemplate">
    <WrapPanel>
        <TextBlock Text="{Binding Name}"
                   FontSize="{Binding ItemCount, Converter={StaticResource CountToBrushConverter}}"
                   Foreground="{Binding ItemCount, Converter={StaticResource CountToFontSizeConverter}}"/>
    </WrapPanel>
</DataTemplate>
</ResourceDictionary>

Tag Class

public class Tag
{
    public Tag(string name)
    {
        Name = name;        
    }

    public string Name { get; set;}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:13:59+00:00Added an answer on May 20, 2026 at 10:13 am

    Do you need to instantiate Tags?

    I’m guessing it’s a null reference exception, you set your items source as Tags in your constructor, but you have not instantiated Tags.

    public TagCloudControl()
    {
        Tags = new ObservableCollection<Tags>();
    
        ItemsSource = Tags;
        ...
    } 
    

    Edit:

    After playing around with code… I’m thinking that Tags might not need to be a DependencyProperty. It appears you want to Bind the ItemsSource to Tags… but you could just make Tags a simple property, and in it, set the ItemsSource to the value passed in:

    public TagCloudControl()
    {
        //this is now empty.
    } 
    
    public IEnumerable<Tag> Tags
    {
        get { return (this.ItemsSource as IEnumerable<Tag>); }
        set { this.ItemsSource = value; }
    }
    

    Also I think your Style might want to be more like this:

    <Style TargetType="{x:Type local:TagCloudControl}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate DataType="{x:Type local:Tag}">
                    <TextBlock Text="{Binding Name}"
                               FontSize="{Binding ItemCount, Converter={StaticResource CountToBrushConverter}}"
                               Foreground="{Binding ItemCount, Converter={StaticResource CountToFontSizeConverter}}">
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:TagCloudControl}">
                    <Grid>
                        <WrapPanel Orientation="Horizontal" Margin="2" IsItemsHost="True">
                        </WrapPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    And finally, I think you might be missing ItemCount in your ‘Tag’ Class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom AccordionItem that can take the tag property value
I'm trying to create a custom JSP tag that would take an array object
I've been trying to create a custom control that works exactly like the Panel
I am trying to create a custom accordion for my page to that display
I'm trying to create a custom control - a button - which will have
I am trying to create a custom tag with Facelets but it isn't rendering
I am trying to create a custom HTML Helper that encapsulates some presentation logic
What I'm trying to do is create a new custom data type that behaves
I'm trying to create a custom attribute called Tag for all editable elements. I
I'm trying to create a custom function that unbinds and then binds an event.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.