2013-05-28 142 views
0

我知道這看起來像一個簡單的問題,但我無法爲我的生活找到如何使網格有能力看穿它。我已經嘗試使背景透明,但是這不起作用。我在XAML中有一個文本框和一張圖片(我的程序中的所有其他部分,包括網格,都是以編程方式編寫的),但我不再看到文本框或圖像,我只看到網格。任何幫助?在網格中顯示XAML

XAML:

<Window x:Name="MainWin" x:Class="WhackaMoleReal.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="421.378" Width="624.929"> 
    <Grid> 
     <Image Margin="353,156,-131,-58" Source="mole2.png" Stretch="Fill"/> 
     <TextBlock HorizontalAlignment="Left" Margin="20,10,0,0" TextWrapping="Wrap" Text="Can You Catch the Mole?" VerticalAlignment="Top" Height="79" Width="497" FontFamily="SimHei" FontSize="40"/> 
    </Grid> 
</Window> 

C#:

 Grid grid_Main = new Grid(); 

// Create Grid \\ 
      MainWin.Content = grid_Main; 
      grid_Main.Height = MainWindowHeight; 
      grid_Main.Width = MainWindowWidth; 
      grid_Main.Background = Brushes.Transparent; 


       // Fill Grid \\ 
       int RowCount = 0; 
       int ColumnCount = 0; 
       for (int i = 0; i <= NumofImages; i++) 
       { 
        Image newImage = HoleImage(); 
        if (RowCount < NumberofRows) 
        { 
         if (ColumnCount < NumberOfColumns) 
         { 
          Console.WriteLine("ColumnCount: " + ColumnCount.ToString()); 
          Grid.SetRow(newImage, RowCount); 
          Grid.SetColumn(newImage, ColumnCount); 
          grid_Main.Children.Add(newImage); 
          ColumnCount++; 
         } 

         else 
         { 
          RowCount++; 
          ColumnCount = 0; 
          Grid.SetRow(newImage, RowCount); 
          Grid.SetColumn(newImage, ColumnCount); 
          grid_Main.Children.Add(newImage); 
          ColumnCount++; 
          Console.WriteLine("RowCount: " + RowCount.ToString()); 
         } 
        } 

        else 
        { 
         break; 
        } 
+0

你能顯示將'grid_Main'添加到Windows Grid的代碼 –

+0

@ sa_ddam213我剛添加它,它在「// Cre吃網格「 – Andrew

+1

然後,你的問題是你正在用你的新網格替換當前的Windows內容(網格,圖像,文本框)。 –

回答

1

問題是要更換的當前窗口的內容(網格,圖像,文本框)與新網格(grid_Main)。

看起來你只是想在新的網格(grid_Main)添加到現有的窗口電網

的XAML:

<Window x:Name="MainWin" x:Class="WhackaMoleReal.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="421.378" Width="624.929"> 
    <Grid x:Name="Content_Grid"> 
     <Image Margin="353,156,-131,-58" Source="mole2.png" Stretch="Fill"/> 
     <TextBlock HorizontalAlignment="Left" Margin="20,10,0,0" TextWrapping="Wrap" Text="Can You Catch the Mole?" VerticalAlignment="Top" Height="79" Width="497" FontFamily="SimHei" FontSize="40"/> 
    </Grid> 
</Window> 

C#:

Grid grid_Main = new Grid(); 

// Create Grid \\ 
Content_Grid.Children.Add(grid_Main); 
+0

謝謝!你知道我如何保持XAML中的圖像不被拉伸? – Andrew

+0

也許'Stretch =「Uniform」',不能看到你在看什麼 –

+0

是的,謝謝,我只是要刪除圖像,它看起來很好。謝謝您的幫助 :) – Andrew