如何在Silverlight中创建动态画布的边框?

时间:2010-03-27 10:03:17

标签: c# silverlight silverlight-3.0 canvas

您好我在后面的代码中创建一个Canvas,如下所示:

Canvas musicPlayerCanvas = new Canvas();
                    musicPlayerCanvas.Background = new SolidColorBrush(Colors.White);
                    musicPlayerCanvas.Height = 80;
                    musicPlayerCanvas.Width = 1018;
LayoutRoot.Children.Add(musicPlayerCanvas);

musicPlayerCanvas.Children.Add(playingText);
musicPlayerCanvas.Children.Add(albumImage);

现在我如何从代码隐藏中向画布添加边框。

我尝试创建一个边框并指定一个像下面这样的孩子:

Border myBorder = new Border();
                    //Border Proporties



                    Canvas.SetTop(musicPlayerCanvas, 26);
                    Canvas.SetLeft(musicPlayerCanvas, 154);
                    LayoutRoot.Children.Add(musicPlayerCanvas);
                    myBorder.Child = musicPlayerCanvas;

这对我不起作用。请帮忙。

谢谢, Subhen

1 个答案:

答案 0 :(得分:4)

您想要将画布添加到边框,如下所示:

Canvas musicPlayerCanvas = new Canvas();
musicPlayerCanvas.Background = new SolidColorBrush(Colors.Purple);

Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(5);
border.Height = 80;
border.Width = 1018;
border.Child = musicPlayerCanvas;

LayoutRoot.Children.Add(border);

另外,在使用文本框和图像等控件时(我认为您可能正在查看控件名称),您可能希望使用Grid而不是Canvas作为容器控件。 干杯, 菲尔