我在网格内有一个画布。我想动态添加控件(文本块和图像)。我使用以下代码,但它没有显示任何内容。
XAML:
<Grid x:Name="BaseGrid"
Background="White"
Grid.Row="2">
<Canvas x:Name="RootCanvas"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Visibility="Collapsed"
Canvas.ZIndex="0">
<Canvas x:Name="BaseCanvas"
CacheMode="BitmapCache"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Canvas.Left="0"
Canvas.Top="0"
Canvas.ZIndex="0">
<Canvas.Clip>
<RectangleGeometry x:Name="BaseCanvasClip"/>
</Canvas.Clip>
<Canvas x:Name="DrawingCanvas"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Canvas.Left="0"
Canvas.Top="0"
Canvas.ZIndex="10">
</Canvas>
<Canvas x:Name="TransparentCanvas"
Background="Transparent"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Canvas.Left="0"
Canvas.Top="0"
Canvas.ZIndex="100"
Visibility="Collapsed">
</Canvas>
</Canvas>
</Canvas>
</Grid>
</Grid>
代码:
TextBlock txtBlk = new TextBlock();
txtBlk.FontSize = 14;
txtBlk.Foreground = new SolidColorBrush(Colors.Red);
txtBlk.Text = CreateConceptTextBox.Text;
Canvas.SetTop(txtBlk, 100);
Canvas.SetLeft(txtBlk, 450);
//Grid.SetRow(txtBlk, 100);
//Grid.SetColumn(txtBlk, 450);
linkDiscovery.DrawingCanvas.Children.Add(txtBlk);
我关注&#34; DrawingCanvas&#34;。我在这里失踪的是什么?
答案 0 :(得分:1)
您将RootCanvas的可见性设置为Collapsed,它可以有效地隐藏它及其所有子项,包括DrawingCanvas。