WPF 工具栏分隔符在 StackPanel 内时缩小为空

发布于 2024-08-01 17:57:28 字数 842 浏览 3 评论 0原文

考虑到非常简单的 wpf 应用程序,

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="800">
    <Grid>
        <ToolBar Height="50" >
            <MenuItem Header="Test1" />
            <MenuItem Header="Test2" />

            <StackPanel Orientation="Horizontal">
                <Separator />
                <MenuItem Header="Test3" />
                <MenuItem Header="Test4" />
                <MenuItem Header="Test5" />
            </StackPanel>
        </ToolBar>
    </Grid>
</Window>

Separator 元素会缩小到没有。 如果我将分隔符放在 StackPanel 开始之前,它就会显示出来。 为什么会出现这种情况? 是否可以在某处应用样式设置来避免这种情况?

Given the very simple wpf app

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="800">
    <Grid>
        <ToolBar Height="50" >
            <MenuItem Header="Test1" />
            <MenuItem Header="Test2" />

            <StackPanel Orientation="Horizontal">
                <Separator />
                <MenuItem Header="Test3" />
                <MenuItem Header="Test4" />
                <MenuItem Header="Test5" />
            </StackPanel>
        </ToolBar>
    </Grid>
</Window>

The Separator element shrinks to nothing. If I put the Separator just before the StackPanel begins, it will show up. Why does this happen? Is there a style setting that can be applied somewhere to avoid this?

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

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

发布评论

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

评论(3

旧情别恋 2024-08-08 17:57:28

工具栏对于你放入其中的内容很有趣。 当所有元素都不是工具栏的直接子元素时,它们会变得很有趣。 分组元素是 ToolBarTray(工具栏组)、ToolBar 和 ToolBarPanel(逻辑,用于折叠溢出)。 这就是 WPF 希望看到的:

<Grid>
    <ToolBarTray>
        <ToolBar Height="Auto">
            <ToolBarPanel Orientation="Horizontal" ToolBar.OverflowMode="AsNeeded"/>
            <MenuItem Header="Test1" />
            <Separator/>
            <MenuItem Header="Test2" />
        </ToolBar>
        <ToolBar Height="Auto">
            <ToolBarPanel ToolBar.OverflowMode="Never"/>
            <MenuItem Header="Test3" />
            <MenuItem Header="Test4" />
            <Separator/>
            <MenuItem Header="Test5" />
            <ToolBarPanel ToolBar.OverflowMode="AsNeeded"/>
            <MenuItem Header="Test6" />
            <MenuItem Header="Test7" />
        </ToolBar>
    </ToolBarTray>
</Grid>

ToolBars are funny about what you put inside. They get funny when all the elements aren't direct children of the ToolBar. The grouping elements are ToolBarTray (group of toolbars), ToolBar, and ToolBarPanel (logical, for collapsing overflow). This is what WPF wants to see:

<Grid>
    <ToolBarTray>
        <ToolBar Height="Auto">
            <ToolBarPanel Orientation="Horizontal" ToolBar.OverflowMode="AsNeeded"/>
            <MenuItem Header="Test1" />
            <Separator/>
            <MenuItem Header="Test2" />
        </ToolBar>
        <ToolBar Height="Auto">
            <ToolBarPanel ToolBar.OverflowMode="Never"/>
            <MenuItem Header="Test3" />
            <MenuItem Header="Test4" />
            <Separator/>
            <MenuItem Header="Test5" />
            <ToolBarPanel ToolBar.OverflowMode="AsNeeded"/>
            <MenuItem Header="Test6" />
            <MenuItem Header="Test7" />
        </ToolBar>
    </ToolBarTray>
</Grid>
卖梦商人 2024-08-08 17:57:28

分隔符默认为水平方向。

直接放置在工具栏内的分隔符的样式已更改,因为工具栏会覆盖其项目的默认样式。 放置在其他地方的分隔符将获得分隔符的默认样式。 因此,如果您想将分隔符保留在 StackPanel 内,则需要自己设置分隔符的样式。

此代码项目讨论包括完成此任务的示例代码。

参考:Adam Nathan 的 WPF Unleashed,第 117 页。

Separators default to Horizontal orientation.

Separators placed directly inside a ToolBar have their styles changed, because Toolbar overrides the default styles of its items. Separators placed elsewhere get the default style of a separator. So you will need to style the separator yourself if you vwant to keep it inside the StackPanel.

This CodeProject discussion includes sample code for accomplishing this.

Reference: WPF Unleashed by Adam Nathan, page 117.

扛起拖把扫天下 2024-08-08 17:57:28

StackPanel 正在以某种方式改变 Separator 的方向。 请注意,如果您明确指定 Separator 的宽度为 20 个单位,则 Separator 将是水平线而不是垂直线。 这就是正在发生的事情的一部分。

如果将 LayoutTransform 应用于 Separator,它将撤消 StackPanel 正在执行的任何操作。

<Separator>
    <Separator.LayoutTransform>
        <RotateTransform
            Angle="90" />
    </Separator.LayoutTransform>
</Separator>

不过,我不明白是否需要 StackPanel。

The StackPanel is changing the orientation of the Separator somehow. Note that if you explicitly tell the Separator to be 20 units wide, the Separator will be a horizontal line instead of a vertical line. That's part of what's going on.

If you apply a LayoutTransform to the Separator, it undoes whatever the StackPanel is doing.

<Separator>
    <Separator.LayoutTransform>
        <RotateTransform
            Angle="90" />
    </Separator.LayoutTransform>
</Separator>

I don't understand the need for a StackPanel, though.

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