Search code examples
wpfxaml

How to add a border to cells in UniformGrid


I am creating a program to simulate turn-based battles like in DND and similar board and computer games. This is my first time working with XAML, I studied a lot of information and came to the conclusion that UniformGrid is best suited for the playing field, since the size can be edited by the user. I can't figure out how to add borders to cells

I searched the entire Internet and can't find any information. Maybe I chose the wrong type of grid, but then I don't understand at all what is better for my purpose


Solution

  • Here is an sample, You need play around a little of what type of control you want to use in uniform grids accordingly.

    In this sample I am using rectangle and label inside uniform grid. In MainWindow.xml, Paste below code -

     <Grid HorizontalAlignment="Stretch" Height="400"  Width="400" VerticalAlignment="Stretch">
        <UniformGrid x:Name="BoardGame" Margin="5">
            <Rectangle StrokeThickness="6" MouseDown="L1_MouseDown" Stroke="DarkBlue" Name="l1" Fill="Green"></Rectangle>
            <Rectangle  StrokeThickness="6" Stroke="DarkBlue" Name="l2" Fill="Red"></Rectangle>
            <Label BorderThickness="6" BorderBrush="Black" Background="Teal"  Name="l3" ></Label>
            <Label BorderThickness="6" BorderBrush="Black"  Background="Yellow" Name="l4"></Label>
        </UniformGrid>
    </Grid>
    

    In MainWindow.cs, Paste below code -

     private void L1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            l3.Background = System.Windows.Media.Brushes.Purple;
            l4.Background = System.Windows.Media.Brushes.DodgerBlue;
        }
    

    SampleImage