簡體   English   中英

WPF錯誤:創建自定義控件時無法創建類型的實例

[英]WPF Error: Could not create an instance of type… when creating custom control

我正在嘗試創建自定義標簽雲控件。 它應該起作用的方式是,用戶可以在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;}

}

您需要實例化標簽嗎?

我猜這是一個空引用異常,您在構造函數中將項源設置為Tag,但是尚未實例化Tag。

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM