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>