【发布时间】:2009-10-18 19:07:55
【问题描述】:
我有一个自定义控件库,其中有几个在我的主应用程序中使用的自定义控件。我有一个简单的自定义控件,可让用户从组合框中选择笔粗细值。现在,我正在创建一个基于列表框的新自定义控件,并且我想在新自定义控件的 ItemTemplate 中包含笔粗细组合框。
我收到此错误:
“无法创建在程序集 CustomControls 中定义的“LineThicknessComboBox”实例...调用的目标已引发异常。标记文件“CustomControls;component/Themes/CustomListBox”中的对象“10_T”出错。 General.xaml'。
这是 LineThicknessComboBox 的 XAML 和代码:
namespace CustomControls
{
public class LineThicknessComboBox : ComboBox
{
public LineThicknessComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LineThicknessComboBox)
, new FrameworkPropertyMetadata(typeof(LineThicknessComboBox)));
}
}
}
在 LineThicknessCombobox.Generic.xaml 中:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControls">
<Style TargetType="{x:Type local:LineThicknessComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
...
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这是我的新 CustomListBox 的 XAML 和代码隐藏:
namespace CustomControls
{
public class CustomListBox : ListBox
{
public CustomListBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomListBox)
, new FrameworkPropertyMetadata(typeof(CustomListBox)));
}
}
}
在 CustomListBox.Generic.xaml 中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControls">
<Style TargetType="{x:Type local:CustomListBox}" BasedOn="{StaticResource {x:Type ListBox}}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border>
...
<local:LineThicknessComboBox />
...
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这是我的 Generix.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CustomControls;component/Themes/LineThicknessComboBox.Generic.xaml"/>
<ResourceDictionary Source="CustomControls;component/Themes/CustomListBox.Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
我在想我有某种参考问题,但不知道哪里出了问题。该程序编译并运行时没有任何警告/错误,但是在我的主应用程序中创建 CustomListBox 时,我收到上面列出的错误。如果不包括 LineThicknessComboBox,CustomListBox 可以正常工作。
谁能解释我为什么会收到这个错误?可以将我的自定义控件包含在另一个控件中,即使它们在同一个自定义控件库中,对吗?
谢谢!
【问题讨论】:
-
"调用的目标抛出了异常。"深入了解 InnerException(您可能需要深入了解不止一个级别),您应该会找到一个更有意义的异常。一旦你看到它,这可能会使错误变得明显,或者如果你可以发布它,它会帮助诊断。
标签: c# wpf user-controls custom-controls resourcedictionary