Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That is correct. Binding to dependency properties is quite simple to do. Understanding the mechanics I would suggest looking through MSDN. However to answer your question you provide static dependency properties registered to the user control. Then your getters \ setters reference the property.</p> <p>Here is a sample line of a dependency property.</p> <pre><code>/// &lt;summary&gt; /// Provides a bindable text property to the user control /// &lt;/summary&gt; public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new PropertyMetadata("", onTextPropertyChanged)); /// &lt;summary&gt; /// optional static call back handler when the property changed /// &lt;/summary&gt; /// &lt;param name="o"&gt;&lt;/param&gt; /// &lt;param name="e"&gt;&lt;/param&gt; static void onTextPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { var obj = o as UserControl1; if (obj == null) return; //TODO: Changed... } /// &lt;summary&gt; /// Gets \ sets the text /// &lt;/summary&gt; public string Text { get { return (string)this.GetValue(TextProperty); } set { if (this.Text != value) this.SetValue(TextProperty, value); } } </code></pre> <p>The above is very simple. We register a dependency property <code>TextProperty</code> to <code>UserControl1</code>, this property is the type of string and has a default value of "" (as noted in the property meta data). I also provided a static callback handler if you wish to perform additional steps once the property has changed.</p> <p>You will then see the Text property uses the <code>GetValue()</code> and <code>SetValue()</code> methods for getting and setting the value of the Text Property.</p> <p><strong>UPDATE: Binding to a child element in XAML.</strong></p> <p>This update is to show how to use the the above TextProperty for binding. </p> <p>Usercontrol1.Xaml. This is the XAML for UserControl1. </p> <pre><code>&lt;UserControl x:Class="WpfApplication1.UserControl1" 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" mc:Ignorable="d" DataContext="{Binding RelativeSource={RelativeSource Self}}" d:DesignHeight="300" d:DesignWidth="300"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Label Grid.Column="0" FontWeight="Bold" Content="Text" VerticalAlignment="Center" /&gt; &lt;TextBox Text="{Binding Text, Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" Padding="4" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>My Main Window View Model (Implement INotifyPropertyChanged)</p> <pre><code>public class MainWindowModel : INotifyPropertyChanged { /// &lt;summary&gt; /// the text /// &lt;/summary&gt; string myProperty = "This is the default text"; /// &lt;summary&gt; /// Gets \ sets the text /// &lt;/summary&gt; public string MyProperty { get { return this.myProperty; } set { if (this.MyProperty != value) { this.myProperty = value; this.OnPropertyChanged("MyProperty"); } } } /// &lt;summary&gt; /// fires the property changed event /// &lt;/summary&gt; /// &lt;param name="propertyName"&gt;&lt;/param&gt; private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } /// &lt;summary&gt; /// the property changed event /// &lt;/summary&gt; public event PropertyChangedEventHandler PropertyChanged; } </code></pre> <p>MainWindow.Xaml. Binding the text property to the view model</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:ctrl="clr-namespace:WpfApplication1" Name="Window1" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.DataContext&gt; &lt;ctrl:MainWindowModel /&gt; &lt;/Window.DataContext&gt; &lt;Grid&gt; &lt;ctrl:UserControl1 Text="{Binding Path=DataContext.MyProperty, Mode=TwoWay, ElementName=Window1}" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload