0

我正在开发一个 Windows Phone 项目,这需要我在画布内动态放置一个网格以及 2 个显示相关数据的文本块。

我的代码看起来像这样。

            Canvas canvas = new Canvas();
            canvas.Height = height;
            canvas.Width = width;

            Grid grid = new Grid();
            grid.Height = height;
            grid.Width = width;
            grid.Background = new SolidColorBrush(Colors.Green);
            grid.ShowGridLines = true;
            canvas.Children.Add(grid);

            ColumnDefinition c1 = new ColumnDefinition();
            c1.Width = GridLength.Auto;
            ColumnDefinition c2 = new ColumnDefinition();
            c2.Width = GridLength.Auto;

            grid.ColumnDefinitions.Add(c1);
            grid.ColumnDefinitions.Add(c2);

            TextBlock wel_txt = new TextBlock();
            wel_txt.Text = wel.ToString();
            wel_txt.TextAlignment = TextAlignment.Center;
            wel_txt.Foreground = new SolidColorBrush(Colors.White);
            wel_txt.FontSource = slkscr;
            wel_txt.FontFamily = new FontFamily(FontName);
            wel_txt.FontSize = FontSize;

            TextBlock tel_txt = new TextBlock();
            tel_txt.Text = tel.ToString();
            tel_txt.TextAlignment = TextAlignment.Center;
            tel_txt.Foreground = new SolidColorBrush(Colors.White);
            tel_txt.FontSource = slkscr;
            tel_txt.FontFamily = new FontFamily(FontName);
            tel_txt.FontSize = FontSize;

            grid.Children.Add(wel_txt);
            grid.Children.Add(tel_txt);
            Grid.SetColumn(tel_txt, 0);
            Grid.SetColumn(wel_txt, 1);

            canvas.Children.Add(grid);

理想情况下,代码应该在画布中创建 2 列,并将每个文本块放在一个列中。完美对齐。但我得到的是两个文本块在画布的左上角相互重叠。

我错过了什么?

4

0 回答 0