0

In my project I am binding to an XML file. There are several bindings: one to textBox and one to a list and one to a UserControl. All apart from the UserControl works, but in the output I get error for all 3 bindings rather than 2.

The error looks like this:

System.Windows.Data Error: 43 : BindingExpression with XPath cannot bind to non-XML object.; XPath='/datlg/@timeStamp' BindingExpression:Path=/InnerText; DataItem='XmlDataCollection' (HashCode=199777); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') XmlDataCollection:'MS.Internal.Data.XmlDataCollection'

My textBlock looks this:

<TextBlock Margin="0" 
           Text="{Binding XPath=/datlg/@timeStamp}" 
           TextWrapping="Wrap"/>

And here is the code for the user control:

string xPath = String.Format("datlg/Devl/dev[@id='{0}']", id);
((XmlDataProvider)this.DataContext).XPath = xPath;
Binding b = new Binding();
b.Source = (XmlDataProvider)this.DataContext;
b.XPath = "prml/prm[@id='1']";
//b.Converter = (IValueConverter)this.Resources["IntToBoolConverter"];
b.Mode = BindingMode.OneWay;
//this.SetBinding(VacuumPumpControl.IsPoweredOnProperty, b);
testBox.SetBinding(TextBlock.TextProperty, b);

Note: for the test purposes I have temporarily bound data to textBox to see if binding is working (e.g. is XPath set to the right one, etc.). And it works if it bound to textBox, but it doesn't if it is bound to DP.

Commented lines are the ones which doesn't work.

And as all (working and not working) binding give an error, this is hard to find out why my user control's binding isn't working.

Any ideas why all bindings give an error and why binding to DP inside a UserControl doesn't work?

1 Answer 1

4

It's possible that the errors are simply due to deferred loading. It's very common to see transient data binding errors. You could try adding this code to enable verbose binding information for that particular binding:

PresentationTraceSources.SetTraceLevel(b, PresentationTraceLevel.High);

Add that before you call SetBinding. Try single stepping through the code. If you see the error message you describe at the point at which you call SetBinding, and then if you hit F5, you see a load more binding messages appearing, particularly if it includes a DataChanged event from the XmlDataProvider, then this is almost certainly the reason.

If you really don't want to see these transient errors, you would need to create these bindings after the XmlDataProvider has finished loading its data.

As for the failing binding, it's hard to be certain without seeing all the code, but it may be down to a type mismatch. If your IntToBoolConverter really expects an integer, then it'll be disappointed - an XML data source always provides values as strings. What does your converter look like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.