2017-02-19 63 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