views:

48

answers:

1

I'm struggling to get to grips with WPF, more specifically performing two way binding of an xml file. Should I be using XMLDataProvider or is their another(better) option? The data is displaying fine but when I change an entry the changes aren't reflected in the xml file.

The XML:

    <?xml version="1.0" encoding="utf-8" ?>
<Licence>
 <Market>
  <Name>DAX</Name>
  <Begin>01/01/2010</Begin>
  <End>01/04/2010</End>
 </Market>
 <Market>
  <Name>DJI</Name>
  <Begin>01/07/2010</Begin>
  <End>01/10/2010</End>
 </Market>
</Licence>

The XAML:

<Window x:Class="WpfApplication5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate x:Key="LicenceTemplate"> 
        <Label Content="{Binding XPath=Name}"/>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Grid.DataContext>
        <XmlDataProvider x:Name="XMLData" Source="XMLFile1.xml" XPath="Licence/Market"/>

    </Grid.DataContext>
    <StackPanel>
        <DataGrid x:Name="DataGridLic"  ItemsSource="{Binding}" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="300" CellEditEnding="DataGridLic_CellEditEnding">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="nameColumn" Binding="{Binding XPath=Name, Mode=TwoWay}" Header="Name" Width="100" Foreground="#FFC28383" />
                <DataGridTextColumn x:Name="BegColumn" Binding="{Binding XPath=Begin, Mode=TwoWay}" Header="Begin" Width="100" Foreground="#FFC14040" />
                <DataGridTextColumn x:Name="EndColumn" Binding="{Binding XPath=End, Mode=TwoWay}" Header="End" Width="100" Foreground="#FFC14040" />
            </DataGrid.Columns>
        </DataGrid>


    </StackPanel>
</Grid>

The CodeBehind:

 public MainWindow()
    {
        InitializeComponent();
    }

    private void DataGridLic_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
       XMLData.Document.Save("XMLFile1.xml");
    }

UPDATE: Some useful resources for xml databinding:

http://msdn.microsoft.com/en-us/library/bb669141.aspx

http://msdn.microsoft.com/en-us/library/cc165615.aspx

+1  A: 

The issue is discussed here, incling example:

http://www.codeproject.com/KB/WPF/WpfXml2WayDataBinding.aspx

Andreas
Thanks Andreas. I'd actually already seen that article via Google and couldn't get the example to work.
Chris
Where are you stuck? Maybe we can find the solution.
Andreas
The example only saves it in memory without updating the xml directly, unless you deploy the xml with your application which I don't want to do.
Chris
Do you mean that you want to create a new xml or that the location of the xml is somewhere else or what exactly are you trying to accomplish?BTW: Have you read the comment of Jostein Kjellsen? He suggests to do the saving in the `NodeChanged` event.
Andreas
Sorry Andreas, yes i'd like to create a new xml and write to it. The application will also have another datagrid on a seperate page which can read/write to the new xml file.
Chris
No i've not read any comments from Jostein Kjellsen. Where is his comment?
Chris
What if you manually create a (rudimentary) XML file before saving if it does not exist? I am running out of ideas, sorry...
Andreas
Well I think if the user changes anything in the datagrid i'd like all the data saved as var TheData = ?????(I've no idea what goes here)then I can run a foreach loop and pick and choose the data I store in the new xml file.
Chris
Any ideas on that?
Chris
Maybe `XMLData.Document`? You could then iterate over `ChildNodes`. See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument_members.aspx
Andreas