2017-02-19 151 views
0

我在Visual Studio中玩WPF,我有这个奇怪的问题。我制作了一个网格,占用了主窗口的大约50%。这个网格将成为我的俄罗斯方块游戏发生的地方。窗口Id的另一半喜欢显示显示分数等的标签。但没有任何东西出现,只是网格内容。有没有人有任何想法可能会导致这个问题? 继承人我XAML代码:C#WPF窗口不显示元素

<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

你的按钮,您的标签是你的网格内。你应该制作一个外部网格来容纳你所有的元素,并把你的游戏板网格放在里面。然后使用其他类型的网格或面板来控制按钮和标签的布局。

<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> 

更新

顺便说一句,你或许不应该指定样式属性的内联,因为它会导致大量的重复。最好在整个窗口中指定一次。

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

更好的是,如果在多个窗口中使用相同的样式,请使用资源文件。

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

+0

谢谢你的工作完美! – Heisenberker