Find an empty element in a Silverlight Grid
-
Well, I have been trying to figure this one out for three days straight and I still haven't come up with a fix. Basically I am trying to swap out the clicked Ellipse with the only empty spot on the 3x3 checkerboard. 8 of the 9 squares are occupied. I need to find the one spot that is not occupied and I can't seem to do it. Why? Because even though there is an empty spot on the grid at runtime, Javascript refuses to acknowledge this. I used the line: var childrenCount = canvasArray[i].children.count; .. so that's all the canvases. If at runtime there is an empty spot, then how come my code refuses to see it? Or am I not writing the right code? How is the empty spot represented and found at runtime? That's what I want to know. Here is the pseudocode: if (squareOnGrid is empty) { log.write(squareOnGrid + ' is empty'); emptySquare = squareOnGrid; oldPositionBorder = sender; oldPositionR = checkerPiece.row; oldPositionC = checkerPiece.col; checkerPiece.row = empty.row; checkerPiece.column = squareOnGrid.column; oldPositionBorder = null; } I want to do this with Javascript (not C#). I already have this (Javascript):
function switchPlaces(sender) { for (var i = 0; i < canvasArray.length; i++) { var oldLocationBorderParent = sender; var oldLocationCanvasParent = oldLocationBorderParent.findName('canvas' + (i + 1)); var oldLocationChild = oldLocationCanvasParent.findName('ellipse' + (i + 1)); var childrenCount = canvasArray[i].children.count; log.info(childrenCount); //all of this outputs '1'. It should have a '0' in there, but no. if (childrenCount == 0) { log.info(canvasArray[i] + ' has no children'); var emptySpot = canvasArray[i]; sender['Grid.Row'] = emptySpot['Grid.Row']; sender['Grid.Column'] = emptySpot['Grid.Column']; oldLocationCanvasParent.children.remove(oldLocationChild); } } }
Here is my Silverlight code:<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Loaded="onLoaded" ShowGridLines="True" Background="CornflowerBlue"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="100"/> <ColumnDefinition Width="100"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.Ro
-
Well, I have been trying to figure this one out for three days straight and I still haven't come up with a fix. Basically I am trying to swap out the clicked Ellipse with the only empty spot on the 3x3 checkerboard. 8 of the 9 squares are occupied. I need to find the one spot that is not occupied and I can't seem to do it. Why? Because even though there is an empty spot on the grid at runtime, Javascript refuses to acknowledge this. I used the line: var childrenCount = canvasArray[i].children.count; .. so that's all the canvases. If at runtime there is an empty spot, then how come my code refuses to see it? Or am I not writing the right code? How is the empty spot represented and found at runtime? That's what I want to know. Here is the pseudocode: if (squareOnGrid is empty) { log.write(squareOnGrid + ' is empty'); emptySquare = squareOnGrid; oldPositionBorder = sender; oldPositionR = checkerPiece.row; oldPositionC = checkerPiece.col; checkerPiece.row = empty.row; checkerPiece.column = squareOnGrid.column; oldPositionBorder = null; } I want to do this with Javascript (not C#). I already have this (Javascript):
function switchPlaces(sender) { for (var i = 0; i < canvasArray.length; i++) { var oldLocationBorderParent = sender; var oldLocationCanvasParent = oldLocationBorderParent.findName('canvas' + (i + 1)); var oldLocationChild = oldLocationCanvasParent.findName('ellipse' + (i + 1)); var childrenCount = canvasArray[i].children.count; log.info(childrenCount); //all of this outputs '1'. It should have a '0' in there, but no. if (childrenCount == 0) { log.info(canvasArray[i] + ' has no children'); var emptySpot = canvasArray[i]; sender['Grid.Row'] = emptySpot['Grid.Row']; sender['Grid.Column'] = emptySpot['Grid.Column']; oldLocationCanvasParent.children.remove(oldLocationChild); } } }
Here is my Silverlight code:<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Loaded="onLoaded" ShowGridLines="True" Background="CornflowerBlue"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="100"/> <ColumnDefinition Width="100"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.Ro
I must be missing something here... What is this canvas array and how does it relate to the Silverlight Grid? How does the managed Silverlight code know about the canvas array? How are you calling javascript from managed code?
Mark Salsbery Microsoft MVP - Visual C++ :java: