Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3320874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:59:43+00:00 2026-05-17T22:59:43+00:00

I have a WPF app that binds a DataGrid to an XML document using

  • 0

I have a WPF app that binds a DataGrid to an XML document using XmlDataProvider. Everything works fine except that I’m trying to “save”/write to XML file on NodeChanged event, instead of having a button or other user-triggered mechanism. Unfortunately, NodeChanged handler that I created is not being picked up. Would appreciate any help to figure out why it’s not fired.

XAML:

  <Grid Name="Grid" Loaded="Grid_Loaded">
    <Grid.DataContext>
      <XmlDataProvider x:Name="MyData" Source="Data.xml" XPath="Nodes/Node" />
    </Grid.DataContext>
    <DataGrid AutoGenerateColumns="False" Name="dataGrid1" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" SelectionUnit="FullRow" SelectionMode="Single" Margin="0,0,0,50">
      <DataGrid.Columns>
        <DataGridTextColumn Header="Header" Binding="{Binding XPath=@Value, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
      </DataGrid.Columns>
    </DataGrid>
    <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="416,276,0,0" Name="SaveButton" VerticalAlignment="Top" Width="75" Click="SaveButton_Click" />
  </Grid>

Code-behind:


namespace WpfApplication1
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
      MyData.Source = new Uri(appPath + "/Data.xml");
    }

    private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
      string source = MyData.Source.LocalPath;
      MyData.Document.Save(source); 
    }

    private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
      MyData.Document.NodeChanged += new XmlNodeChangedEventHandler(Document_NodeChanged);
    }

    void Document_NodeChanged(object sender, XmlNodeChangedEventArgs e)
    {
      MessageBox.Show("Node changed");
    }
  }
}

XML data file:

<Nodes>
  <Node Value="test1" />
  <Node Value="test3" />
  <Node Value="ttt" />
</Nodes>

To reproduce, you can just paste the above into a new WPF app, name XML data file Data.xml, place it into bin/Debug of the project, and add it to the project as “existing item”.

UPDATE

Thanks Robert for pointing out the issue!

Here’s an even more interesting situation. If you just add a LayoutUpdated event handler to the MainWindow to my sample code as below, you will get very weird behavior in that NodeChanged event will actually be fired if a totally unrelated MessageBox is displayed:

public partial class MainWindow : Window
{
    private bool event_activated = false;

    public MainWindow()
    {
        InitializeComponent();
        string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
        MyData.Source = new Uri(appPath + "/Data.xml");
    }

    private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        string source = MyData.Source.LocalPath;
        MyData.Document.Save(source);  
    }

    private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        MyData.Document.NodeChanged += new XmlNodeChangedEventHandler(Document_NodeChanged);
    }

    private void Document_NodeChanged(object sender, XmlNodeChangedEventArgs e)
    {
        MessageBox.Show("Node changed");
    }

    private void Window_LayoutUpdated(object sender, EventArgs e)
    {       
        if (!event_activated)
        {
            event_activated = true;
            MessageBox.Show("LayoutUpdated"); // <-- NodeChanged is fired with this one line of code, and not fired if you comment this line out!!
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:59:44+00:00Added an answer on May 17, 2026 at 10:59 pm

    It doesn’t fire because the value of the node doesn’t change, only the attribute that happens to be named Value. It is stated in the documentation:

    Occurs when the Value of a node belonging to this document has been changed.
    …
    This event only applies to nodes that have a value.

    How about using DataGrid.CellEditEnding or DataGrid.RowEditEnding instead?

    Update:

    You are correct, the NodeChanged will fire for attributes as well. I did find the problem in your nice repro app though. If I comment out your constructor code, it all works. You already set the source in Xaml. It seems that this code fires an internally handled exception which puts the XmlDataProvider in a corrupt state.

      public MainWindow()
      {
         InitializeComponent();
         //string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
         //MyData.Source = new Uri(appPath + "/Data.xml");
      }
    

    You can see the exception in debug output. WPF seems to like to fail silently and leave you wondering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF app that is using the MVVM pattern. Hooking up buttons
I have a WPF app that works beautifully if I debug (F5) in Visual
I'm having an issue using a new WPF app that is trying to display
I have a WPF app that uploads XML data to a web service asynchronously.
I have a WPF MVVM app that contains an editable DataGrid. I am subscribing
I have a WPF App that implements a ListView. I would like to show
I have a wpf app that needs to communicate(exchange data) with a custom designed
We have a WPF app that allows our users to download encrypted content and
I have a WPF app, my purpose is that the app can be automatically
I have a folder in my WPF app Images that has several .png files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.