[英]UWP xaml: How to display a button with icon and text in it?
How can I display an image/icon AND text in a button? 如何在按钮中显示图像/图标和文字?
<StackPanel Name="stackPanel" Orientation="Horizontal" Tapped="stackPanel_Tapped">
<Button x:Name="button" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" />
<TextBlock Text="Grades" FontSize="18" VerticalAlignment="Center" />
</StackPanel>
I can add a taplistener to the stackpanel, but that won't make the stackpanel have visual effect like a real button. 我可以在stackpanel中添加一个taplistener,但这不会使stackpanel像真正的按钮一样具有视觉效果。
I've also tried: 我也尝试过:
<Button FontFamily="Segoe MDL2 Assets" Width="50" Height="50" x:Name="button" Content="test">
<Image x:Name="button" Source="Assets/test.png" />
</Button>
But I can't set the content twice. 但我不能两次设置内容。 I want both text and an icon in a button so when a user clicks on it, it will have a visual effect like a button.
我想要一个按钮中的文本和图标,所以当用户点击它时,它将具有像按钮一样的视觉效果。 Is this possible?
这可能吗? Or does anyone else have another method to achieve this?
或者其他人有其他方法来实现这一目标吗?
Thanks for reading. 谢谢阅读。
So since I couldn't find a direct duplicate (which I could swear this is the type of question that's been asked before) I guess I'll supply a legitimate answer. 所以,因为我找不到直接复制品(我可以发誓这是之前被问过的问题类型)我想我会提供一个合理的答案。
At the heart of the button's template is a ContentPresenter
which allows you to pass in any CLR object. 按钮模板的核心是
ContentPresenter
,它允许您传入任何CLR对象。 The caveat though is that only one can be passed. 但需要注意的是,只有一个可以通过。 HOWEVER, if that object is a panel capable of containing children then it will pass everything through as the content.
但是,如果该对象是一个能够包含子节点的面板,那么它将作为内容传递所有内容。
So in this instance if we did it this way: 所以在这种情况下,如果我们这样做:
<Button>
<Image/>
<TextBlock/>
</Button>
Then it will fail and complain about only one object allowed at a time. 然后它会失败并抱怨一次只允许一个对象。
Except when you provide a parent panel to hold those children, it respects it all as content and will provide the desired result as example: 除非您提供父母小组来容纳这些孩子,否则它将所有孩子视为内容,并将提供所需的结果作为示例:
<Button>
<StackPanel>
<Image/>
<TextBlock/>
</StackPanel>
</Button>
So since StackPanel
is a CLR object that can host children, you're good to go. 因为
StackPanel
是一个可以托管孩子的CLR对象,所以你很高兴。 Hope this helps, cheers! 希望这会有所帮助,欢呼!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.