WPF双向绑定XML [英] WPF two-way binding XML

查看:183
本文介绍了WPF双向绑定XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我挣扎得到认真处理WPF,更具体的执行双向的XML文件的约束力。我应该使用XMLDataProvider或者是他们的另一个(更好)的选项?
该数据显示正常,但当我更改条目的更改不会反映在XML文件中。

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.

中的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>

在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>

在codeBehind:

The CodeBehind:

 public MainWindow()
    {
        InitializeComponent();
    }

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

更新:
对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

推荐答案

使用XMLDataProvider没问题。你只需要确保你读取和写入同一个XML文件。

No problem using the XMLDataProvider. You just need to make sure you are reading and writing to the same XML file.

只需更新code如下:

Just update your code as follows;

public MainWindow()
{
    InitializeComponent();
    var xmlFilePath = @"c:\whatever\XMLFile1.xml";
    XMLData.Source = new Uri(xmlFilePath);
}

private void DataGridLic_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
   var xmlSource = XMLData.Source.LocalPath;
   XMLData.Document.Save(xmlSource);
}

这篇关于WPF双向绑定XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆