Вы можете прослушать событие, когда ячейка заканчивает редактирование, а затем сохранить ваш источник данных.т.е. поместите это в ваш конструктор элемента управления, который содержит сетку (вероятно, пользовательский элемент управления, окно, страницу и т. д.) после вызова InitializeComponent ()
this.myDataGrid.CellEditEnding += new EventHandler<DataGridCellEditEndingEventArgs>(grid_CellEditEnding);
, а затем обработчик сохранит источник данных
void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
//save my data source
}
лично я предпочитаю подход, при котором вы можете выполнить свои правки, а затем нажать «Сохранить» в конце, но в вашем случае вы можете использовать String.Join, чтобы создать строку CSV, а затем записать ее в файл.
Чтобы сделать это, создайте свойство, которое будет вашим списком, чтобы ваше построение ваших данных для сетки выглядело бы так:
public Collection<Person> MyPersonDataSource {get; private set; }
public MyWindowsConstructor() {
//build the grid data before you initialize the window, as the PersonDataSource
//does not implement NotifyPropertyChanged, if you build the data afterwards
//the binding won't be updated
BuildGridData();
InitializeComponent();
}
private void BuildGridData(){
this.MyPersonDataSource = new Collection<Person>();
Person p = new Person();
string[] str = new string[] { "Stacey", "Olivia", "Dylan", "Lauryn", "Beth", "Caitlin" };
var data = from s in str
select s;
Person pers;
foreach (var d in data)
{
pers = new Person();
pers.Name = d;
pers.Age = 22;
this.MyPersonDataSource.Add(pers);
}
}
, затем в функции редактирования конца ячейки
void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
//save my data source
var nameArray = this.MyPersonDataSource.Select(item => item.Name).ToArray();
//create the csv string
String csvString = String.Join("," nameArray);
//write it to a file
System.IO.File.WriteAllText(@"C:\SomeFolderYouHavePermissionsOn\names.csv", csvString);
}
Я бы привязал свою сетку прямо к свойству MyPersonDataSource, вот так ..
<Window x:Class="WpfApplication4.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" Loaded="Window_Loaded" Name="win1" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:WpfApplication4">
<Window.Resources>
<CollectionViewSource x:Key="personViewSource" Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=MyPersonDataSource}" d:DesignSource="{d:DesignInstance my:Person, CreateList=True}" />
</Window.Resources>
<StackPanel Width="369" Height="230" DataContext="{StaticResource personViewSource}">
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Name="personDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Width="88" HorizontalAlignment="Left" BorderThickness="4" Background="#FFF8C5C5" SelectionChanged="personDataGrid_SelectionChanged" TextInput="personDataGrid_TextInput" RowEditEnding="personDataGrid_RowEditEnding" TargetUpdated="personDataGrid_TargetUpdated">
<DataGrid.Columns>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name, Mode=TwoWay, NotifyOnTargetUpdated=True}" Header="Name" Width="SizeToHeader" />
<DataGridTextColumn x:Name="ageColumn" Binding="{Binding Path=Age}" Header="Age" Width="SizeToHeader" Foreground="#FFC14040" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Window>
И я бы, вероятно, посмотрел на более надежное хранилище данных, чем CSV, вы можете использовать xml и bindк нему с помощью XPath, но я не использовал это достаточно, чтобы сформулировать соответствующий ответ.