简体   繁体   中英

WPF ContextMenu text alignment

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?

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>

每当我遇到这种情况时,我都会启动 Snoop ( https://github.com/snoopwpf/snoopwpf ) 并使用它来确定哪个控件未正确对齐。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM