【问题标题】:Canvas Stacking Issue画布堆叠问题
【发布时间】:2012-07-05 22:41:47
【问题描述】:

我在画布上有一个包含一些内容的边框和一个Line。 Border + 内容是可拖动的,Line 已更新为与 Border 一起移动。我的问题是这条线在边界的顶部,所以在某些情况下它会阻止我的内容。我尝试设置 ZIndex 并更改了它们在 XAML 中的顺序,但它没有任何影响。我会冒险猜测这是因为Line 不断被渲染,因为它改变了形状,并且由于某种原因输出到顶部的屏幕。有什么办法吗?

我的一些代码

XAML

<Canvas x:Name="canvas"
            MouseDown="Canvas_MouseDown"
            MouseUp="Canvas_MouseUp"
            MouseMove="Canvas_MouseMove">
            <Border BorderBrush="Aqua" BorderThickness="3" Padding="3" Name="bdr"
Background="{StaticResource GradientBackground}" Canvas.ZIndex="99"
                MouseLeftButtonDown="MouseLeftBtnDown">
                <Border.RenderTransform>
                    <TranslateTransform />
                </Border.RenderTransform>
    <button/>
            </Border>
        </Canvas>
        <Polygon
            Canvas.ZIndex="98"
            Name="SpeechPoly"
            Stroke="Aqua" 
            StrokeThickness="2"
            Fill="{StaticResource GradientBackground}">
        </Polygon>

C#

private void Dragging()
{
...
point = bdr.TransformToAncestor(this).Transform(new Point(0, 0));
myPoints.RemoveAt(1);
myPoints.RemoveAt(1);

myPoints.Add(new Point(p.X, p.Y + 50));
myPoints.Add(new Point(p.X, p.Y + 25));
SpeechPoly.Points = myPoints;
}

【问题讨论】:

  • 你可能有兴趣阅读这篇文章:stackoverflow.com/questions/630006/…
  • 嗯尝试应用最佳答案,但他们列出的方法需要一个参数,但方法调用不提供一个:/
  • 你的多边形肯定是一个依赖对象,你能用提供的函数找到它的内容呈现器吗?
  • OOOOOOOOOOOOOOPPPPPS 给你!!!你为你的多边形放置了一个 Canvas.ZIndex 而它不在画布中!!!
  • lmao,我在一个阶段把它放在画布上,忘记删除 ZIndex 但尝试将它们全部放在同一个画布中(我以前没有尝试过)并且工作!再次感谢 Vin。

标签: c# wpf canvas


【解决方案1】:

废话解决方法是确定线条来自边界的位置(即右侧/左侧,顶部/底部)并将其移动到边界周围。

更新

if (p.X > myPoints[0].X)//right
{
    if (p.Y + bdr.ActualHeight> myPoints[0].Y)//lower
    {
        myPoints.Add(new Point(p.X, p.Y + 50));
        myPoints.Add(new Point(p.X, p.Y + 25));
    }
    else//higher
    {
        myPoints.Add(new Point(p.X, p.Y + bdr.ActualHeight- 50));
        myPoints.Add(new Point(p.X, p.Y + bdr.ActualHeight - 25));
    }
}
else if (p.X + bdr.ActualWidth > myPoints[0].X)//Middle
{
    if (p.Y + bdr.ActualHeight > myPoints[0].Y)//lower
    {
        myPoints.Add(new Point(p.X + (bdr.ActualWidth / 2) + 25, p.Y));
        myPoints.Add(new Point(p.X + (bdr.ActualWidth / 2), p.Y));
    }
    else if (p.Y + bdr.ActualHeight < myPoints[0].Y)//higher
    {
        myPoints.Add(new Point(p.X + (bdr.ActualWidth / 2) + 25, p.Y + bdr.ActualHeight));
        myPoints.Add(new Point(p.X + (bdr.ActualWidth / 2), p.Y + bdr.ActualHeight));
    }
}
else//left
{
    if (p.Y > myPoints[0].Y)//lower
    {
        myPoints.Add(new Point(p.X + bdr.ActualWidth, p.Y + 50));
        myPoints.Add(new Point(p.X + bdr.ActualWidth, p.Y + 25));
    }
    else//higher
    {
        myPoints.Add(new Point(p.X + bdr.ActualWidth, p.Y + 50));
        myPoints.Add(new Point(p.X + bdr.ActualWidth, p.Y + 25));
    }
}

解决方案

必须让我的Line 与边框位于同一画布内。现在看起来很明显......谢谢文森特!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多