0

I have a Listview which is bound to a XML file. The values of the different nodes of the XML files can be changed using Textboxes. If I change the value of an element which is directly bound to the element in the listview everything works fine. The problem starts if I change values in the XML file depending on other values and then I have to refresh the XML dataprovider to see the changes in the listbox. If I do so, then my previously SelectedItem is lost. I also tried to save the SelectedItem and reload it after refreshing, but it didn't work.

Is there an alternative method to refresh a XML dataprovider? Or is there a better way to refreh the ListView?

in Window1.xaml:
...

       <Grid.DataContext>
            <XmlDataProvider x:Name="VoltageData" Source="Voltages.xml" XPath="Voltages/Voltage" />
       </Grid.DataContext>


...
in Window1.xaml.cs:
...

        xmlDoc.Save(VoltageData.Source.LocalPath);
        VoltageData.Refresh();

...

  • XML provider does not implement INotifyPropertyChanged. If you have a know structure what you can do is load the XML into a slass that implements INotifyPropertyChanged then put that class in an ObservableCollection and bind that to the ListView. In the Set of the class property is where you would update the XML. – paparazzo Jan 17 '12 at 18:10

1 Answers1

0
  1. Before refresh get the selected item ( I prefer SelectedValue) of the List View.
  2. After refresh set the same selected item (or SelectedValue) to the ListView.

This way it will maintain the selection. If the selected Item (or Value) is bound using WPF binding then update the object \ property representing the source path of the binding.

WPF-it
  • 19,625
  • 8
  • 55
  • 71