0

Im playing with WPF in Visual Studio, and I have this weird problem. I have made a grid which takes about 50% of the main window. This grid will be a place where my Tetris game takes place. On the other half of window Id like to display labels showing score and so on. But nothing shows up, just the grid content. Does anyone have any idea what might cause this issue ? Heres my xaml code:

<Window x:Class="Tetris_Final.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Tetris_Final"
    mc:Ignorable="d"
    Title="MainWindow" Height="500" Width="500" KeyDown="Window_KeyDown">
<Grid x:Name="GridPlayBoard" Width="255" Height="405
      " HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,5,0,0">
    <Button x:Name="button" Content="Start game!" HorizontalAlignment="Left" Margin="337,148,-177,0" VerticalAlignment="Top" Width="95" Height="48"/>
    <Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="337,48,-214,0" VerticalAlignment="Top" Width="132" Height="42"/>
</Grid>

1 Answer 1

1

Your button and your label are inside your grid. You should make an outer grid to hold all of your elements and put you game board grid inside it. Then use some other kind of grid or panel to control the layout of your buttons and labels.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid x:Name="GridPlayBoard" Grid.Column="0"
          Width="255" Height="405" 
          HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,5,0,0">
        <!--put your game here-->
    </Grid>
    <StackPanel Orientation="Vertical" Grid.Column="1">
        <Button x:Name="button" Content="Start game!" 
                HorizontalAlignment="Left" VerticalAlignment="Top" Width="95" Height="48"/>
        <Label x:Name="label" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Width="132" Height="42"/>
    </StackPanel>
</Grid>

Update

As an aside, you probably shouldn't specify your style properties inline because it'll lead to a lot of repetition. It would be better to specify them once for the whole window.

<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Width" Value="95"/>
        <Setter Property="Height" Value="48"/>
    </Style>
</Window.Resources>   

Better yet, if the same style will be used on multiple windows, use a resource file.

https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/resourcedictionary-and-xaml-resource-references

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.