Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF窗口不显示元素_C#_Wpf_Xaml - Fatal编程技术网

C#WPF窗口不显示元素

C#WPF窗口不显示元素,c#,wpf,xaml,C#,Wpf,Xaml,我在VisualStudio中使用WPF,我有一个奇怪的问题。我做了一个网格,占据了主窗口的50%。这个格子将是我玩俄罗斯方块游戏的地方。在窗口的另一半,我想显示显示分数等标签。但什么都没有显示,只有网格内容。有人知道是什么导致了这个问题吗? 这是我的xaml代码: <Window x:Class="Tetris_Final.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我在VisualStudio中使用WPF,我有一个奇怪的问题。我做了一个网格,占据了主窗口的50%。这个格子将是我玩俄罗斯方块游戏的地方。在窗口的另一半,我想显示显示分数等标签。但什么都没有显示,只有网格内容。有人知道是什么导致了这个问题吗? 这是我的xaml代码:

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


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

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

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