如何防止 WPF UserControl 元素在我所需的视图范围之外可见?
我正在尝试将 WPF 应用程序转换为 WPF UserControl。在最初的应用程序中,我故意修改了某些元素的边距,使它们的边缘不在窗口的边界内。我这样做是为了隐藏不需要编写自己的控件模板就无法摆脱的不需要的边框。这是一个简单的修复,但不幸的是,当我的应用程序制作成用户控件时,该技术不起作用。如果我将 UserControl 的宽度设置为与原始应用程序中的窗口宽度相同,则当我在测试应用程序中查看此 UserControl 时,我想要隐藏其边框的元素现在完全可见。
我不明白为什么会发生这种情况。如果我将UserControl的宽度设置为某个WIDTH,那么UserControl的宽度应该等于WIDTH,对吧?好吧,正如您在下面的图 1 中看到的那样,无论我将 WIDTH 设置为多少,UserControl 的所有元素都是完全可见的。所需的视觉效果(我在原始应用程序中使用的视觉效果)如图 2 所示,其中元素被窗口边界正确地切断。
我的问题 http://img715.imageshack.us/img715/1807/probleme.png< /a>
如何确保具有负边距的元素在 UserControl 中按照我希望的方式显示?任何有助于实现这一目标的帮助将不胜感激。
非常感谢你,
达拉尔
I am trying to convert my WPF application into a WPF UserControl. In the original application, I had intentionally modified the margins of certain elements so that their edges were not within the bounds of the window. I did this in order to hide undesirable borders that I could not get rid of without having to write my own control template. It was a simple fix, but unfortunately this technique is not working when my application is made into a UserControl. If I set the width of my UserControl to the same width as the window in my original application, when I view this UserControl within a test application, the elements whose borders I wanted to hide are now fully visible.
It doesn't make sense to me why this would happen. If I set the width of the UserControl to a certain WIDTH, then the width of the UserControl should be equal to WIDTH, right? Well, as you can see below in Image 1, all the elements of the UserControl are fully visible, no matter what I set WIDTH to be. The desired visual (the one I used to get in the original application) is shown in Image 2, where the elements are properly cut off by the boundaries of the window.
My Problem http://img715.imageshack.us/img715/1807/probleme.png
How can I ensure that elements with negative margins will display the way I want them to in a UserControl? Any help on accomplishing this would be greatly appreciated.
Thank you so much,
Dalal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将 UserControl 中元素的
ClipToBounds
属性设置为True
?Have you tried setting the
ClipToBounds
property on your elements within your UserControl toTrue
?在用户控件内,将容器的
Clip
属性(例如Grid
)设置为用户控件的大小(宽度、高度)。例如,
事件处理程序:
Inside your user control, set the
Clip
property of the container , for exampleGrid
to the size(width, height) of user control .For example,
and the event handler: