i am building a context menu for a WPF c# application and just for simplicity's sake, if i have text and then i add an image, the text is always aligned at the top of the menu items cell and i cant figure out how to align it to the center. i have tried the veticalalignment property and veticalcontentalignment property but they dont help.. any ideas?
我正在为WPF c#应用程序构建一个上下文菜单,为了简单起见,如果我有文本然后我添加一个图像,文本总是对齐在菜单项单元格的顶部,我无法弄清楚如何对齐它到中心。我已经尝试了veticalalignment属性和veticalcontentalignment属性,但他们没有帮助..任何想法?
2 个解决方案
#1
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.
我想这取决于您用来托管文本和图像的面板类型。我尝试使用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>
#2
Whenever I get stuck like this, I fire up Snoop (http://blois.us/snoop) - make sure your app is set to compile as 32-bit (it's not by default!), then use Snoop to figure out which control is aligned incorrectly
每当我像这样陷入困境时,我会启动Snoop(http://blois.us/snoop) - 确保你的应用程序设置为32位编译(默认情况下不是!),然后使用Snoop找出哪个控件对齐不正确
#1
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.
我想这取决于您用来托管文本和图像的面板类型。我尝试使用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>
#2
Whenever I get stuck like this, I fire up Snoop (http://blois.us/snoop) - make sure your app is set to compile as 32-bit (it's not by default!), then use Snoop to figure out which control is aligned incorrectly
每当我像这样陷入困境时,我会启动Snoop(http://blois.us/snoop) - 确保你的应用程序设置为32位编译(默认情况下不是!),然后使用Snoop找出哪个控件对齐不正确