2017-01-23 125 views
1

我搜索了整個計算器並找不到簡單的解決方案來解決我的問題。WPF文本覆蓋網格

我有網格,我想通過一些文字/圖像覆蓋整個電網。你有什麼想法,我該怎麼做?

其實這是俄羅斯方塊遊戲,我想顯示用戶文本/圖片「遊戲結束」,他失去之後,所以我需要從C#做手工。有任何想法嗎 ?

感謝您的幫助:-)

<Window x:Class="TetrisWPF.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:TetrisWPF" 
    mc:Ignorable="d" 
    Title="MainWindow" 
    AllowsTransparency="True" 
    ResizeMode="CanResizeWithGrip" 
    WindowStyle="None" 
    ShowInTaskbar="True" 
    WindowState="Maximized" 
    KeyDown="HandleKeyDown" 
    Initialized="MainWindow_Initilized" Background="#222222"> 
<Window.Resources> 

    <FontFamily x:Key="FontAwesome">/Fonts/fontawesome-webfont.ttf#FontAwesome</FontFamily> 
</Window.Resources> 
<DockPanel LastChildFill="false"> 
    <Button DockPanel.Dock="Right" Visibility="Hidden" Width="300">Right</Button> 
    <StackPanel DockPanel.Dock="Right" Width="311" > 
     <Button x:Name="btnPlay" Content="Play" Click="btnPlay_Click" Width="50" Height="25" Margin="5"/> 
     <Label Content="Score " Height="56" x:Name="Score" HorizontalAlignment="Center" FontSize="28" FontWeight="Bold" Margin="0,0,0,0"/> 
     <Label Content="Lines " Height="56" x:Name="Lines" HorizontalAlignment="Center" FontSize="28" FontWeight="Bold" Margin="0,0,0,0"/> 
     <Label Content="Level 1" Height="56" x:Name="level" HorizontalAlignment="Center" FontSize="28" FontWeight="Bold" Margin="0,0,0,0" /> 
     <Button x:Name="buttonPlay" HorizontalAlignment="Right" VerticalAlignment="Top" Height="60" Margin="0,20,0,0" Width="205" Click="buttonPlay_Click" > 
      <Button.Template> 
       <ControlTemplate TargetType="Button"> 
        <Image Name="img1" Source="C:\Users\xx\Pictures\btn.png" /> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 
     <Button x:Name="buttonPause" Content="Pause (L1)" HorizontalAlignment="Right" VerticalAlignment="Top" Height="60" Margin="0,20,0,0" Width="205" Click="buttonPause_Click" /> 
     <Button x:Name="buttonRestart" Content="Restart" HorizontalAlignment="Right" VerticalAlignment="Top" Height="60" Margin="0,20,0,0" Width="205" Click="buttonRestart_Click" /> 
     <Button x:Name="buttonStop" Content="Stop" HorizontalAlignment="Right" VerticalAlignment="Top" Height="60" Margin="0,20,0,0" Width="205" Click="buttonStop_Click" /> 
     <Button x:Name="buttonDemo" Content="Demo" HorizontalAlignment="Right" VerticalAlignment="Top" Height="60" Margin="0,20,0,0" Width="205" Click="buttonDemo_Click" /> 
     <Button x:Name="buttonExit" Content="Exit" HorizontalAlignment="Right" VerticalAlignment="Top" Height="60" Margin="0,20,0,0" Width="205" Click="buttonExit_Click" /> 
     <TextBlock x:Name="GameOverText" Height="56" FontSize="28" FontWeight="Bold" TextWrapping="Wrap" Text="Game Over" Foreground="#FFD41A1A"/> 
     <TextBlock x:Name="GamePausedText" Height="56" FontSize="28" FontWeight="Bold" TextWrapping="Wrap" Text="Game Paused" Foreground="#FF0D15B6" Margin="0,0,-0.8,0"/> 
    </StackPanel> 
    <Grid Name="MainGrid" Height="750" Width="375" DockPanel.Dock="Right"> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
    </Grid> 
</DockPanel> 

回答

3

具一格,你可以簡單地添加佔用相同的空間物體,他們將在您添加的順序重疊。在你的例子中,你有很多的列和行,所以爲了覆蓋所有的東西,你必須將它設置爲RowSpan和ColumnSpan爲你填充所有空間的行數/列數。你可以將一個更簡單的方法放在另一個網格中(只有一行和一列),並添加一些內容(這是我想要疊加組件時所做的事情,只需將它們粘貼在它們自己的小部件中1x1格)。

像這樣:

<Grid> 
    <Grid Name="MainGrid" Height="750" Width="375" DockPanel.Dock="Right"> 
     ... all those columns 
    </Grid> 
    <Border Name="GameOverlay" Background="Black" Visibility="Hidden"> 
     <TextBlock Text="Game Over!" Foreground="White" FontWeight="Bold" 
        FontSize="24" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center"/> 
    </Border> 
</Grid> 

注意,一定要設置背景的東西,如果你想掩蓋你的原創內容是很重要的(雖然是半透明背景可能看起來很酷!)。

要顯示和隱藏它的代碼,只需顯示&用GameOverlay.Visibility = Visibility.VisibleGameOverlay.Visibility = Visibility.Hidden隱藏它,或者將其綁定到可以更改的屬性。

通過這種方式,您可以將其放置在您想要的位置,並將其設置爲設計器中的Visible,然後將其更改爲隱藏(以便您可以使其在代碼中可見)。

不是構造它的代碼,當你需要它,並手動將其添加到UX容易得多。

+0

謝謝!你節省了我的一天,真的:-)我正在嘗試類似的東西,但我不知道爲什麼它不起作用。你的營養很棒! – Icet

+0

沒問題。你是否試圖將它添加到遊戲網格中,然後在代碼中添加遊戲元素?這會導致訂單全部搞亂(它們按您添加它們的順序繪製)。您可以使用Canvas.ZIndex強制繪製順序。更容易在它自己的網格中將它推向更高的樹!另外,如果隱藏起來並且在需要的時候顯示它們,如果它變化的很快,通常會更高效。 – Joe