WPF错误:创建自定义控件时无法创建类型的实例....

时间:2011-03-02 22:01:56

标签: c# wpf

我正在尝试创建自定义Tag Cloud控件。它应该工作的方式是用户可以在itemsSource中为它提供一组字符串,转换后的字符串将显示在UI中。问题是当我尝试将控件放入应用程序时,我收到以下错误:“无法创建TagCloudControl类型的实例”。有人可以帮忙吗?

代码

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>

标记类

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

    public string Name { get; set;}

}

1 个答案:

答案 0 :(得分:0)

您是否需要实例化标签?

我猜它是一个空引用异常,你在构造函数中将你的项目源设置为标签,但你还没有实例化标签。

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

    ItemsSource = Tags;
    ...
} 

编辑:

在玩代码之后......我认为标签可能不需要是DependencyProperty。看来你想将ItemsSource绑定到Tags ...但是你可以只使Tags成为一个简单的属性,并在其中将ItemsSource设置为传入的值:

public TagCloudControl()
{
    //this is now empty.
} 

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

另外我认为你的风格可能想要更像这样:

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

最后,我认为您可能在“标签”类中缺少ItemCount。