0

I can't figure out why I keep getting this error, and when I make a change to tb_Name TextBox in the GUI the Set in Name never gets called.

System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''String' (HashCode=2106982518)'. BindingExpression:Path=Name; DataItem='String' (HashCode=2106982518); target element is 'TextBox' (Name='tb_Name'); target property is 'Text' (type 'String')

XAML

<UserControl
             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:ConfigStudioUI.Controls" x:Class="ConfigStudioUI.Controls.DeviceTypeTabCtrl" 
             mc:Ignorable="d"  x:Name="DeviceTypeUC" Loaded="DeviceType_Loaded"
             d:DesignHeight="400" d:DesignWidth="600" Background="#FF00C8FF"  
           >
    <Grid>
    <TabControl Background="#FF00FF99" FontSize="14" 
    TabStripPlacement="Left" Margin="0, 0, 0, 10" >
    <TabItem Name="PropertiesTab" Header="Properties">
    <Grid>
    <Grid  >
    <TextBox Text="{Binding Source=DeviceType, Path=Name, Mode=TwoWay}" 
    TabIndex="0" x:Name="tb_Name" HorizontalAlignment="Stretch" Height="32" 
        Margin="159,28,5.2,0"  VerticalAlignment="Top"  />
    </Grid>
                </Grid>
            </TabItem>
     </TabControl>
</UserControl>

Code Behind

public partial class DeviceTypeTabCtrl : UserControl
{
public DeviceType DeviceType { get; set; }

public DeviceTypeTabCtrl(DeviceType deviceTypeObject, DeviceTypeGroup 
deviceTypeGroupObject)
    {
        InitializeComponent();
        DataContext = this;
        this.DeviceType = new DeviceType();
        this.DeviceType = deviceTypeObject;
        this.tb_Name.Text = deviceTypeObject.Name;
        this.DeviceType.DeviceTypeGroupGUID = 
        deviceTypeGroupObject.DeviceTypeGroupGUID;
    }
}

public class DeviceType : INotifyPropertyChanged
{
    /// <summary>
    /// Name
    /// </summary>
    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }
}
techmakin
  • 73
  • 1
  • 9
  • 1
    The error seems to be indicating that the `DeviceType` of your binding's `Source` is a `String`, not an instance of your `DeviceType` class (that's why it says `Name` doesn't exist). Do you have a special reason for using `Source` on your binding, instead of letting the `DataContext` flow naturally? Is it possible you have a `Resource` defined somewhere in your XAML with a `key` of "ResourceType"? If so, it would be picking that up. – Bradley Uffner Aug 16 '17 at 14:15
  • Could you show us more of the XAML from your `UserControl`? I'd like to see how all ancestors of your `TextBox` are bound, and where the `DataContext` is coming from. – Bradley Uffner Aug 16 '17 at 14:21
  • NotifyPropertyChanged, shouldn't that be OnPropertyChanged? – stuicidle Aug 16 '17 at 14:34
  • @stuicidle It can be anything that eventually raises the `PropertyChanged` event. Either way, it is unrelated to his error, as the error is happening when the binding is created, well before any notifications are expected to happen. – Bradley Uffner Aug 16 '17 at 15:05
  • Added more of the XAML Code, @BradleyUffner, I'm not sure what you meant by letting the DataContext flow naturally? I'm still a newbie at this wpf binding stuff. – techmakin Aug 16 '17 at 17:02

1 Answers1

0

I ended up finding the answer here: How To Bind a Property to Textbox using MVVM and MVVM toolkit?

I also updated the object name (DeviceTypeObj) to be more clear.

<TextBox Text="{Binding DeviceTypeObj.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TabIndex="0" 
                                 x:Name="tb_Name" HorizontalAlignment="Stretch" Height="32" Margin="159,28,5.2,0"  VerticalAlignment="Top"  />
techmakin
  • 73
  • 1
  • 9