Search code examples
c#xmlwpfxmldataprovider

Set XMLDataProvider source dynamically


While all examples and sources I found is to set the resource in XAML statically, I would only know in run time the name of the XML file to be connected with XMLDataProvider. Is there a way to set either in code behind or in XAML?

<Window.Resources>
    <XmlDataProvider x:Key="XMLFoo" Source="Foo.xml" XPath="Foo"/>
</Window.Resources>

It could be Foo.xml, or could be Goo.xml.


Solution

  • Yes you can change that while runtime. Unfortunately you cannot bind it, so you have to do stuff in Code-Behind.

    Here's a simple example:

    (this.Resources["XMLFoo"] as XmlDataProvider).Source = new Uri("Goo.xml");
    

    Cheers