title: VB.NET WPF How to create UserControls for Forms that function like the regular Form controls inside a Form
body: <p>EDIT: My last question wasn't great, so I changed it here. Hopefully this explanation will be better!</p>
  
  <p>I'm trying to create <code>UserControls</code> that work like the regular <code>Form Controls</code> in a <code>Form</code>, or as close as possible.</p>
  
  <p>For my example, I have a <code>UserControl</code> that has inside it a &lt; TextBox >. I want to give this 'UserControl' all the functions and properties that are appropriate for a <code>TextBox</code>, so that it can mimic a regular <code>TextBox</code> inside my <code>Form</code>. This way I can get and set its properties and call its <code>Events</code> from within my <code>Form</code> code.</p>
  
  <p>In my <code>Form1</code>, I have a regular <code>TextBox</code> on top, a regular <code>Button</code> beside it and a <code>ElementHost</code> for my <code>UserControl</code> Textbox.</p>
  
  <p>I want to type in some string into the top <code>TextBox</code>, click my add <code>Button</code> and have to text from the top <code>TextBox</code> passed into my <code>UserControl TextBox</code> that is inside the <code>ElementHost</code> as I'm sure that's the only way you can display the <code>UserControl</code> in a <code>Form</code>!</p>
  
  <p>This is what I've got so far. I don't know if this is the correct approach as I'm new to all this.</p>
  
  <p><strong>UCTextbox.xaml - UserControl</strong>
  </p>
  
  <pre><code>&lt;UserControl x:Class="UCTextbox"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           xmlns:local="clr-namespace:UCTextboxTest"
           mc:Ignorable="d" 
           d:DesignHeight="50" d:DesignWidth="300"&gt;
  &lt;Grid&gt;
       &lt;TextBox x:Name="txtBox" Text="{Binding Text}"/&gt;   
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>UCTextbox.xaml.vb - back code</strong>
  </p>
  
  <pre><code>Public Class UCTextbox
    Public Sub New()
  
      ' This call is required by the designer.'
      InitializeComponent()
  
      ' Add any initialization after the InitializeComponent() call.'
      Me.DataContext = Me
    End Sub
  
    'Get/Set the text value of my &lt; TextBox &gt; UserControl'
    Private textValue As String
    Public Property Text() As String
      Get
          Return textValue
      End Get
      Set(ByVal value As String)
          textValue = value
      End Set
    End Property
  
  ' MORE PROPERTIES AND EVENTS WILL BE ADDED - KEEPING IT SIMPLE FOR NOW'
  End Class
  </code></pre>
  
  <p><strong>Form1 Layout</strong></p>
  
  <p><a href="https://i.stack.imgur.com/81OuV.png" rel="nofollow noreferrer">Form1 Layout</a></p>
  
  <p><strong>Form1 Class</strong>
  </p>
  
  <pre><code>Public Class Form1
    'Create UserControl uc so that Button can reference it'
    Dim uc As UCTextbox
  
    Public Sub New()
  
      ' This call is required by the designer.'
      InitializeComponent()
  
      ' Add any initialization after the InitializeComponent() call.'
  
      'Tried creating UserControl-uc textbox and added it into ElementHost-hostTextbox here instead'
      'Dim uc As UCTextbox = New UCTextbox()'
      'hostTextbox.Child = uc'
      'Me.Controls.Add(hostTextbox)'
  
    End Sub
  
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  
      uc = New UCTextbox()
      hostTextbox.Child = uc
  
      uc.Text = "Dog" 'Using uc function .Text only seem to work inside Form1_Load'
    End Sub
  
  
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
  
     'This does not work here'
      uc.Text = tbxTop.Text
      hostTextbox.Child = uc 'Do I have to reapply the UserControl to the ElementHost each time I make a change to uc'
    End Sub
  End Class
  </code></pre>