1

I've been following this tutorial and bound my Datagrid successfully to an XML data provider resource.

My grid didn't update when the xmlfile (that the xmldataprovider was referenced to) changed.

So I followed this guy's tutorial and while I could get a notice every time the file changed (the tutorial offers a custom class 'myxmldataprovider' that extends xmldataprovider and has a filesystem watcher listening for file change. There is a function in the class called "on file changed" and it is accessed when the XML file is changed). Using refresh in the file changed function, as suggested by 2nd tutorial didn't do anything to update the datagrid or the dataprovider's content.

I'm really late on my project.. and this seems to be the sole thing keeping me from moving on. I've searched the internet and browsed countless forums. and have yet to find a clear answer that works.

custom class

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 = Path.GetFullPath(Path.Combine(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();

}

xaml

<local:MyXmlDataProvider Source="XMLFile123.xml"
                     XPath="firstnode" x:Key="xml" />
</Window.Resources>
<DataGrid Name="CustomerGrid" AutoGenerateColumns="False"
              ItemsSource="{Binding Source={StaticResource xml},XPath=*}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding XPath=@attribute1}"
                                Header="attribute1" />
            <DataGridTextColumn Binding="{Binding XPath=@attribute2}"
                                Header="attribute2" />
            <DataGridTextColumn Binding="{Binding XPath=@attribute3}"
                                Header="attribute3" />
            <DataGridTextColumn Binding="{Binding XPath=@attribute4"
                                Header="attribute4" />
        </DataGrid.Columns>
    </DataGrid>
Mat
  • 41
  • 1
  • 5
  • Can you please post the code you are trying to use instead of just describing it? – slasky Aug 31 '17 at 17:13
  • edited. code is in post. – Mat Aug 31 '17 at 17:40
  • `XmlDataProvider` inherits from `DataSourceProvider`, which implements `INotifyPropertyChanged`, and has a [protected `OnPropertyChanged()` method](https://msdn.microsoft.com/en-us/library/system.windows.data.datasourceprovider.onpropertychanged(v=vs.110).aspx) that your subclass can call to notify consumers of any changes. I guess you'd call that for property `"Data"` when the data changes. But maybe `base.refresh();` does the needful already. – 15ee8f99-57ff-4f92-890c-b56153 Aug 31 '17 at 17:46
  • i tried the base.refresh(). – Mat Aug 31 '17 at 17:57
  • sadly, i didn't see any change in the datagrid or the xmldataprovier's content – Mat Aug 31 '17 at 17:57
  • i'll try the on propertychanged method, but i fear it requires me to create a new dependency property. seeing as data is kept as an object and not a dependency property – Mat Aug 31 '17 at 18:01

0 Answers0