我想创建一个没有边框、带有背景图像并插入中心文本的 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/