“BindingExpression path error” when binding to a POCO

I'm passing a POCO to a Child Window, and the very first thing it needs to do is display a couple of properties from that object. So, I rattled off the basic code;

        private MachineCredentials machineCreds { get; set; }

        private BackupProviderType backupProvider { get; set; }

 

        public RemoteBrowser(MachineCredentials machineCreds, BackupProviderType backupProvider)

        {

            this.machineCreds = machineCreds;

            this.backupProvider = backupProvider;

           

            InitializeComponent();

        }

 

 and the xaml;

            <TextBlock x:Name="machineDesc" Grid.Row="0" Grid.Column="1" Text="{Binding Machine.Description, Source=machineCreds}" />
            <TextBlock x:Name="credsDesc" Grid.Row="1" Grid.Column="1" Text="{Binding Description, Source=machineCreds}" />
            <TextBlock x:Name="providerDesc" Grid.Row="2" Grid.Column="1" Text="{Binding Description, Source=backupProvider}" />

but, when I run it, the textblocks remain blank and I get the following errors in my output window;

System.Windows.Data Error: BindingExpression path error: 'Machine' property not found on 'machineCreds' 'System.String' (HashCode=-1414592852). BindingExpression: Path='Machine.Description' DataItem='machineCreds' (HashCode=-1414592852); target element is 'System.Windows.Controls.TextBlock' (Name='machineDesc'); target property is 'Text' (type 'System.String')..

System.Windows.Data Error: BindingExpression path error: 'Description' property not found on 'machineCreds' 'System.String' (HashCode=-1414592852). BindingExpression: Path='Description' DataItem='machineCreds' (HashCode=-1414592852); target element is 'System.Windows.Controls.TextBlock' (Name='credsDesc'); target property is 'Text' (type 'System.String')..

System.Windows.Data Error: BindingExpression path error: 'Description' property not found on 'backupProvider' 'System.String' (HashCode=-944648434). BindingExpression: Path='Description' DataItem='backupProvider' (HashCode=-944648434); target element is 'System.Windows.Controls.TextBlock' (Name='providerDesc'); target property is 'Text' (type 'System.String')..

 

That's odd, thought I. Well, I can set the DataContext rather than user Source=, right?

 

            <TextBlock x:Name="machineDesc" Grid.Row="0" Grid.Column="1" Text="{Binding Machine.Description}" DataContext="machineCreds" />

            <TextBlock x:Name="credsDesc" Grid.Row="1" Grid.Column="1" Text="{Binding Description}" DataContext="machineCreds" />

            <TextBlock x:Name="providerDesc" Grid.Row="2" Grid.Column="1" Text="{Binding Description}" DataContext="backupProvider" />

 

Nope, same result. What it turns out to be is that you have to set the DataContext after the InitializeComponent() call. So, changing the code-behind to;

        private MachineCredentials machineCreds { get; set; }
        private BackupProviderType backupProvider { get; set; }
 
        public RemoteBrowser(MachineCredentials machineCreds, BackupProviderType backupProvider)
        {
            this.machineCreds = machineCreds;
            this.backupProvider = backupProvider;
           
            InitializeComponent();
           
            machineDesc.DataContext = this.machineCreds;
            credsDesc.DataContext = this.machineCreds;
            providerDesc.DataContext = this.backupProvider;
        }

and simplifying the xaml to read;

            <TextBlock x:Name="machineDesc" Grid.Row="0" Grid.Column="1" Text="{Binding Machine.Description}" />
            <TextBlock x:Name="credsDesc" Grid.Row="1" Grid.Column="1" Text="{Binding Description}" />
            <TextBlock x:Name="providerDesc" Grid.Row="2" Grid.Column="1" Text="{Binding Description}" />

does the trick.

I'm used to doing this with context loaded data (via RIA) but it didn't occur to me I would still need to do it with POCOs. Obvious in hindsight…

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">