Skip to main content
 首页 » 编程设计

c#之如何在 WinRT 中的按钮中将文本放置在图像上

2024年02月06日11findumars

我想创建一个使用图像作为背景的按钮,我想在背景之上放置我的文本。

我试过这样的:

<Button Style="{StaticResource ImageButtonStyle}"> 
    <StackPanel> 
        <TextBlock Text="test"></TextBlock> 
        <Image Source="ms-appx:///Skins/Images/buton.png" Stretch="None" /> 
    </StackPanel> 
</Button> 

文本不会正确居中。

<Button Style="{StaticResource ImageButtonStyle}"> 
    <StackPanel> 
        <TextBlock Text="test"></TextBlock> 
        <Label Padding="0">My Button Text</Label> 
    </StackPanel> 
</Button> 

控件 Label 不存在。

我如何在第一次尝试时将我的文字正确居中放置在图像上? 你知道更好的方法吗?

请您参考如下方法:

您应该使用网格代替堆栈面板。尝试这样的事情:

 <Button > 
        <Grid> 
            <Image Source="..." Stretch="None" /> 
            <TextBlock Text="test" VerticalAlignment="Center" HorizontalAlignment="Center"/> 
        </Grid> 
  </Button>