【发布时间】:2010-07-17 13:25:34
【问题描述】:
我在从 csv 文件填充的 WPF 中有一个数据网格。 这是通过 Linq 完成的,并通过 CollectionViewSource.Source 填充数据网格。
要求对数据网格中的数据进行的任何更新/更改都保存回 csv。
我需要知道如何保存对数据的任何更改?我一直在玩一些事件和数据上下文等,但还没有任何效果。
如果这是初学者的问题,我深表歉意。从 Windows 应用程序迁移到 WPF 是一个陡峭的学习曲线(至少对我而言)。 我现在只是从一个数组中填充,同时我试图弄清楚。基本上就是想把数据再拿出来,另存为var。
System.Windows.Data.CollectionViewSource personViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personViewSource")));
List<Person> T = new List<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;
T.Add(pers);
}
personViewSource.Source = T;
xaml:
<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" 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>
谢谢
【问题讨论】:
-
您希望它如何工作?数据网格是否应该自动将值保存到文件中?或者每当按下按钮或同样手动的东西时?此外,也许首先向我们展示将数据导入 DataGrid 的代码会有所帮助。
-
自动或通过提交按钮。我只是在努力解决它
-
您的数据显示正确吗?因为如果它正在编辑它应该更新您的人员对象,请在属性的设置器之一上放置一个断点以确保。
-
用上面的代码保证数据正确显示。
-
不用担心,克里斯,对我的回答进行了编辑