突出显示或勾勒装饰层中的任何 UIElement

发布于 2024-10-13 09:34:11 字数 349 浏览 6 评论 0原文

我希望能够以某种方式勾勒或突出显示装饰层中的任何特定 UIElement (甚至可能是 Visual)。装饰器本身不是问题。我更关心创建 UIElement 的轮廓。

我的目标是 OuterGlowBitmapEffect 提供类似的效果。我想遵循 UIElement 的外部轮廓。我尝试了很多方法来检查 Clip 属性(几乎总是 null)和其他一些方法,但我惨败了。

现在我想这肯定很容易,只是我错过了一些东西。此外,谷歌这次也不是我的朋友。

编辑:NET 3.5 是一个要求

I would like to be able to somehow outline or highlight any particular UIElement (or perhaps even Visual) in an adorner layer. Adorner is not a problem per se. I am more concerned about creating an outline of a UIElement.

I am aiming at a similar effect that OuterGlowBitmapEffect provides. I want to follow the outer contour of an UIElement. I have tried many approaches with examining Clip property (almost always null) and some other methods but I failed miserably.

Now I am thinking this must surely be easy it is just that I am missing something. In addition, Google was not my friend this time as well.

EDIT: NET 3.5 is a requirement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2024-10-20 09:34:11

您可以将 OpacityMaskVisualBrush 一起使用,并将其 Visual 设置为您想要轮廓的元素。在下面的示例中,前景有一个 Rectangle,背景有一个 TabControl。由于选项卡控件不是矩形,我们可以看看该技术是否有效:

<Grid Background="Gray">
    <TabControl Name="element">
        <TabItem Header="Tab1">
            <TextBlock Text="Hello, world!" FontSize="40" FontWeight="Bold"/>
        </TabItem>
    </TabControl>
    <Rectangle Fill="Yellow" Opacity="0.5">
        <Rectangle.OpacityMask>
            <VisualBrush Visual="{Binding ElementName=element}"/>
        </Rectangle.OpacityMask>
    </Rectangle>
</Grid>

结果如下所示:

Visual Outline of a Control

仅突出显示选项卡控件及其选项卡标题。

You can use an OpacityMask with a VisualBrush with its Visual set to the element you want the outline of. Here's an example where we have a Rectangle in the foreground and a TabControl in the background. Since the tab control is not rectangular, we can see if the technique works:

<Grid Background="Gray">
    <TabControl Name="element">
        <TabItem Header="Tab1">
            <TextBlock Text="Hello, world!" FontSize="40" FontWeight="Bold"/>
        </TabItem>
    </TabControl>
    <Rectangle Fill="Yellow" Opacity="0.5">
        <Rectangle.OpacityMask>
            <VisualBrush Visual="{Binding ElementName=element}"/>
        </Rectangle.OpacityMask>
    </Rectangle>
</Grid>

The result looks like this:

Visual Outline of a Control

Only the tab control and its tab header are highlighted.

想挽留 2024-10-20 09:34:11

一种方法是重写 UIElementOnRender,如 MSDN SimpleCircleAdorner 示例

  // A common way to implement an adorner's rendering behavior is to override the OnRender
  // method, which is called by the layout system as part of a rendering pass.
  protected override void OnRender(DrawingContext drawingContext)
  {
    Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize);

    // Some arbitrary drawing implements.
    SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green);
    renderBrush.Opacity = 0.2;
    Pen renderPen = new Pen(new SolidColorBrush(Colors.Navy), 1.5);
    double renderRadius = 5.0;

    // Draw a circle at each corner.
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius);
  }
}

如果您想在典型矩形或圆角矩形之外提供强大的解决方案,则必须使用 路径几何图形,它允许您构建由 BezierSegmentLineSegmentArcSegment< 等线段组成的路径/code> 从而在 UIElement 周围创建适当的路径。

另一方面,如果矩形或圆角矩形就足够了,您可以在 OnRender 中分别使用 DrawingContext.DrawRectangleDrawingContext.DrawRoundedRectangle覆盖。

One way is to override the OnRender of the UIElement as seen in the MSDN SimpleCircleAdorner example.

  // A common way to implement an adorner's rendering behavior is to override the OnRender
  // method, which is called by the layout system as part of a rendering pass.
  protected override void OnRender(DrawingContext drawingContext)
  {
    Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize);

    // Some arbitrary drawing implements.
    SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green);
    renderBrush.Opacity = 0.2;
    Pen renderPen = new Pen(new SolidColorBrush(Colors.Navy), 1.5);
    double renderRadius = 5.0;

    // Draw a circle at each corner.
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius);
  }
}

If you want to provide a robust solution outside a typical rectangle or rounded rectangle you will have to make use of path geometries which will allow you to build out a path composed of segments such as a BezierSegment, a LineSegment, or an ArcSegment thus creating an appropriate path around the UIElement.

If on the other hand a rectangle or rounded rectangle would suffice you can make use of the DrawingContext.DrawRectangle and DrawingContext.DrawRoundedRectangle respectively within the OnRender override.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文