Received this question:
I need to bind some objects to an XML file – and refresh the binding whenever the XML source changes. By default my application (c# .exe) reads the source values romthe XML once at start-up, and doesn’t notice subsequent changes.
I didn't see an easy way to do this in Markup but with two lines of code you can call the refresh on the XMLDataProvider:
private void Button1click(object sender, RoutedEventArgs e)
{
XmlDataProvider DataProv = (XmlDataProvider)this.Window.FindResource("QuestionsDS");DataProv.Refresh();//this.LayoutRoot.DataContext = DataProv;
}
And the markup is:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="UntitledProject18.Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480">
<Window.Resources>
<XmlDataProvider x:Name="Chuck" x:Key="QuestionsDS" d:IsDataSource="True" Source="C:\teched\XMLFile1.xml"/>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<ListBox Margin="144,93,280,151" ItemsSource="{Binding Mode=Default, Source={StaticResource QuestionsDS}, XPath=/Questions/Question/@Wrong_answer2}"/>
<Button HorizontalAlignment="Left" Margin="88,23,0,0" VerticalAlignment="Top" Width="154" Height="57" Content="Button" x:Name="Button1" Click="Button1click"/>
</Grid>
</Window>