Visual Studio Development Bookmark and Share   
 index > Visual Studio WPF Designer > Cannot create an instance of the custom control in WPF
 

Cannot create an instance of the custom control in WPF

Hello,

I have created textbox custom control in wpfinheriting from standard textbox.I have added it to vs2008 toolbox,but when i drag and drop this custom control in .xaml page,it raises an error like "Could not create an instance of type "CTextbox"( CTextBox is the name of my custom control)
rest of the custom controls like button and listbox are working fine this are also inherited from standard controls in wpf.

Thanks In Advance
SivaT  Tuesday, September 01, 2009 3:46 AM
Hello,

Myproblem is solved its working without any error.It is because of some addingreferences issue.

Thank you
SivaT  Friday, September 04, 2009 9:29 AM
Can you post your code for the CTextBox. This will help us provide the needed assistance.
Cheers, Karl - Microsoft Product Team, Code Project MVP - http://karlshifflett.wordpress.com/
karl140.6  Tuesday, September 01, 2009 2:37 PM
Hello Karl,

I just created a simple textbox custom control without any attributes.If this is solved then i thought of giving attributes.
So this is my code
I have added ClassLibrary in WPF application.In ClassLibrary

under 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:CustControlLibrary.WpfAppCustCtrl">


<Style TargetType="{x:Type local:CTextBox}">

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type local:CTextBox}">

<TextBox Width="300" Height="150"> 

</TextBox>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</ResourceDictionary>

Under CTextBox.vb
 Public Class CTextBox
        Inherits System.Windows.Controls.TextBox

        Shared Sub New()
            'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
            'This style is defined in Themes\Generic.xaml
            DefaultStyleKeyProperty.OverrideMetadata(GetType(CTextBox), New FrameworkPropertyMetadata(GetType(CTextBox)))
        End Sub

After adding References of classlibrary i just tried to use my custom control inmy application then i got anerror as cannot create an instance ofType "CTextBox"

Under Window.xaml

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Employee Details" Height="300" Width="600"
        xmlns:cc="clr-namespace:CustControlLibrary.WpfAppCustCtrl;assembly=CustControlLibrary">
     
   <Window.Resources>
    <XmlDataProvider x:Key="CustomerListData" Source="Details.xml"
                     XPath="/CustomerList/Customer"></XmlDataProvider>
   </Window.Resources>

    <Grid>
        <cc:CTextBox></cc:CTextBox>(Error is raised here)
</Grid>
</Window>

Thanks in Advance
SivaT  Wednesday, September 02, 2009 2:47 AM
I can't repro this problem on Visual Studio 2010 didn't have any problems.

I'll try later today to repro on Visual Studio 2008.
Cheers, Karl - Microsoft Product Team, Code Project MVP - http://karlshifflett.wordpress.com/
karl140.6  Wednesday, September 02, 2009 6:39 PM
I removed all references and added again all the referencesand rebuild the project but still i am getting error.I am working on Visual studio 2008.
SivaT  Thursday, September 03, 2009 2:48 AM
Hello,

Myproblem is solved its working without any error.It is because of some addingreferences issue.

Thank you
SivaT  Friday, September 04, 2009 9:29 AM

You can use google to search for other answers

Custom Search

More Threads

• unable to bind property on propertychanged event
• Hosting Cider Designer in my Application
• Generating .chm files for WPF app
• WPF/E
• How to convert Access database forms into .net web forms?
• Design vs runtime
• How to host WPF designer in my application?
• Vista business sp1, VS 2008 STD sp1 Wpf solution, crash on exit
• Unhandled exception in designer canvas of WPF project
• App throws exception if property value is mis-spelled