【问题标题】:WPF Error: Could not create an instance of type... when creating custom controlWPF 错误:创建自定义控件时无法创建类型的实例...
【发布时间】:2011-03-15 09:14:57
【问题描述】:

我正在尝试创建自定义标签云控件。它应该工作的方式是用户可以在 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;}

}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    你需要实例化标签吗?

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

    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。

    【讨论】:

    • 谢谢,我收到错误“a new expression requires () after type”
    • 是的...我忘记了()...我编辑更新。 (此外,您还需要删除“**”)。它不能只是 IEnumerable。它必须是实现 IEnumerable 的特定类型。
    • 非常感谢,我将标记为答案,就我的逻辑而言,这看起来可以吗?
    • 嗯...我不确定私有静态构造函数和公共构造函数发生了什么,但假设这不是问题。您似乎没有在 xaml 中设置您的 Tags Dependency 属性...
    • 我将快速模拟一些东西来测试它,我会尽快回复你。
    猜你喜欢
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2010-10-26
    • 1970-01-01
    • 2020-04-21
    • 2011-04-11
    相关资源
    最近更新 更多