我有一个从csv文件填充的WPF中的数据网格。 这是通过Linq完成的,并通过CollectionViewSource.Source填充数据网格。来自datagrid的WPF更新
要求是对数据网格中的数据进行更新/更改,然后将其保存回csv。
我需要知道如何保存对数据的任何更改?我一直在玩一些事件和datacontext和类似的东西,但没有任何工作。
我很抱歉,如果这是一个初学者的类型的问题。从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应该将这些值自动保存到文件中吗?或者每当一个按钮被按下或者一样手动的时候?另外,也许向我们展示将数据放入DataGrid的代码会很有帮助。 – Goblin 2010-07-17 18:13:59
自动或通过提交按钮。我只是在努力让自己的头转向它 – Chris 2010-07-17 19:20:18
是您的数据正确显示?因为如果它正在编辑它应该更新您的人物对象,请在属性的其中一个setter上放置一个断点以确保。 – 2010-07-18 23:57:26