I have a UserControl where I want to bind textboxes to an XmlDocument. The important parts of the xaml-code looks like:
...
<UserControl.DataContext>
<XmlDataProvider x:Name="Data" XPath="employee"/>
</UserControl.DataContext>
...
<TextBox Text={Binding XPath=general/description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
...
In the constructor of the usercontrol I have the following lines:
string xmlPath = System.IO.Path.Combine(Thread.GetDomain().BaseDirectory, "Data", "TestXml.xml");
FileStream stream = new FileStream(xmlPath, FileMode.Open);
this.Data.Document = new XmlDocument();
this.Data.Document.Load(stream);
If I changed textbox-text, the XmlDocument Data is not updated. What do I have to do, to achieve this?
The above code worked for me. Instead of using a stream, I used a hard-coded data.
XAML File :
<Window x:Class="TestWPFApp.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.DataContext>
<XmlDataProvider x:Name="Data" XPath="employee"/>
</Window.DataContext>
<Grid>
<StackPanel Orientation="Vertical">
<TextBox Width="100" Foreground="Red" Height="20" Text="{Binding XPath=general/description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="Test" Width="50" Height="20" Click="Button_Click"></Button>
</StackPanel>
</Grid>
</Window>
Code Behind :
using System.Windows;
using System.Xml;
namespace TestWPFApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Data.Document = new XmlDocument();
this.Data.Document.LoadXml(@"<employee><general><description>Test Description</description></general></employee>");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var data = this.Data.Document.SelectSingleNode("descendant::description").InnerText;
}
}
}