I have a WPF application with various controls, both native and UserControls, bound to an XML object. Here is a snippet of the XAML.
<Window.Resources><XmlDataProvider x:Name="MyData" x:Key="MyData" Source="/Settings.xml" XPath="StrawberryField" DataChanged="XmlDataProvider_DataChanged" IsAsynchronous="False" IsInitialLoadEnabled="True"/> ... ...</Window.Resources> <Grid x:Name="MainGrid"><Grid.DataContext ><Binding Source="{StaticResource MyData}"/></Grid.DataContext> ... ...
<TextBlock Name="EntryAirlockPressure" Style="{StaticResource ModuleDataLabelStyle}" Height="20" Width="50" Margin="255,90,0,0">
<TextBlock.Text>
<Binding Mode="OneWay" XPath="data_type[@Id='conveyor_plc_data']/plc_type[@Id=//active_data/@Value]/plc[@Id='EntryAirlock']/Item[@Id='EACHP']/@Value"/>
</TextBlock.Text>
</TextBlock>
The XML document contains the data to display in the controls. If I change the data in a given XML node's Value attribute, the control properly shows the new value.
Here comes the problem:
I will be receiving copies of the XML document from another process. It will look exactly like the XML document my UI elements are bound to, except the values in the various nodes will be different. I need to quickly replace the XML document with the new incoming XML document without messing up the binding. This would in theory update all the controls which are bound to various nodes' values.
I can't seem to get this to work. I've tried several things, such as
realDoc = incomingDoc
Dim dc As Object = Me.MainGrid.DataContext Me.MainGrid.DataContext = Nothing realDoc = incomingDoc Me.MainGrid.DataContext = realDoc
realDoc = incomingDoc Dim dp As XmlDataProvider dp = DirectCast(Me.FindResource("MyData"), XmlDataProvider) dp.Refresh()
None of these things seem to work. Either the UI controls stay the same, or they lose their bindings entirely, defaulting to original design-time values.
Again, if I simply set an XML node's Value in code, without messing with the binding, everything works as advertised. This is cumbersome, however, because the other process wants to send my application a (newer) copy of my XML document which is the data source for my binding. There are many items to update in my UI. I would be forced to loop through the incoming XML document and feed the values to the current XML document in order to update my UI controls with the new values. I can certainly do this, but it seems labor-intensive.
Isn't there a simple way to 1) Remove the Binding in code, 2) Replace my XML document object with the new document, 3) Re-establish the binding?
TIA for any quick ideas...
Ron Mittelman