WPF 工具栏中的控件之间的动态间距

发布于 2025-01-07 02:45:47 字数 158 浏览 0 评论 0原文

我有一个工具栏,其中包含多个控件,例如 10 个。

我想将最后三个控件放置在工具栏的右上角,并在这三个控件和其余控件之间留出一些空间。

当我调整窗口大小时,它应该减少空间,一旦没有空间,就应该将其一一剪辑到溢出下拉列表中。

注意:我只需要使用一个工具栏。

I have a toolbar which contains several controls, say 10.

I want to place last three controls in the right corner of the tool bar with some space between these three controls and remaining controls.

When I resize the window, it should reduce the space, once there is no space it should be cliped to overflow drop down one by one.

Note: I need to use only one toolbar.

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

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

发布评论

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

评论(1

故人的歌 2025-01-14 02:45:47

我之前做过一次

这些项目存在于自定义的 ScrollViewer 中,因此它们可以离开屏幕,并且 ScrollViewerSizeChanged 事件会检查每个项目都可以查看它是否在屏幕外。如果它不可见或部分可见,则该项目将被隐藏,并且数据对象将被添加到菜单中。如果该项目可见,它会将其从 Menu 中删除(如果它存在),并将其设置为可见。 Menu 仅在包含项目时才可见。

这是不久前我刚接触 WPF 时完成的,所以我确信这个过程可以改进。

下面列出了我用来确定项目可见性的辅助类

public static ControlVisibility IsObjectVisibleInContainer(FrameworkElement child, UIElement parent)
{
    GeneralTransform childTransform = child.TransformToAncestor(parent);
    Rect childSize = childTransform.TransformBounds(new Rect(new Point(0, 0), new Point(child.Width, child.Height)));

    Rect result = Rect.Intersect(new Rect(new Point(0, 0), parent.RenderSize), childSize);
    if (result == Rect.Empty)
    {
        return ControlVisibility.Hidden;
    }
    else if (result.Height == childSize.Height && result.Width == childSize.Width)
    {
        return ControlVisibility.Full;
    }
    else if (result.Height == childSize.Height)
    {
        return ControlVisibility.FullHeightPartialWidth;
    }
    else if (result.Width == childSize.Width)
    {
        return ControlVisibility.FullWidthPartialHeight;
    }
    else
    {
        return ControlVisibility.Partial;
    }
}

public enum ControlVisibility
{
    Hidden,
    Partial,
    Full,
    FullHeightPartialWidth,
    FullWidthPartialHeight
}

它可以像这样使用:

ControlVisibility itemVisibility = 
    MyHelpers.IsObjectVisibleInContainer(someItem, parentContainer);

I did this once before

The items existed in a customized ScrollViewer so they could go off-screen, and the SizeChanged event of the ScrollViewer would check each item to see if it was off-screen or not. If it wasn't visibile, or was partially visible, then the item would get hidden and the data object would get added to a Menu. If the item was visible, it would remove it from the Menu if it existed there, and set it visible. The Menu was only visible if it had items.

This was done a while back when I was still new to WPF, so I'm sure the process could be improved on.

The helper class I used to determine item visibility is listed below

public static ControlVisibility IsObjectVisibleInContainer(FrameworkElement child, UIElement parent)
{
    GeneralTransform childTransform = child.TransformToAncestor(parent);
    Rect childSize = childTransform.TransformBounds(new Rect(new Point(0, 0), new Point(child.Width, child.Height)));

    Rect result = Rect.Intersect(new Rect(new Point(0, 0), parent.RenderSize), childSize);
    if (result == Rect.Empty)
    {
        return ControlVisibility.Hidden;
    }
    else if (result.Height == childSize.Height && result.Width == childSize.Width)
    {
        return ControlVisibility.Full;
    }
    else if (result.Height == childSize.Height)
    {
        return ControlVisibility.FullHeightPartialWidth;
    }
    else if (result.Width == childSize.Width)
    {
        return ControlVisibility.FullWidthPartialHeight;
    }
    else
    {
        return ControlVisibility.Partial;
    }
}

public enum ControlVisibility
{
    Hidden,
    Partial,
    Full,
    FullHeightPartialWidth,
    FullWidthPartialHeight
}

It could be used like this:

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