I have bound a list to a DataGrid. After modifying the list in the DataGrid I'd like to save the list in a xml file. how can I get access to the list in the c# code?
In other words I wanna get the content of Welle1 after clicking on a Button.
InitializeComponent();
List<Wellenelement> we1 = new List<Wellenelement>();
Welle Welle1 = new Welle
{
Elemente = we1
};
dataGrid.DataContext = Welle1;
```c#
So, first of all, using WPF, you'll have to make use of Properties and the PropertyChangedEvent.
Go to your MainWindow.xaml.cs (or to your ViewModel, if you already use MVVM) and add beneath your Constructor (usually
public MainWindow(){ //[...])you'll also have to add
using System.ComponentModel;to your usings to find the required classes.Next you add a new
Propertyright above your Constructor like this:Note: I'll suggest using an
ObservableCollectionover aList, if you want to change your ItemsSource at runtime. (You have to addusing System.Collections.ObjectModel;to get the class)Now you can bind your
DataGridto yourObservableCollection:Now you can do whatever you want with your List in code behind like: