在网格下显示XAML

时间:2013-05-28 01:39:17

标签: c# wpf grid

我知道这似乎是一个简单的问题,但我不能为我的生活找到如何使网格有能力透视它。我已经尝试使背景透明,但这不起作用。我在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;
                    }

1 个答案:

答案 0 :(得分:1)

问题是您正在用新网格(grid_Main)替换当前的Windows内容(Grid,Image,TextBox)。

看起来您只想将新的Grid(grid_Main)添加到现有的Window Grid

<强>的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);