Ask a questionAsk a question
 

AnswerCustom Control Problem

  • Thursday, November 05, 2009 1:50 AMPrettyman Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm trying to create two controls.  I've modified a RichTextBox to include some extra properties and I want to use that in a user control that I've created.  If I build the richtextbox outside of the project then everything works fine, but if I try to include it in the project things go crazy and it can't load it properly.  The exact error is this:  Type "..." is not defined.  I'm kind of stumped as to why but this is my first time trying this so anyone who could offer an explanation at least would be helping me out.  Thanks

Answers

  • Wednesday, November 11, 2009 4:07 AMLinda LiuMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Prettyman,

    The following is a walkthrough to use a derived RichTextBox in a UserControl. It may be different than in your practice, but it may be helpful.

    1. Create a WPF application project, named WpfApplication1.
    2. Add a class into the project, which derives from the RichTextBox class.
    3. Add a UserControl into the project. The XAML of the UserControl is as follows:

    <UserControl x:Class="WpfApplication1.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Height="300" Width="300">
      <local:iRichTextBox/>
    </UserControl>

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Thursday, November 05, 2009 1:54 AMFoovanadil Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you post any code snippets? I don't fully understand what you are asking. Are you including an assembly that has this custom control or are you including the code file and the generic.xaml for your custom control directly in the project with the user control?



    If you are including an assembly with the custom control in it have you made sure to include a namespace alias (xmlns) for your assembly so you can reference the control.


  • Thursday, November 05, 2009 5:26 AMPrettyman Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    I haven't done any retemplating of the richtextbox, I've just got a code behind file that add's two properties, they are as follows...

    Public Class iRichTextBox   
    Inherits RichTextBox
    Public Property SelectionFont as FontFamily
         Get
             Me.Selection.GetPropertyValue(Inline.FontFamily)
         End Get
         Set (ByVal value as FontFamily)
             Me.Selection.ApplyPropertyValue(Inline.FontFamily, value)
         End Set
    End Property
    
    Public Property SelectionFontSize as Double
         Get
             Me.Selection.GetPropertyValue(Inline.FontSize)
         End Get
         Set (ByVal value as Double)
             Me.Selection.ApplyPropertyValue(Inline.FontSize, value)
         End Set
    End Property
    End Class
    
    


    And I'm trying to create a usercontrol that uses this "custom richtextbox" to create a richtexteditor.  The problem being that it gives me the error from above.
  • Thursday, November 05, 2009 3:32 PMFoovanadil Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    So this class and the UserControl live in the same solution correct? Can you post the XAML snippet that show how you are trying to use the RichTextBox in the UserControl? I am guessing that it might be a namespace alias issue, but I can't tell without seeing the XAML.

    --Brad
  • Wednesday, November 11, 2009 4:07 AMLinda LiuMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Prettyman,

    The following is a walkthrough to use a derived RichTextBox in a UserControl. It may be different than in your practice, but it may be helpful.

    1. Create a WPF application project, named WpfApplication1.
    2. Add a class into the project, which derives from the RichTextBox class.
    3. Add a UserControl into the project. The XAML of the UserControl is as follows:

    <UserControl x:Class="WpfApplication1.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Height="300" Width="300">
      <local:iRichTextBox/>
    </UserControl>

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.