0

I have a custom control library where I have several custom controls that I use in my main application. I have a simple custom control that lets the user select pen thickness values from a combobox. Now, I am creating a new custom control, based on a listbox, and I want to include the pen thickness combobox in the new custom control's ItemTemplate.

I am getting this error:

"Cannot create instance of "LineThicknessComboBox" defined in assembly CustomControls ... Exception has been thrown by the target of an invocation. Error at object "10_T" in markup file 'CustomControls;component/Themes/CustomListBox.General.xaml'.

Here is the XAML and code behind for the LineThicknessComboBox:

namespace CustomControls
{
    public class LineThicknessComboBox : ComboBox
    {
        public LineThicknessComboBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LineThicknessComboBox)
                        , new FrameworkPropertyMetadata(typeof(LineThicknessComboBox)));

        }
    }
}

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

Here is the XAML and codebehind for my new CustomListBox:

namespace CustomControls
{
    public class CustomListBox : ListBox
    {
        public CustomListBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomListBox)
                        , new FrameworkPropertyMetadata(typeof(CustomListBox)));
        }
    }
}

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

And here is my 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>

I'm thinking I have some kind of reference problem, but can't figure out what is wrong. The program compiles and runs without any warnings/errors but when the CustomListBox is created in my main application, I get the error listed above. Without including the LineThicknessComboBox, the CustomListBox works fine.

Can anyone explain why I am getting this error? It is possible to include my custom control in another, even though they are in the same custom control library, correct?

Thanks!

4

2 に答える 2

1

// 依存関係プロパティのメタデータのオーバーライドは常に静的コンストラクターで行う必要があるため、どちらの場合も静的コンストラクターにする必要があると思います。

// not public LineThicknessComboBox()
static LineThicknessComboBox()        
{
            DefaultStyleKeyProperty.OverrideMetadata(
               typeof(LineThicknessComboBox)
                        , new FrameworkPropertyMetadata(
                              typeof(LineThicknessComboBox)));
}

OverrideMetadataへの呼び出しは、このメソッドの forType パラメーターとして自身を提供する型の静的コンストラクター内で、または同様のインスタンス化を通じてのみ実行する必要があります。所有者型のインスタンスが存在した後にメタデータを変更しようとしても、例外は発生しませんが、プロパティ システムで一貫性のない動作が発生します。

MSDN から

于 2009-10-19T15:47:52.437 に答える