为什么我的Canvas中的TextBlocks不显示?

时间:2019-01-11 22:31:54

标签: c# wpf canvas

我打算在大图像上叠加4至6层之间的多个Canvas,以便可以通过简单的show或Hide例程将给定Canvas中的所有对象设置为可见或不可见。图层类。 UpdateLayers只需对每个layer.Update()进行一组调用。就沉降名称层而言,更新代码似乎没有完成其工作。应该是这样的工作;

private void ShowCities_Click(object sender, RoutedEventArgs e)
{
    UpdateLayers();
    settlements.Show(Settlements);
    settlementNames.Show(SettlementNames);
}

public void Show(Canvas canvas)
{
    canvas.Visibility = Visibility.Visible;
}

这与第一个在ZIndex 1处包含图标大小的BitmapImages的画布完美配合(大Image本质上是ZIndex 0的背景)。当我尝试在ZIndex 2处添加其他画布时,代码按预期方式执行但不显示内容。这次的内容是一组TextBlocks。

已检查代码中的AssociatedCanvas属性,它是正确的Canvas实例,该实例已放置在XAML主窗口中。

public void Update(string layerSelectSqlQuery, LayerType layerType)
{
    DataTable layerDataTable = null;
    int x = -1;
    int y = -1;
    string label;

    using (MySqlClientWrapper db = new MySqlClientWrapper("Server = localhost; Database = tribes;Uid = root;Pwd = xxxxxxxxx;"))
    {
        // TODO add population column - and filter to those settlements considered cities.
        layerDataTable = db.GetDataTable(layerSelectSqlQuery);
    }

    AssociatedCanvas.Children.Clear();

    foreach (DataRow dataRow in layerDataTable.Rows)
    {
        x = (int)dataRow["MapX"];
        y = (int)dataRow["MapY"];
        label = dataRow["Name"].ToString();
        if (x != -1 && y != -1)
        {
            switch (layerType)
            {
                case LayerType.Settlements:
                    DrawBitmapImage(x, y);
                    break;

                case LayerType.SettlementNames:
                    WriteLabel(x, y, label, Color.FromRgb(0, 0, 0));
                    break;

                case LayerType.Units:
                    break;

                case LayerType.UnitNames:
                    break;

                default:
                    break;
            }
        }
    }
}    


Public void WriteLabel(int x, int y, string text, Color color)
{
    TextBlock textBlock = new TextBlock();
    textBlock.Text = text;
    textBlock.Foreground = new SolidColorBrush(color);
    Canvas.SetLeft(textBlock, x);
    Canvas.SetTop(textBlock, y);
    AssociatedCanvas.Children.Add(textBlock);
}

XAML部分如下:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <!--<Slider Grid.Column="0" Orientation="Vertical" HorizontalAlignment="Left" Minimum="1" x:Name="slider" />-->
    <ScrollViewer Name="mapScroller" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Grid Name="grid" RenderTransformOrigin="0.5,0.5">
            <Grid.LayoutTransform>
                <TransformGroup>
                    <ScaleTransform x:Name="scaleTransform" />
                </TransformGroup>
            </Grid.LayoutTransform>
            <Viewbox Grid.Column="0" Grid.Row="0" >
                <Image x:Name="MainMap" UseLayoutRounding="True" Stretch="Fill" HorizontalAlignment="Center" VerticalAlignment="Center" 
                       MouseLeftButtonUp="MainMap_MouseLeftButtonUp" Source="{Binding MainTerrainMap}"></Image>
            </Viewbox>
            <Canvas x:Name="Settlements" Panel.ZIndex="1" />
            <Canvas x:Name="SettlementNames" Panel.ZIndex="2" >
            </Canvas>
        </Grid>
    </ScrollViewer>
</Grid>

0 个答案:

没有答案