locked
user control binding error RRS feed

  • Question

  • getting the following error when binding to a simple user control from a window.

    System.Windows.Data Error: 40 : 
    BindingExpression path error: 'FirstName' property not found on 'object' ''Control2' (Name='')'. 
    BindingExpression:Path=FirstName; DataItem='Control2' (Name=''); 
    target element is 'Control2' (Name=''); target property is 'CustName' (type 'String')

    FirstName is a string property of the window. I can bind it to the Text property of a TextBlock. But get the error when binding to a dependency property of the user control. 

    Why the binding error? How to go about finding the cause?

    ( for the data binding. in the Window I set the DataContext of the Grid to this ( the window class ).  And in the User Control I set the DataContext of the user control to this, the user control class. )

    thanks,

    <UserControl x:Class="TelnetTester.Windows.Control2"
                 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:TelnetTester.Windows"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
          <Label>Cust name:</Label>
          <TextBlock Text="{Binding CustName}"/>
        </StackPanel>
      </Grid>
    </UserControl>
    
      public partial class Control2 : UserControl
      {
        public Control2()
        {
          InitializeComponent();
          this.DataContext = this;
        }
    
        public string CustName
        {
          get { return (string)GetValue(CustNameProperty); }
          set { SetValue(CustNameProperty, value); }
        }
    
        public static readonly DependencyProperty CustNameProperty =
        DependencyProperty.Register("CustName", typeof(string),
        typeof(Control2), new PropertyMetadata(""));
    
    
      }
    
    
    <Window x:Class="TelnetTester.Windows.Window2"
            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:TelnetTester.Windows"
            mc:Ignorable="d"
            Title="Window2" Height="300" Width="300">
        <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
          <RowDefinition Height="auto"></RowDefinition>
          <RowDefinition Height="auto"></RowDefinition>
          <RowDefinition Height="auto"></RowDefinition>
          <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
          <local:Control2 Grid.Row="0" CustName="{Binding Path=FirstName}"/>
        <TextBox x:Name="tb1" Grid.Row="1" Text="{Binding FirstName}"/>
      </Grid>
    </Window>
    
    namespace TelnetTester.Windows
    {
      /// <summary>
      /// Interaction logic for Window2.xaml
      /// </summary>
      public partial class Window2 : Window
      {
        public Window2()
        {
          InitializeComponent();
          Loaded += Window2_Loaded;
        }
    
        private void Window2_Loaded(object sender, RoutedEventArgs e)
        {
          this.FirstName = "Steve";
          this.LayoutRoot.DataContext = this;
        }
    
        public string FirstName
        { get; set; }
      }
    }
    

    Wednesday, September 21, 2016 7:29 PM

Answers

  • take the setting of the datacontext in the usercontrol out.  It should inherit from its owner.  If you set it, it will look for FirstName in the UC.

    Lloyd Sheen

    • Marked as answer by Steve Richter Thursday, September 22, 2016 3:57 AM
    Wednesday, September 21, 2016 10:36 PM