WPF ContextMenu 文本对齐

发布于 2024-07-22 19:09:30 字数 186 浏览 1 评论 0原文

我正在为 WPF 应用程序构建一个上下文菜单,如果我有文本,然后添加图像,则文本始终与菜单项的顶部对齐,并且我不知道如何将其与中心对齐。

我已经尝试了 VerticalAlignmentVerticalContentAlignment 属性,但它们没有帮助......有什么想法吗?

I am building a context menu for a WPF application and if I have text and then I add an image, the text is always aligned at the top of the menu item and I can't figure out how to align it to the center.

I have tried the VerticalAlignment and VerticalContentAlignment properties but they dont help.. any ideas?

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

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

发布评论

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

评论(2

沒落の蓅哖 2024-07-29 19:09:30

我想这取决于您使用哪种面板来承载文本和图像。 我尝试使用 StackPanel,添加 VerticalAlignment="Center" 后,文本就正确对齐了。 如果仍然无法解决您的问题,请提供更多信息。

<Button Content="Right-click me">
    <Button.ContextMenu>
        <ContextMenu>
            <MenuItem>
                <MenuItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock VerticalAlignment="Center">Menu item 1</TextBlock>
                        <Image Source="image.png" Height="50" />
                    </StackPanel>
                </MenuItem.Header>
            </MenuItem>
        </ContextMenu>
    </Button.ContextMenu>
</Button>

I guess it depends on what kind of panel you are using to host the text and the image. I tried with a StackPanel and once I added VerticalAlignment="Center", the text was aligned correctly. Please provide some more information if it still doesn't work out for you.

<Button Content="Right-click me">
    <Button.ContextMenu>
        <ContextMenu>
            <MenuItem>
                <MenuItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock VerticalAlignment="Center">Menu item 1</TextBlock>
                        <Image Source="image.png" Height="50" />
                    </StackPanel>
                </MenuItem.Header>
            </MenuItem>
        </ContextMenu>
    </Button.ContextMenu>
</Button>
独木成林 2024-07-29 19:09:30

每当我遇到这样的困难时,我都会启动 Snoop (https://github.com/snoopwpf/snoopwpf) 并用它来找出哪个控件对齐不正确。

Whenever I get stuck like this, I fire up Snoop (https://github.com/snoopwpf/snoopwpf) and use it to figure out which control is aligned incorrectly.

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