相关文章推荐
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
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?
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
			
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •  
    推荐文章