title: C# WPF User Control with Textbox, TextBlock and Button
body: <p>I created the following user control that consists of:
  - TextBlock : Contains the description of the path to be inserted
  - TextBox : Contains the name of directory
  - Button : Lets you search for a directory in your pc and put it in the textbox</p>
  
  <pre><code>  public partial class TextBrowse : System.Windows.Controls.UserControl, INotifyPropertyChanged
  {
  public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged(string propertyName)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }
  
  public static readonly DependencyProperty SetName =
    DependencyProperty.Register("BlockName", typeof(string), typeof(TextBrowse),
    new PropertyMetadata(0, new PropertyChangedCallback(
    (obj, chng) =&gt;
    {
      (obj as TextBrowse).BlockName = (string)chng.NewValue;
    }
    ))); 
  }
  public string BlockName
  {
    get { return (string)GetValue(SetName); }
    set { SetValue(SetName, value); }
  }
  private Type _T;
  public Type T
  {
    set { _T = value; }
    get { return _T; }
  }
  public enum Type { File, Folder }
  public TextBrowse()
  {
    InitializeComponent(); 
    if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
      return;
  }
  private void CButton_Click(object sender, RoutedEventArgs e)
  {
    if (_T == Type.File)
    {
      OpenFileDialog ofd = new OpenFileDialog();
      if (ofd.ShowDialog() == DialogResult.OK)
        this.DirectoryName = ofd.FileName;
    }
    else
    {
      FolderBrowserDialog fbd = new FolderBrowserDialog();
      if (fbd.ShowDialog() == DialogResult.OK)
        CTextbox.Text = fbd.SelectedPath;
    }
  }
  public string DirectoryName
  {
    get
    {
      return (string)this.GetValue(DirectoryNameProperty);
    }
    set
    {
      this.SetValue(DirectoryNameProperty, value);
    }
  }
  public static readonly DependencyProperty DirectoryNameProperty = DependencyProperty.Register(
    "TextName", typeof(string), typeof(TextBrowse), new PropertyMetadata(""));}
  </code></pre>
  
  <p>This is the XAML code</p>
  
  <pre><code>&lt;UserControl x:Class="XXX.TextBrowse"
           x:Name="TB"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  &lt;Grid&gt;
  &lt;Grid.ColumnDefinitions&gt;
    &lt;ColumnDefinition Width="Auto"/&gt;
    &lt;ColumnDefinition Width="Auto"/&gt;
  &lt;/Grid.ColumnDefinitions&gt;
    &lt;Grid.RowDefinitions&gt;
    &lt;RowDefinition Height="Auto"/&gt;
    &lt;RowDefinition Height="Auto"/&gt;
  &lt;/Grid.RowDefinitions&gt;
  &lt;TextBlock Text="{Binding Path=DirectoryName, ElementName=TB, Mode=TwoWay}" Name="CTextBlock" Margin="4" Grid.Column ="0" Grid.Row="0"/&gt; 
  &lt;Button Name="CButton" Margin="4" Grid.Column = "1" Grid.Row="1" Content="Browse" Click="CButton_Click" /&gt;
  &lt;TextBox Name="CTextbox" Margin="4" Grid.Column = "0" Grid.Row="1"/&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>And I would like to call it that way from a window</p>
  
  <pre><code>    xmlns:custom ="clr-namespace:XXX"
  Height="300" Width="300"&gt;
  &lt;Grid&gt;
     &lt;custom:TextBrowse BlockName="Name of TextBlock"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>And the result is the dynamic creation of the user control.
  In the name of the TextBlock is the value entered in BlockName
  When a path is selected by means of the button is inserted in the texbox.</p>
  
  <p>As I did I did not work
  Best regard</p>