简体   繁体   中英

refresh combobox when xmldataprovider is updated

i have this code:

<Grid>
        <Grid.Resources>
            <XmlDataProvider x:Name="ScenesXmlName" x:Key="ScenesXml"
                    XPath="scenari-list/scenario"
                    Source="myXml.xml"/>
        </Grid.Resources>

        <ComboBox Name="ScenariCombo"
                  ItemsSource="{Binding Source={StaticResource ScenesXml}}"
                  DisplayMemberPath="@name"
                  SelectionChanged="ScenariCombo_SelectionChanged" />
</Grid>

Combobox items are loaded correctly.
What i wanto to know is if there is any way to update ScenariCombo.Items when i update myXml.xml (so the itemsource).
Thanks in advance!

One option could be to watch for changes in XML file through FileSystemWatcher and update the XmlDataProvider once changes are made in XML file.

On little googling I found this . A custom XMLDataProvider with built-in functionality of looking for any changes in XML file through FileSystemWatcher .

public class MyXmlDataProvider : XmlDataProvider
    {
        public new Uri Source
        {
            get { return base.Source; }
            set
            {
                base.Source = value;

                FileSystemWatcher watcher = new FileSystemWatcher();
                //set the path of the XML file appropriately as per your requirements
                watcher.Path = AppDomain.CurrentDomain.BaseDirectory;

                //name of the file i am watching
                watcher.Filter = value.OriginalString;

                //watch for file changed events so that we can refresh the data provider
                watcher.Changed += new FileSystemEventHandler(file_Changed);

                //finally, don't forget to enable watching, else the events won't fire           
                watcher.EnableRaisingEvents = true;
            }
        }

        void file_Changed(object sender, FileSystemEventArgs e)
        {
            base.Refresh();
        }
    }

It will work in your scenario. Take a look at linked article for example and more details

Edit

You can do two things.

Either create a property containing source file path and bind it with Source property of XMLDataProvider. Once the file gets changes raise a property changed event so that XML Data provider updates/reloads its source (I haven't tested this thing)

Or

Update the XMLDataProvider through code by resetting the source to same file.

Having said that I must say this is not the right way of playing with XML. Ideally you should load XML in some data structure like Observable collection then use the property changed notification to refresh the data in your control

I finally understood this. It was easier than expected. You have to force the reloading of the xmldataprovider:

XmlDataProvider xmlDataProvider = (XmlDataProvider)BaseGrid.FindResource("ScenesXml");
xmlDataProvider.Refresh();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM