/// <summary> /// Toggle the buttons to the top, left, right and bottom of the pressed button /// </summary> /// <param name="toggledButton">Which button was pressed</param> private void ToggleNearbyButtons(CustomButton toggledButton) { // Grab a list of buttons assigned to mainGrid. Make sure they're a 'CustomButton' object, just in case we add elements later! IEnumerable <CustomButton> buttonsList = from child in mainGrid.Children where child is CustomButton select child as CustomButton; // See if we're not the top row, there's nothing above! if (toggledButton.Row > 0) { int aboveRow = toggledButton.Row - 1; // Find the button above var upperButtonQuery = from btn in buttonsList where btn.Row == aboveRow && btn.Column == toggledButton.Column select btn; CustomButton upperButton = upperButtonQuery.FirstOrDefault(); if (upperButton != null) { upperButton.IsLit = !upperButton.IsLit; } } // Check if we're not the left-most button if (toggledButton.Column > 0) { int leftColumn = toggledButton.Column - 1; // Find the button to the left var leftButtonQuery = from btn in buttonsList where btn.Row == toggledButton.Row && btn.Column == leftColumn select btn; CustomButton leftButton = leftButtonQuery.FirstOrDefault(); if (leftButton != null) { leftButton.IsLit = !leftButton.IsLit; } } // See if we're not on the bottom row if (toggledButton.Row < GridRows) { int lowerRow = toggledButton.Row + 1; // Find the button below var lowerButtonQuery = from btn in buttonsList where btn.Row == lowerRow && btn.Column == toggledButton.Column select btn; CustomButton lowerButton = lowerButtonQuery.FirstOrDefault(); if (lowerButton != null) { lowerButton.IsLit = !lowerButton.IsLit; } } // Make sure we're not the right-most button if (toggledButton.Column < GridColumns) { int rightColumn = toggledButton.Column + 1; // Find the button to the right var rightButtonQuery = from btn in buttonsList where btn.Row == toggledButton.Row && btn.Column == rightColumn select btn; CustomButton rightButton = rightButtonQuery.FirstOrDefault(); if (rightButton != null) { rightButton.IsLit = !rightButton.IsLit; } } }