none
Binding to a dependency property in a different UserControl RRS feed

  • Question

  • I have a UserControl with a dependency property (of type string). I can add a TextBox to my UserControl and bind its Text property to the DependencyProperty no bother. When I insert my UserControl into a Window and try to bind to the DependencyProperty of the UserControl I get run time binding error 'Cannot find source for binding with reference 'ElementName=myUserControl'. I have made a very simple project to highlight the issue...

    UserControl1.xaml...

    <UserControl 
        x:Class="BindingTest.UserControl1"
        x:Name="myUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel>
            <Button Click="Button_Click">Change the property</Button>                
            <TextBox Text="{Binding ElementName=myUserControl, Path=StringDP}" />
        </StackPanel>
    </UserControl>
    

    UserControl1.cs...

    namespace BindingTest
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }
    
            public String StringDP
            {
                get { return (String)GetValue(StringDPProperty); }
                set { SetValue(StringDPProperty, value); }
            }
    
            public static readonly DependencyProperty StringDPProperty = 
                DependencyProperty.Register("StringDP", typeof(String), typeof(UserControl1), new FrameworkPropertyMetadata("default string"));
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                StringDP = DateTime.Now.ToString("u");
            }
        }
    }
    
    


    Window1.xaml...

    <Window x:Class="BindingTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:BindingTest"
        Title="Window1" Height="300" Width="300">
        <DockPanel>
            <TextBox Text="{Binding ElementName=myUserControl, Path=StringDP}" DockPanel.Dock="Top" Background="LightGreen"/>
            <c:UserControl1></c:UserControl1>
        </DockPanel>
    </Window>
    



     

    Monday, January 30, 2012 2:43 PM

Answers

  • Jeje, Sorry

    The UserControl's Name that you have provided in the UserControl definition is not the same that the user control instance name.

    So, in order to bind the TextBlock's Text property using the element name you need to give a name to each instance of the UserControl1.

     

      <DockPanel>
            <TextBox Text="{Binding ElementName=myUserControl, Path=StringDP}" DockPanel.Dock="Top" Background="LightGreen"/>
            <c:UserControl1 x:Name="myUserControl"></c:UserControl1> <-- This is a new UserControl1 instance
        </DockPanel>

     

    Hope it helps


    • Edited by HomeroThompson Monday, January 30, 2012 3:27 PM
    • Marked as answer by flobad0b Monday, January 30, 2012 3:33 PM
    Monday, January 30, 2012 3:27 PM

All replies

  • Hello, 

    You can use RelativeSource instead of ElementName 

     

    <UserControl 
        x:Class="BindingTest.UserControl1"
        x:Name="myUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel>
            <Button Click="Button_Click">Change the property</Button>                
            <TextBox Text="{Binding Path=StringDP, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}" />
        </StackPanel>
    </UserControl>

    Good Luck

     

     

    Monday, January 30, 2012 3:04 PM
  • I think you have miss understood my problem. Binding inside the UserControl is fine. Binding from the Window to the DP in the UserControl is not.
    • Edited by flobad0b Monday, January 30, 2012 3:20 PM spelling
    Monday, January 30, 2012 3:20 PM
  • Jeje, Sorry

    The UserControl's Name that you have provided in the UserControl definition is not the same that the user control instance name.

    So, in order to bind the TextBlock's Text property using the element name you need to give a name to each instance of the UserControl1.

     

      <DockPanel>
            <TextBox Text="{Binding ElementName=myUserControl, Path=StringDP}" DockPanel.Dock="Top" Background="LightGreen"/>
            <c:UserControl1 x:Name="myUserControl"></c:UserControl1> <-- This is a new UserControl1 instance
        </DockPanel>

     

    Hope it helps


    • Edited by HomeroThompson Monday, January 30, 2012 3:27 PM
    • Marked as answer by flobad0b Monday, January 30, 2012 3:33 PM
    Monday, January 30, 2012 3:27 PM
  • Yes! Thank you.
    Monday, January 30, 2012 3:34 PM