Click here to Skip to main content
15,921,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I I am building a battleship game and I have a border in which I am placing a Grid in. I asked my instructor and he said "The border itself has the property of shrinking to the size of the object it contains. And you have no object in the border, so it has shrunk to zero". How do I solve this. This is the code of adding the border into the Grid.

public void BorderShot(Grid gridEnemy)
       {

           for (int i = 0; i < 10; i++)
           {
               for (int j = 0; j < 10; j++)
               {
                   Border borderSecond = new Border()
                   {
                       BorderBrush = Brushes.Black,
                       BorderThickness = new Thickness(5),
                       Background = Brushes.Transparent

                   };

                   Grid.SetRow(borderSecond, i);
                   Grid.SetColumn(borderSecond, j);

                   gridEnemy.Children.Add(borderSecond);
                   borderSecond.MouseEnter += borderSecond_MouseEnter;
                   borderSecond.MouseLeave += borderSecond_MouseLeave;
                   borderSecond.MouseLeftButtonUp += BorderSecond_MouseLeftButtonUp;
                   borderSecond.MouseDown += borderSecond_MouseDown;
               }
           }
       }


What I have tried:

I am pretty sure I wrote it correctly but to no avail
Posted
Updated 20-Dec-22 12:23pm
Comments
Richard Deeming 14-Dec-22 4:38am    
Your instructor is wrong. Unless you change the HorizontalAlignment and VerticalAlignment properties on the border, it will stretch to fill the available space.

How have you defined your grid rows and columns? If you've set them to "auto" height/width, then they will shrink to the size of their content. Perhaps that's what your instructor meant?
Member 15627495 14-Dec-22 4:39am    
as comment :
you can define the below part out of the loops because its a constant.
Border borderSecond = new Border()
                   {
                       BorderBrush = Brushes.Black,
                       BorderThickness = new Thickness(5),
                       Background = Brushes.Transparent

                   };

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900