The example given here is a simplification of the actual UserControl that I am trying to implement but it illustrates the structure and suffers from the same problem. The user control has a DependencyProperty Words that sets the text of textblock that is defined in the user control XAML.

    public partial class MyControl : UserControl
        public static readonly DependencyProperty WordsProperty = DependencyProperty.Register("Words", typeof(string), typeof(MyControl));
        public MyControl()
        public string Words
            get { return (string)GetValue(WordsProperty); }
                m_TextBlock.Text = value;
                SetValue(WordsProperty, value);

An INotifyPropertyChanged ViewModelBase class derived ViewModel is assigned to the mainWindow DataContext. The ModelText property set calls OnPropertyChanged.

class MainWindow : ViewModelBase
    private string m_ModelString;
    public string ModelText
        get { return m_ModelString; }
            m_ModelString = value;

In the MainWindow XAML binding is made to the UserControl and a TextBlock

 <Window x:Class="Binding.View.MainWindow"
        Title="MainWindow" Height="218" Width="266" xmlns:my="clr-namespace:Binding.View">
        <my:MyControl Words="{Binding ModelText}" HorizontalAlignment="Left" Margin="39,29,0,0" x:Name="myControl1" VerticalAlignment="Top" Height="69" Width="179" Background="#FF96FF96" />
        <TextBlock Height="21" HorizontalAlignment="Left" Margin="59,116,0,0" Name="textBlock1" Text="{Binding ModelText}" VerticalAlignment="Top" Width="104" Background="Yellow" />

The binding works for the textblock but not for the user control. Why can't UserControl DependencyProperty be bound in the same way as Control Properties?

  • You need to format the code block with the {} – paparazzo Sep 19 '12 at 13:25
  • I did try to use {} but obviously I did it wrong. Sorry about that it was my first attempt. – user1683083 Sep 19 '12 at 13:32

The culprit is:

m_TextBlock.Text = value;

WPF does not directly use properties backed by a DP.

If you want to update m_TextBlock.Text when the WordsProperty is modified, either bind that textblock to Words, or use the PropertyChangedCallback in UIPropertyMetadata:

 public static readonly DependencyProperty WordsProperty = 
                                    new UIPRopertyMetadata(
                 new PropertyChangedCallback(
                     (dpo, dpce) => 
                         //Probably going to need to first cast dpo to MyControl
                         //And then assign its m_TextBlock property's text.
                         m_TextBlock.Text = dpce.NewValue as string;

Consider dpo the sender, and dpce the event args.

  • Thanks I tried the PropertyChangedCallback and it works perfectly. – user1683083 Sep 19 '12 at 14:17
  • @user1683083 Please consider accepting this answer then ;) (click the tick at the top left of the answer) – Louis Kottmann Sep 19 '12 at 14:24

I think that you should to assign the text to your UserControl's textbox by a binding and not by m_TextBlock.Text = value;.

Maybe you could user a binding like this in your UserControl Xaml:

    Text="{Binding Words
  • I can see your point in the case of the example I gave. The actual user control that I want to implement is more complicated though. It uses a canvas to render a foreground bitmap over a background bitmap using translation and rotation transforms, but the transform coordinates come from the model. I planned to add a dependency property to the user control to allow the model to notify the user control by supplying a class containing x,y,theta then use this data to modify the transforms, but I can't get the binding to work. – user1683083 Sep 19 '12 at 14:01
  • I think the better idea could be to set x,y,theta properties as DependencyProperties in the user control, and then make bindings in the view model to them. Then (like Baboon said) you can to subscribe to the dependency property's OnPropertyChange callback. – Raúl Otaño Sep 19 '12 at 14:16
  • My idea is that you can also use bindings to the same user control's dependency properties in this one's view (xaml). – Raúl Otaño Sep 19 '12 at 14:19
  • Yes, that sound a better approach to build a view model for the user control and bind each property separately rather than coding the view. – user1683083 Sep 19 '12 at 14:26
  • This is the idea – Raúl Otaño Sep 19 '12 at 14:35

Your Answer

