我有一個從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