title: Binding on Dependency Property of UserControl
body: <p>I'm trying to create UserControls to be able to reuse them later in the WPF application. I choose to build a small project to train myself, but I can't make it work.</p>
  
  <p>The objective is to have a TextBox whose content will be sent as a Label Text with the click on a Button.</p>
  
  <p>I've read and tried the solutions on those links :</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4962877/xaml-binding-on-dependency-property">XAML Binding on Dependency Property</a> </li>
  <li><a href="https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/custom-dependency-properties" rel="nofollow noreferrer">Custom Dependency Properties</a></li>
  <li><a href="https://stackoverflow.com/questions/5698865/simple-dependency-property-and-usercontrol-issues-in-c-sharp">Simple Dependency Property and UserControl issues in C#</a></li>
  <li><a href="https://stackoverflow.com/questions/815797/add-dependency-property-to-control">Add dependency property to control</a></li>
  </ul>
  
  <p>But even the starting text I set in the constructor doesn't show up, and the button click does nothing at all.</p>
  
  <p>Here are my files :</p>
  
  <p><strong>MyControl.xaml</strong></p>
  
  <pre><code>&lt;UserControl x:Class="WpfApplication1.MyControl"
               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:WpfApplication1"
               mc:Ignorable="d" 
               Name="control"
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition/&gt;
              &lt;RowDefinition/&gt;
              &lt;RowDefinition/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;TextBox Text="{Binding TextBoxContent,ElementName=control}"/&gt;
          &lt;Button Content="Print Entry" Grid.Row="1" Command="{Binding ButtonCommmand,ElementName=control}"/&gt;
          &lt;Label Grid.Row="2" Content="{Binding LabelContent,ElementName=control}"/&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>MyControl.xaml.cs</strong></p>
  
  <pre><code>using GalaSoft.MvvmLight.CommandWpf;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WpfApplication1
  {
      public partial class MyControl : UserControl
      {
          public MyControl()
          {
              InitializeComponent();
              ButtonCommmand = new RelayCommand(Action);
          }
  
          public string TextBoxContent
          {
              get
              {
                  return (string)GetValue(TextBoxContentProperty);
              }
              set
              {
                  SetValue(TextBoxContentProperty, value);
              }
          }
          public RelayCommand ButtonCommmand
          {
              get
              {
                  return (RelayCommand)GetValue(ButtonCommandProperty);
              }
              set
              {
                  SetValue(ButtonCommandProperty, value);
              }
          }
  
          public string LabelContent {
              get
              {
                  return (string)GetValue(LabelContentProperty);
              }
              set
              {
                  SetValue(LabelContentProperty, value);
              }
          }
  
          public void Action()
          {
              LabelContent = TextBoxContent;
          }
  
          public static readonly DependencyProperty TextBoxContentProperty = DependencyProperty.Register("TextBoxContent", typeof(string), typeof(MyControl), new PropertyMetadata(""));
  
          public static readonly DependencyProperty ButtonCommandProperty = DependencyProperty.Register("ButtonCommmand", typeof(RelayCommand), typeof(MyControl), new PropertyMetadata(null));
  
          public static readonly DependencyProperty LabelContentProperty = DependencyProperty.Register("LabelContent", typeof(string), typeof(MyControl), new PropertyMetadata(""));
      }
  }
  </code></pre>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:WpfApplication1"
          xmlns:control="clr-namespace:WpfApplication1"
          mc:Ignorable="d"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;control:MyControl DataContext="{Binding customControl}" TextBoxContent="{Binding Text,Mode=TwoWay}" LabelContent="{Binding EndText,Mode=TwoWay}" ButtonCommmand="{Binding Command,Mode=TwoWay}"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>MainWindow.xaml.cs</strong></p>
  
  <pre><code>using GalaSoft.MvvmLight.CommandWpf;
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Runtime.CompilerServices;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WpfApplication1
  {
      public partial class MainWindow : Window, INotifyPropertyChanged
      {
          MyControl customControl = new MyControl();
          public MainWindow()
          {
              InitializeComponent();
              Command = new RelayCommand(Action);
              Text = "Testing... Testing... 1, 2, 3,...";
          }
  
          private string text;
          public string Text
          {
              get
              {
                  return text;
              }
              set
              {
                  text = value;
                  NotifyPropertyChanged();
              }
          }
          private string endText;
          public string EndText
          {
              get
              {
                  return endText;
              }
              set
              {
                  endText = value;
                  NotifyPropertyChanged();
              }
          }
          private RelayCommand command;
          public RelayCommand Command
          {
              get
              {
                  return command;
              }
              set
              {
                  command = value;
                  NotifyPropertyChanged();
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          public void Action()
          {
              EndText = Text;
          }
  
          private void NotifyPropertyChanged([CallerMemberName]string PropertyName = "")
          {
              if (!String.IsNullOrEmpty(PropertyName))
              {
                  PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
              }
          }
      }
  }
  </code></pre>
  
  <p>Thank you a lot for your help !</p>