WPF 平铺背景未对齐
我们有一个相当复杂的 WPF 应用程序(我无法在此处显示),该应用程序的平铺背景与其中一个用户控件未对齐。我无法在“干净”的 WPF 项目中重现此问题,但会尝试在这张图片中说明该问题:
(来源:kintespace.com)
灰色区域代表用户控件中的黑色和红色格子表示用户控件内的平铺背景。白色背景代表托管该控件的窗口。这里使用了VisualBrush
,但是DrawingBrush
也出现了同样的问题。我宁愿不使用ImageBrush
。
你能打开一个“干净的”WPF 项目并构建一些会导致平铺背景像这样不对齐的东西吗?
We have quite a complex WPF application (that I cannot show here) that somehow has the tiled background misaligned of one of its user controls. I was unable to reproduce this problem in a "clean" WPF project but will try to illustrate the problem in this picture:
(source: kintespace.com)
The gray area represents the user control and the black and red checks represents the tiled background inside the user control. The white background represents the window hosting the control. The VisualBrush
is used here but the same problem comes with the DrawingBrush
. I would prefer not to use the ImageBrush
.
Can you open a "clean" WPF project and build something that will cause a tiled background to be misaligned like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果用户控件的布局根具有 -10 左边距和 -10 上边距,则可以执行此操作。平铺背景位于用户控件的布局根上,因此看起来像是对齐问题而不是画笔问题。
You can do this if the layout root of the user control has a -10 left margin and a -10 top margin. the tiled backgoround in on the layout root of the usercontrol so, it looks like a alignment problem not a brush problem.
此问题与平铺背景将其自身塞入托管窗口中
UserControl
实例的计算高度有关。我仍然无法在新项目中复制这个问题,但它已“解决”(通过水平而不是垂直地在用户控件中堆叠面板)。This problem is related to the tiled background cramming itself into the calculated height of the
UserControl
instance in the hosting Window. I am still unable to replicate this problem in a new project but it is "solved" (by stacking panels in the user control horizontally instead of vertically).以下是两种在您遇到的情况下非常有用的诊断技术:
获取您的复杂项目,制作一个副本,然后开始删除大部分代码和 XAML,直到问题消失。然后将最后一部分放回去,并更轻柔地撕下东西。重复此操作,直到找到造成差异的更改。
运行应用程序,在局部变量引用您的 UserControl 的位置处中断,然后在“局部变量”或“监视”窗口中探索 UserControl 上方和下方的可视化树。查看每个 Visual 的内部属性 VisualOffset、VisualTransform 和 VisualContentBounds。这些属性通常会提示您哪些属性设置不正确,然后您可以找出原因。
Here are two diagnostic techniques that can be useful in situations like yours:
Take your complex project, make a copy, and start tearing out large sections of code and XAML until the problem disappears. Then put the last section back and tear things out more gently. Repeat until you find the change that makes the difference.
Run the application, break where a local variable references your UserControl, then explore the visual tree just above and below your UserControl in the Locals or Watch window. Look at each Visual's internal properties VisualOffset, VisualTransform, and VisualContentBounds. These properties will usually clue you in to what property is being set incorrectly, and from there you can figure out why.