<p>When developing WPF UserControls, what is the best way to expose a DependencyProperty of a child control as a DependencyProperty of the UserControl?  The following example shows how I would currently expose the Text property of a TextBox inside a UserControl. Surely there is a better / simpler way to accomplish this?</p>
<pre class="lang-xml prettyprint-override"><code>    &lt;UserControl x:Class=&quot;WpfApplication3.UserControl1&quot;
        xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;&gt;
        &lt;StackPanel Background=&quot;LightCyan&quot;&gt;
            &lt;TextBox Margin=&quot;8&quot; Text=&quot;{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}&quot; /&gt;
        &lt;/StackPanel&gt;
    &lt;/UserControl&gt;
</code></pre>
<pre class="lang-cs prettyprint-override"><code>    using System;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace WpfApplication3
    {
        public partial class UserControl1 : UserControl
        {
            public static DependencyProperty TextProperty = DependencyProperty.Register(&quot;Text&quot;, typeof(string), typeof(UserControl1), new PropertyMetadata(null));
            public string Text
            {
                get { return GetValue(TextProperty) as string; }
                set { SetValue(TextProperty, value); }
            }
    
            public UserControl1() { InitializeComponent(); }
        }
    }
</code></pre>

[{'answer_id': 77094, 'author': 'user7116', 'author_id': 7116, 'author_profile': 'https://Stackoverflow.com/users/7116', 'pm_score': 5, 'selected': True, 'text': '<p>That is how we\'re doing it in our team, without the RelativeSource search, rather by naming the UserControl and referencing properties by the UserControl\'s name.</p>\n\n<pre><code>&lt;UserControl x:Class="WpfApplication3.UserControl1" x:Name="UserControl1"\n    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"\n    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;\n    &lt;StackPanel Background="LightCyan"&gt;\n        &lt;TextBox Margin="8" Text="{Binding Path=Text, ElementName=UserControl1}" /&gt;\n    &lt;/StackPanel&gt;\n&lt;/UserControl&gt;\n</code></pre>\n\n<p>Sometimes we\'ve found ourselves making too many things UserControl\'s though, and have often times scaled back our usage. I\'d also follow the tradition of naming things like that textbox along the lines of PART_TextDisplay or something, so that in the future you could template it out yet keep the code-behind the same.</p>\n'}, {'answer_id': 1995911, 'author': 'Lessneek', 'author_id': 240947, 'author_profile': 'https://Stackoverflow.com/users/240947', 'pm_score': 1, 'selected': False, 'text': '<p>You can set DataContext to this in UserControl\'s constructor, then just bind by only path.</p>\n\n<p>CS:</p>\n\n<pre><code>DataContext = this;\n</code></pre>\n\n<p>XAML:</p>\n\n<pre><code>&lt;TextBox Margin="8" Text="{Binding Text} /&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76412', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/317/']