UWP xaml:如何显示带有图标和文本的按钮? [英] UWP xaml: How to display a button with icon and text in it?

查看:164
本文介绍了UWP xaml:如何显示带有图标和文本的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在按钮中显示图像/图标和文本?

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="&#xE779;" 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.

我也尝试过:

<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?

感谢阅读.

推荐答案

因此,由于我找不到直接的重复项(我可以发誓这是之前提出的问题类型),我想我会提供一个合法答案.

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.

按钮模板的核心是ContentPresenter,它允许您传入任何CLR对象.需要注意的是,只有一个可以通过.但是,如果该对象是一个能够容纳子对象的面板,那么它将作为内容传递所有内容.

At the heart of the button's template is a ContentPresenter which allows you to pass in any CLR object. 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>

因此,由于StackPanel是一个可以承载子代的CLR对象,因此您很高兴.希望这会有所帮助,加油!

So since StackPanel is a CLR object that can host children, you're good to go. Hope this helps, cheers!

这篇关于UWP xaml:如何显示带有图标和文本的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆