我想將List<List<Button>> lloMatrix
的當前狀態保存在其他變量List<List<Button>> lloMatrixCopy
中,創建一個Frame(我寫的一個類)並將它添加到列表loFrames
。作爲幀的屬性,lloMatrixCopy
不得在之後改變。我嘗試了不同的方式,但是我的最終列表每次只列出相同的lloMatrixCopy
,全部相同於最新版本lloMatrix
。 所以我的問題是如何做lloMatrix的當前狀態的副本,而不會在lloMatrix發生變化後立即被覆蓋。WPF:保存變量變量的當前值
List<List<Button>> lloMatrixCopy = new List<List<Button>>;
List<List<Button>> lloMatrix = new List<List<Button>>;
List<Frame> loFrames = new List<Frame>;
//...
//lloMatrix gets filled with objects
//...
private void Btn_Click(object sender, RoutedEventArgs e)
{
lloMatrixCopy = lloMatrix;
var oNewFrame = new Frame(lloMatrixCopy);
loFrames.Add(oNewFrame);
}
lloMatrix後來被改變了,但loFrames只會在那個按鈕被按下的時候列出它的狀態。我想這是一個簡單的問題,但我嘗試了很多東西,但它不起作用。也對不完美的英語感到抱歉。我希望這是可以理解的。
編輯:謝謝你的快速響應,但由於某些原因
_lloMatrixCopy = _lloMatrixLeds.Select(original => original.ToList()).ToList();
也沒有解決問題。在這裏,全Btn_Click() - 方法
private void Btn_Click(object sender, RoutedEventArgs e)
{
lloMatrixCopy = lloMatrix.Select(original => original.ToList()).ToList();
var oNewFrame = new Frame(lloMatrixCopy);
loFrames.Add(oNewFrame);
//After adding the copy to the list i want to put lloMatrix back in Default
//mode - which means in my case Change the Background Color of every Button to a specific Default
//Color. but the foreach-loop doenst only Change the lloMatrix, but also the
//copy, so that every Matrix saved in loFrames is a Default Matrix
// Globals.LClickedButtons is a list of every Button in lloMatrix which Background-Color
// isn't equal to the Default-Color
foreach (var btn in Globals.LClickedButtons)
{
btn.Background = loLedColorBrushes[0];
}
}
每個Matrix在loFrames仍然是儘快的foreach循環默認矩陣完成。
你更新'lloMatrix'與狀態值? –
對不起,我不太清楚你的意思,我在做完副本後將'lloMatrix'更新並添加到列表中。請查看已編輯的問題 – Kunibert
僅當您創建另一個列表