SelectedItem of Listview is lost when refreshing XML DataProvider

867 Views Asked by At

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();

...

1

There are 1 best solutions below

0
On
  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.