c# - 如何插入一个没有边框、带有背景图片和文本的按钮

标签 c# wpf button border

我想创建一个没有边框、带有背景图像并插入中心文本的 WPF 按钮。所以我有这个:

         <Style TargetType="Button" x:Key="TransparentButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="Transparent">
                            <ContentPresenter/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

       <Button Style="{StaticResource TransparentButton}">
          <Button.Content>
             <StackPanel Orientation="Horizontal">
                <Image Source="./Resources/Images/bottone_indietro.png" />
                <Label content="Pippo"/>
             </StackPanel>
          </Button.Content>
       </Button>

它有效,但文本“Pippo”显示在图像右侧。可以让文字居中吗???

问候...

最佳答案

如果您想在图像上居中对齐文本,您可以将这两个元素放在 Grid 而不是 StackPanel

<Grid>
    <Image Source="./Resources/Images/bottone_indietro.png" />
    <Label Content="Pippo" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>

StakcPanel 将水平或垂直堆叠其子元素,而 Grid 允许您将一个元素放在其他元素之上。来自 MSDN :

Child elements of a Grid are drawn in the order in which they appear in markup or code. As a consequence, layered order (also known as z-order) can be achieved when elements share the same coordinates

您可以阅读有关 WPF 面板的更多信息 here

关于c# - 如何插入一个没有边框、带有背景图片和文本的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25850230/

相关文章:

c# - 在 ConverterParameter 中使用枚举

c# - WPF 中的比例最小宽度

wpf - 为什么 XAML 资源与 CSS 样式不同?

c# - 是否有任何 C# 数学库可以进行插值/外推

c# - 如何通过网络流 c# 接收大文件?

wpf - 在包含按钮的 ListView 中,如何获取单击的索引?

java - CountDownTimer 开始按钮

HTML 为什么表单中的按钮会发送数据?

c# - 如何查询子表值

c# - SonarQube 分析 scss/sass 文件?