按钮中的 WPF 图像和文本对齐方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18226341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:26:02  来源:igfitidea点击:

WPF image and text alignment in button

wpfxamlbutton

提问by Babak.Abad

I need a way to align a label to right and align an image to right. I tried this code:

我需要一种将标签向右对齐并将图像向右对齐的方法。我试过这个代码:

<Button HorizontalAlignment="Left" Margin="11,265,0,0" VerticalAlignment="Top" Width="190" Height="51">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                <Image Source="Resources/Accept-icon.png" Stretch="Uniform" HorizontalAlignment="Left"/>
                <Label Content="?????" HorizontalContentAlignment="Right" VerticalAlignment="Center" FontFamily="2  badr" FontSize="20"/>
            </StackPanel>
        </Button> 

But I see label sticks to image.

但我看到标签贴在图像上。

Alsois there any way to have some parameter like cell padding (from right/left/top/bottom)?

有没有什么办法能有像单元格填充了一些参数(由左/右/上/下)?

回答by Dutts

Try using a DockPanelinstead

尝试使用 aDockPanel代替

<Button HorizontalAlignment="Left" Margin="11,265,0,0" VerticalAlignment="Top" Width="190" Height="51">
  <DockPanel HorizontalAlignment="Stretch">
    <Image Source="Resources/Accept-icon.png" Stretch="Uniform" DockPanel.Dock="Left"/>
    <Label Content="?????" DockPanel.Dock="Right" VerticalAlignment="Center" FontFamily="2  badr" FontSize="20"/>
  </DockPanel>
</Button> 

For your padding question, which element are you trying to pad?

对于您的填充问题,您要填充哪个元素?

回答by dkozl

try using Gridinstead of StackPanel

尝试使用Grid代替StackPanel

<Grid>
   <Image ... HorizontalAlignment="Left"/>
   <Label ... HorizontalAlignment="Right"/>
</Grid>

there is Paddingproperty which is published by types like Block, Border, Control, and TextBlockso for example it will not be published by Imagecontrol, which inherits directly from FrameworkElement, but will be by Labelwhich is a Control

Padding其通过类型出版喜欢属性BlockBorderControl,和TextBlock因此,例如它不会被发表Image控制,直接它继承自FrameworkElement,但将通过Label这是一个Control