I want to save the current state of List<List<Button>> lloMatrix
in an other variable List<List<Button>> lloMatrixCopy
, create a Frame (a class i wrote) with it and add it to a list loFrames
. lloMatrixCopy
, as a property of the Frame, shall not change afterwards. I tried different ways, but my final List only lists equal lloMatrixCopy
everytime, all identical to the latest Version of lloMatrix
.
So my question is how to make a copy of the current state of lloMatrix without it getting overwritten afterwards as soon as lloMatrix changes.
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 is getting changed afterwards, but loFrames shall only list it state at that Moment the button got pressed. I guess it's an easy question, but i tried many Things and it just doesn't work. Also sorry for not perfect english. I hope it's understandable.
EDIT: Thank you for fast Responses, but for some reasons
_lloMatrixCopy = _lloMatrixLeds.Select(original => original.ToList()).ToList();
also doesn't solve the Problem. Here the full Btn_Click()-Method
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];
}
}
Every Matrix in loFrames still is a default Matrix as soon as the foreach-loop is done.