我应该使用 AdornerLayer 来避免将装饰器剪切到屏幕外吗?

发布于 2024-09-12 08:24:54 字数 1641 浏览 3 评论 0原文

我正在编写一些涉及装饰器的 WPF 代码。我正在使用 Josh Smith 的 UIElementAdorner.cs(在他的 基础设施博客)。我用一段信息文本来装饰。我需要巧妙地放置装饰器,这样它就不会从屏幕上剪下来。

确定我是否要剪辑的最佳方法是什么?

我使用以下代码来创建并放置我的装饰器。我有一种有趣的感觉,根据我是否会剪辑 AdornerLayer 不是最好的选择。

var infoBubble = new InfoBubble {InformationText = @"I like cheese."};
var adornedElementRect = new Rect(Target.DesiredSize);
var layer = AdornerLayer.GetAdornerLayer(Target);

var adorner = new UiElementAdorner<Control>(Target) { Child = infoBubble };
adorner.Measure(new Size(layer.ActualWidth, layer.ActualHeight));

var adornerRect = new Rect(adorner.DesiredSize);
var top = -1*(adornerRect.Height);
var left = adornedElementRect.Width/2;

// Using layer to judge where to place the adorner
var upperLeftPoint = Target.TranslatePoint(new Point(left, top), layer);
var lowerRightPoint = Target.TranslatePoint(new Point(left + adornerRect.Width, 
    top + adornerRect.Height), layer);

if (upperLeftPoint.Y < 0) top -= upperLeftPoint.Y; // shift down by Y
if (lowerRightPoint.X > layer.ActualWidth) 
    left  -= (lowerRightPoint.X - layer.ActualWidth); // shift left

请记住,此代码包含在 TargetedTriggerAction 设计者(也称为 Blend 用户)希望在某些 UI 元素上方显示信息简介时使用。因此,这段代码对要装饰的元素或其环境知之甚少。

I'm writing some WPF code involving Adorners. I'm using Josh Smith's UIElementAdorner.cs (found in the project on his Infragistics Blog). I'm adorning with a blurb of information text. I need to place my adorner smartly, so that it does not clip off the screen.

What's the best way to find out if I'm going to clip?

I'm using the following code to create and place my adorner. I have a funny feeling that basing whether or not I'll clip on the AdornerLayer isn't the best option.

var infoBubble = new InfoBubble {InformationText = @"I like cheese."};
var adornedElementRect = new Rect(Target.DesiredSize);
var layer = AdornerLayer.GetAdornerLayer(Target);

var adorner = new UiElementAdorner<Control>(Target) { Child = infoBubble };
adorner.Measure(new Size(layer.ActualWidth, layer.ActualHeight));

var adornerRect = new Rect(adorner.DesiredSize);
var top = -1*(adornerRect.Height);
var left = adornedElementRect.Width/2;

// Using layer to judge where to place the adorner
var upperLeftPoint = Target.TranslatePoint(new Point(left, top), layer);
var lowerRightPoint = Target.TranslatePoint(new Point(left + adornerRect.Width, 
    top + adornerRect.Height), layer);

if (upperLeftPoint.Y < 0) top -= upperLeftPoint.Y; // shift down by Y
if (lowerRightPoint.X > layer.ActualWidth) 
    left  -= (lowerRightPoint.X - layer.ActualWidth); // shift left

Keep in mind that this code is contained in a TargetedTriggerAction that designers (aka users of Blend) are expected to use when they want information blurbs above certain UI elements. Thus, this code will know very little about the element to be adorned or its environment.

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

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

发布评论

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

评论(1

墨小墨 2024-09-19 08:24:54

是的,这是我能看出的最好答案。

根据进一步的阅读和一些实验,当调用 GetAdornerLayer 时,我们收到可视化树中目标控件上方的最低层。这意味着我们可以在 AdornerDecorator 在窗口模板中定义的层。较低的 AdornerDecorator 可以有 ClipToBounds="True" (我不知道为什么,但它可以)。

知道这些信息后,我可以相对确定我正在绘制的 AdorneLayer 是我正在绘制的任何内容的最佳边界框。我可以有能力在此框之外进行绘制(例如,如果在低于窗口的 AdornerDecorator 上 ClipToBounds 为 False),但我不应该指望这种能力。

Yes, is the best answer I can discern.

According to further reading and some experimentation, when calling GetAdornerLayer we receive the lowest layer above the target control in the visual tree. This means we could get a layer below the AdornerDecorator's layer defined in a Window's template. That lower AdornerDecorator could have ClipToBounds="True" (I have no idea why, but it could).

Knowing this information, I can be relatively certain that the AdorneLayer I'm drawing into is the best bounding box for whatever I'm drawing. I could have the ability to draw outside this box (for example if ClipToBounds were False on a AdornerDecorator lower than the Window's), but I shouldn't count on that ability.

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