如何防止 WPF UserControl 元素在我所需的视图范围之外可见?

发布于 2024-09-10 09:54:07 字数 699 浏览 8 评论 0原文

我正在尝试将 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 技术交流群。

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

发布评论

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

评论(2

烟酉 2024-09-17 09:54:07

您是否尝试过将 UserControl 中元素的 ClipToBounds 属性设置为 True

Have you tried setting the ClipToBounds property on your elements within your UserControl to True?

素染倾城色 2024-09-17 09:54:07

在用户控件内,将容器的 Clip 属性(例如 Grid)设置为用户控件的大小(宽度、高度)。

例如,

<Window x:Class="TestClipping.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="Auto" Width="Auto">
    <Grid SizeChanged="OnGridSizeChanged"
          x:Name="myGrid">

    </Grid>
</Window>

事件处理程序:

private void OnGridSizeChanged(object sender, SizeChangedEventArgs e)
{
    // Set the clipping region to match the current display region of the grid.
    var visibleArea = new RectangleGeometry();
    visibleArea.Rect = new Rect(0, 0,
    myGrid.ActualWidth, myGrid.ActualHeight);
    myGrid.Clip = visibleArea;
}

Inside your user control, set the Clip property of the container , for example Grid to the size(width, height) of user control .

For example,

<Window x:Class="TestClipping.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="Auto" Width="Auto">
    <Grid SizeChanged="OnGridSizeChanged"
          x:Name="myGrid">

    </Grid>
</Window>

and the event handler:

private void OnGridSizeChanged(object sender, SizeChangedEventArgs e)
{
    // Set the clipping region to match the current display region of the grid.
    var visibleArea = new RectangleGeometry();
    visibleArea.Rect = new Rect(0, 0,
    myGrid.ActualWidth, myGrid.ActualHeight);
    myGrid.Clip = visibleArea;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文