2017-06-13 6 views
2

私はしばらく検索しましたが、同様の質問をしている人は、データベースを操作している、または他の特別な状況が必要です。もし私がちょうど誤解しているなら、私は繰り返しの質問をお詫び申し上げます。編集したバージョンが必要なときに古いデータを返すDataGrid.ItemsSource

List(Entry)> - ()と同じ値に設定されたDataGrid.ItemsSourceがあります。このエントリは、3つの文字列の構造体です。

すべてが完全に機能し、DataGridがすべてを正しく表示します。しかし、私はユーザーが各セルのデータを編集して「保存」ボタンを押してそのデータをリスト<(エントリ)に保存できるようにしたいと考えました。残念ながら、古いItemsSourceはまだそこにあるようです。

だから...新しいデータにアクセスするにはどうしたらいいですか?ここに私のコードです。 EntryGridは私のDataGridです。

public struct Entry 
{ 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string Description { get; set; } 
} 

private void createTable() 
{ 
    List<Entry> data = new List<Entry>(); 
    data.Add(new Entry() { Description = "StackOverflow", Username = "TieRein", Password = "LetMeIn" }); 
    data.Add(new Entry() { Description = "AnotherStackOverflow", Username = "NewTieRein", Password = "DontLetMeIn" }); 
    EntryGrid.ItemsSource = data; 
} 

private void SaveFile(bool confirmation) 
{ 
    EntryGrid.Items.Refresh(); 
    List<Entry> Save = new List<Entry>(); 
    Save = (List<Entry>)EntryGrid.ItemsSource; 
    string filename = "../../../" + m_profile; 
    using (var file = File.OpenWrite(filename)) 
    { 
     var writer = new BinaryFormatter(); 
     writer.Serialize(file, Save);   
    } 

    if (confirmation) 
     MessageBox.Show("Account Saved"); 
} 

答えて

2

データの構造体のリストを使用しており、値渡しされています。バインディングがデータを更新するとき、元の構造体は変更されませんが、バインディング実装のどこかの構造体のコピーが変更されます。

は、あなたのデータを保持するクラスを使用します。

public class Entry 
{ 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string Description { get; set; } 
} 
+1

私はクラスがすべての違いになるだろう知りませんでした。これはまさに私が必要としたものであり、簡単な修正です!私はC#言語の私の理解を再評価する必要があるように見えます。ありがとうございました! –

関連する問題