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

WPF,请问两个关于Margin属性的布局
第一段代码:

<Grid>        
    <Button Margin="50,0,0,0" HorizontalAlignment="Left" Content="button1" Width="60" />
    <Button Margin="50,0,0,0" HorizontalAlignment="Left" Content="button2" Width="60" />
</Grid>


两个Button的左边距都是50,因此button1和button2重叠了。

第二段代码:

<Grid>    
    <TabControl HorizontalAlignment="Left" Height="101" Grid.Row="1" VerticalAlignment="Top" Width="243" >
        <TabItem Header="TabItem" Height="25" Margin="7,0,0,0" />
        <TabItem Header="TabItem" Height="25" Margin="7,0,0,0" />
        <TabItem Header="TabItem" Height="25" Margin="7,0,0,0" />
    </TabControl>
</Grid>


TabItem的容器是TabPanel,三个TabItem的Margin都是同一个值,但是为什么三个TabItem没有重叠呢?

------解决方案--------------------
取决于布局,布局确定了位置,外间距是后计算的。
------解决方案--------------------
楼主TabControl放在grid里面
你比较的是grid的里面的布局
为什么又在看TabControl里面的布局
TabControl?是个headercontent的控件
TabItem在一个横向的stackpanel里面
所以你的margin就是在一个stackpanel里面的三个子元素的margin
楼主应该看看TabControl?的template是怎么定义的
http://blog.csdn.net/lhx527099095/article/details/7999009
------解决方案--------------------
引用:
Quote: 引用:

楼主TabControl放在grid里面
http://blog.csdn.net/lhx527099095/article/details/7999009

我看了TabControl的控件模板的,是TabPanel吧,不是stackpanel



你完全可以写成其他的panel无所谓 看你自己的需求了
比如你要一个左右侧边的tabcontrol就可以重写
IsItemsHost="True"这个是关键


<TabControl.Template>
    <ControlTemplate TargetType="{x:Type TabControl}">
      <Grid ClipToBounds="True" KeyboardNavigation.TabNavigation="Local" SnapsToDevicePixels="True">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Name="ColumnDefinition0" Width="*"/>
          <ColumnDefinition Name="ColumnDefinition2" Width="0"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Name="RowDefinition0" Height="Auto"/>
         &nb