views:

77

answers:

1

I have the following test case xaml.

<Window.Resources>
    <XmlDataProvider x:Key="testDS1">
        <x:XData>
            <root xmlns="">
                <item>1</item>
                <item>1</item>
            </root>
        </x:XData>
    </XmlDataProvider>
</Window.Resources>
<StackPanel>
    <ListBox ItemsSource="{Binding Source={StaticResource testDS1},XPath=/root/item}"/>
    <Button Content="Change" Click="OnChangeClicked"/>
</StackPanel>

This displays a listbox of numbers. I then execute this code.

    public void OnChangeClicked(object sender, RoutedEventArgs e)
    {
        XmlDataProvider ds = Resources["testDS1"] as XmlDataProvider;
        string xml = "<root><item>1</item></root>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        ds.Document = doc;
    }

This causes this warning to appear.

System.Windows.Data Error: 43 : BindingExpression with XPath cannot bind to non-XML object.; XPath='/root/item' BindingExpression:Path=; DataItem='XmlDataCollection' (HashCode=40644060); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') XmlDataCollection:'MS.Internal.Data.XmlDataCollection'

However, the ListBox is bound correctly and has the correct values. From reading this thread, its mentioned that this behaivour is normal and the binding expression has reattached. How do i eliminate this warning ? I have tried BindingOperations.ClearBinding but even that triggered this warning. Should i just live with this warning ?

A: 

Finally, found an answer

public void OnChangeClicked(object sender, RoutedEventArgs e)
{
    XmlDataProvider ds = Resources["testDS1"] as XmlDataProvider;
    string xml = "<root><item>1</item></root>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    using(ds.DeferRefresh())
    {
       ds.Document = doc;
       ds.XmlNamespaceManager = new XmlNamespaceManager(doc.NameTable);
    }
}
Andrew Keith