日期:2014-05-17  浏览次数:21097 次

WPF,这个布局为什么不靠左
<Button Margin="136,118,0,0" Width="148" Height="86">
    <TextBlock Text="熊俊" Height="55" Width="93" HorizontalAlignment="Left"  Foreground="Red"/>
</Button>



Button是TextBlock的父元素,我在TextBlock中设置了对于父元素的水平对齐特征HorizontalAlignment属性值为Left,可是结果为什么没有靠左呢?

------解决方案--------------------
你在TextBlock中设置HorizontalAlignment这个是表示TextBlock中显示的文字的对齐特征
------解决方案--------------------
我很好奇,从图片上面我已经看到了左对齐了,textblock不就是靠左了? 至于textblock的容器button是很明显无法左对齐的,因为设置了margin
------解决方案--------------------
<Button HorizontalContentAlignment="Left Margin="136,118,0,0" Width="148" Height="86">
     <TextBlock Text="熊俊" Height="55" Width="93" HorizontalAlignment="Left"  Foreground="Red"/>
 </Button>
TextBlock是Button的Content,不管你怎设TextBlock都不会靠左的,只有设Button的Contewnt属性
------解决方案--------------------
引用:
Quote: 引用:

<Button HorizontalContentAlignment="Left Margin="136,118,0,0" Width="148" Height="86">
     <TextBlock Text="熊俊" Height="55" Width="93" HorizontalAlignment="Left"  Foreground="Red"/>
 </Button>
TextBlock是Button的Content,不管你怎设TextBlock都不会靠左的,只有设Button的Contewnt属性

能有MSDN官方的解释吗?给个链接,谢谢

这还要解解释,因为你的Button的Content是默认居中对齐的而且是大小是自动的,而不是Stretch

你的TextBlock是在Button的Content中
你要是设Button的HorizontalContentAlignment="Stretch"后你的TextBlock才会起作用
你应去看一下ContentControl
------解决方案--------------------
你要理解,Button不是容器,Button的Content所有都是由ContentPresenter显示出来的。
------解决方案--------------------
引用: