WPF真入门教程11--UI布局8

这节操作下DockPanel

1、介绍DockPanel

DockPanel使得在所有四个方向(顶部、底部、左侧和右侧)都可以很容易地停靠内容。这在很多情况下都是一个很好的选择,其中您希望将窗口划分为特定的区域,尤其是因为默认情况下,DockPanel内的最后一个元素(除非该特性被特别禁用)将自动填充剩余的空间(中心)。它其实就是在WinForm类似于Dock属性的元 素。DockPanel会对每个子元素进行排序,并停靠在面板的一侧,多个停靠在同侧的元素则按顺序排序。 

   如果将 LastChildFill 属性设置为 true(默认设置),那么无论对 DockPanel 的最后一个子元素设置的其他任何停靠值如何,该子元素都将始终填满剩余的空间。若要将子元素停靠在另一个方向,必须将 LastChildFill 属性设置为 false,还必须为最后一个子元素指定显式停靠方向。

  2、DockPanel 常用的属性

    DockPanel有一个Dock附加属性,因此子元素用4个值来控制她们的停靠:Left、Top、Right、Bottom。

    HorizontalAlignment:获取或设置在父元素(如 Panel 或项控件)中组合此元素时所应用的水平对齐特征。

    VerticalAlignment:获取或设置在父元素(如面板或项控件)中组合此元素时所应用的垂直对齐特征。

    Margin:获取或设置元素的外边距。

    Opacity:透明度

    LastChildFill:获取或设置一个值,该值指示 DockPanel 中的最后一个子元素是否拉伸以填充剩余的可用空间,默认为True(填充)。 

   3、示例代码

代码1:填充剩余空间,自动停靠在边界

<DockPanel>
	<Button DockPanel.Dock="Top" Content="Button Top"/>
	<Button DockPanel.Dock="Left"  Content="ButtonLeft"/>
	<Button DockPanel.Dock="Right" Content="Button Right"/>
	<Button DockPanel.Dock="Bottom" Content="Button Bottom"/>
	<Button Content="Button Center"/>
</DockPanel>

代码2:最后元素不填充剩余空间

<!--设置最后一个元素不填充剩余空间-->
<DockPanel LastChildFill="False">
	<Button DockPanel.Dock="Top" Content="Button Top"/>
	<Button DockPanel.Dock="Left"  Content="ButtonLeft"/>
	<Button DockPanel.Dock="Right" Content="Button Right"/>
	<Button DockPanel.Dock="Bottom" Content="Button Bottom"/>
	<Button DockPanel.Dock="Right" Content="Button Center "/>
	<Button Content="Button Center "/>
</DockPanel>

可以看出,各个按钮未设置高度和宽度,在DockPanel中设置位置后,均是拉伸考边界停靠的,不会交叉重叠;默认情况下,最后一个元素是占满剩余空间的

向窗口拖入DockPanel控件和Label,Rectangle,完整代码:

<Window x:Class="WpfApp6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp6"
        mc:Ignorable="d"
        Title="WPF基础控件" Height="758.275" Width="1078.5" Icon="images/22i.jpg" WindowStartupLocation="CenterScreen"  Loaded="Window_Loaded">
    <Grid Name="mygrid" >

        <Button Name="btnlogin"  IsDefault="True" Click="btnlogin_Click" Background="YellowGreen" BorderThickness="3"  BorderBrush="Red" Content="登录"  HorizontalAlignment="Left" Margin="166,95,0,0" VerticalAlignment="Top" Width="74" RenderTransformOrigin="-0.2,0.368"/>
        <Label Content="帐号" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="54,23,0,0" VerticalAlignment="Top" Width="102"/>
        <TextBox Name="txtname" HorizontalAlignment="Left" Height="23" Margin="142,23,0,0" TextWrapping="Wrap" Text="admin" VerticalAlignment="Top" Width="120"/>
        <Label Content="密码" HorizontalAlignment="Left" Margin="276,23,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.088,0.6"/>
        <PasswordBox Name="txtpass" HorizontalAlignment="Left" Height="22" Margin="330,23,0,0"  Password="123456" PasswordChar="#" VerticalAlignment="Top" Width="120"/>
        <Button Name="btncancle" IsCancel="True" Foreground="Chartreuse"  FontSize="14" Click="btncancle_Click" Content="取消" HorizontalAlignment="Left" Margin="266,95,0,0" VerticalAlignment="Top" Width="74">
            <Button.Background>
                <ImageBrush  ImageSource="images/32s.jpg"></ImageBrush>
            </Button.Background>
        </Button>
        <RadioButton Content="老师"  GroupName="role" HorizontalAlignment="Left" Margin="156,65,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
        <RadioButton Content="学生"  GroupName="role" HorizontalAlignment="Left" Margin="230,65,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.14,0.357" Checked="RadioButton_Checked"/>
        <RadioButton Content="管理员"   GroupName="role" HorizontalAlignment="Left" Margin="303,65,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
        <!-- IsThreeState属性指定 ToggleButton 是否有两种或三种状态:true,false,null,即选中 、清除 和不确定-->
        <CheckBox Content="汽车" Name="chkexcel" IsChecked="True" IsThreeState="True"  HorizontalAlignment="Left" Margin="45,138,0,0" VerticalAlignment="Top"/>
        <CheckBox Content="阅读"   HorizontalAlignment="Left" Margin="118,138,0,0" VerticalAlignment="Top"/>
        <CheckBox Content="唱歌" HorizontalAlignment="Left" Margin="181,138,0,0" VerticalAlignment="Top"/>
        <CheckBox Content="体育" HorizontalAlignment="Left" Margin="241,138,0,0" VerticalAlignment="Top"/>
        <Button Content="获取复选框" HorizontalAlignment="Left" Margin="301,135,0,0" VerticalAlignment="Top" Width="91" Click="Button_Click"/>
        <Button Content="创建复选框" HorizontalAlignment="Left" Margin="409,134,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        <!--Image图片控件,Stretch指定拉伸模式 -->
        <Image HorizontalAlignment="Left"  Name="myimg" Height="83" Margin="45,184,0,0" VerticalAlignment="Top" Width="85" Source="images/6p.jpg" Stretch="Fill" RenderTransformOrigin="0.541,0.157"/>
        <Button Content="相对替换" HorizontalAlignment="Left" Margin="152,187,0,0" VerticalAlignment="Top" Width="69" Height="32" Click="Button_Click_2"/>
        <Button Content="绝对替换" HorizontalAlignment="Left" Margin="152,240,0,0" VerticalAlignment="Top" Width="69" Height="28" RenderTransformOrigin="0.536,0.815" Click="Button_Click_3"/>
        <!-- Border边框控件,与布局面板一起使用,里面只能有一个子元素,BorderThickness边框粗细,BorderBrush边框颜色,CornerRadius圆角大小,Background背景色-->
        <Border BorderBrush="red" BorderThickness="3" HorizontalAlignment="Left" Height="32" Margin="266,184,0,0" VerticalAlignment="Top" Width="100">
            <Label Content="我有边框" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Border>
        <Border BorderBrush="BlueViolet" BorderThickness="5" CornerRadius="9" Background="Bisque" HorizontalAlignment="Left" Height="38" Margin="266,229,0,0" VerticalAlignment="Top" Width="100">
            <Label Content="我有圆角" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="16,1,16,3"/>
        </Border>
        <!--ComboBox下拉框,IsDropDownOpen指定是否展开,默认不展开 -->
        <ComboBox HorizontalAlignment="Left" Name="hecbm" IsDropDownOpen="False" Margin="403,187,0,0" VerticalAlignment="Top" Width="81" Height="22">
            <ComboBoxItem Content="请选择" IsSelected="True" Background="#FF26D365"/>
            <ComboBoxItem Content="亚洲"/>
            <ComboBoxItem Content="非洲"/>
            <ComboBoxItem Content="欧洲"/>
        </ComboBox>
        <ComboBox Name="mycbm" HorizontalAlignment="Left" Margin="403,241,0,0" VerticalAlignment="Top" Width="81" Height="27" RenderTransformOrigin="0.247,0.481"/>
        <ListBox HorizontalAlignment="Left" Height="95" Margin="45,290,0,0" VerticalAlignment="Top" Width="100" RenderTransformOrigin="0.21,0.484">
            <ListBoxItem Content="茶杯" IsSelected="True"/>
            <ListBoxItem Content="茶叶"/>
            <ListBoxItem Content="茶水"/>
            <ListBoxItem Content="茶桌"/>
        </ListBox>
        <ListBox Name="mylist" HorizontalAlignment="Left" Height="95" Margin="166,290,0,0" VerticalAlignment="Top" Width="100"/>
        <!-- DatePicker日期控件-->
        <DatePicker Focusable="False"  HorizontalAlignment="Left"  Margin="301,290,0,0" VerticalAlignment="Top" Width="106" SelectedDateFormat="Long" />
        <!--Slider 滑动控件,Orientation刻度方向(默认水平),TickPlacement刻度位置(默认没有):both/left/right/none,TickFrequency刻度间隔,IsSnapToTickEnabled表示数值变化形式(true代表整形,false代表浮点)-->
        <Slider Name="sd1" HorizontalAlignment="Left" Margin="303,327,0,0" TickPlacement="Both" TickFrequency="1" IsSnapToTickEnabled="true" Maximum="100" Minimum="1" Value="1" VerticalAlignment="Top" Width="104"/>
        <!--Label控件, Content指定内容:Binding绑定,ElementName绑定控件名称,Path绑定控件的value值,Mode=OneWay指单向影响-->
        <Label Content="当前值:" HorizontalAlignment="Left" Margin="304,362,0,0" VerticalAlignment="Top"/>
        <Label Content="{Binding ElementName=sd1,Path=Value,Mode=OneWay}" Name="mylbl" HorizontalAlignment="Left" Margin="360,362,0,0" VerticalAlignment="Top" Width="47"/>
        <Slider HorizontalAlignment="Left" Name="sd2" Margin="421,282,0,0" TickPlacement="BottomRight" TickFrequency="5"  Orientation="Vertical" Maximum="100" Minimum="1" Value="10" VerticalAlignment="Top" Height="103" Width="33" Background="#FFCCFB2D" BorderBrush="#FF4118B0"/>
        <!--TextBlock文字块 -->
        <TextBlock FontSize="{Binding ElementName=sd2,Path=Value,Mode=OneWay}"  HorizontalAlignment="Left"  Margin="459,290,0,0" TextWrapping="Wrap" Text="看我" VerticalAlignment="Top" Height="86" Width="61"/>
        <!--ProgressBar进度条:Orientation方向(Horizontal/Vertical,水平/垂直),IsIndeterminate指定连续进度 -->
        <ProgressBar  Orientation="Horizontal"  IsIndeterminate="true" Value="30"  HorizontalAlignment="Left" Height="18" Margin="48,394,0,0" VerticalAlignment="Top" Width="327"/>
        <ProgressBar Name="ps2"  Orientation="Vertical" Value="0" Minimum="0" Maximum="120" HorizontalAlignment="Left" Height="149" Margin="402,394,0,0" VerticalAlignment="Top" Width="29"/>
        <Button Content="加载进度" HorizontalAlignment="Left" Margin="441,434,0,0" VerticalAlignment="Top" Width="60" Click="Button_Click_4"/>
        <Label Content="当前:" HorizontalAlignment="Left" Margin="438,469,0,0" VerticalAlignment="Top"/>
        <Label Content="" Name="ps2lbl" HorizontalAlignment="Left" Margin="474,469,0,0" VerticalAlignment="Top"/>
        <!--StackPanel堆栈布局控件,默认对齐是垂直方向 -->
        <Label Content="堆积面板 (StackPanel)" HorizontalAlignment="Left" Margin="54,422,0,0" VerticalAlignment="Top" RenderTransformOrigin="-3.825,0.04"/>
        <StackPanel HorizontalAlignment="Left" Height="109" Cursor="ArrowCD" Margin="56,446,0,0" VerticalAlignment="Top" Width="143">
            <Button>吉利</Button>
            <Button>长城</Button>
            <Button Margin="0,2,0,2">奇瑞</Button>
            <Button>红旗</Button>
            <Button>比亚迪</Button>
        </StackPanel>
        <Border BorderBrush="Crimson"  BorderThickness="1" HorizontalAlignment="Left" Height="92" Margin="219,450,0,0" VerticalAlignment="Top" Width="154">
            <StackPanel Margin="0,0,0,0" Orientation="Horizontal" >
                <Button Margin="5,0,5,0" Background="#FFA6C321">华山</Button>
                <Button Margin="5,0,5,0">峨眉</Button>
                <Button Margin="5,0,5,0">
                    <Button.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Black" Offset="0"/>
                            <GradientStop Color="#FFF7F0EA" Offset="1"/>
                        </LinearGradientBrush>
                    </Button.Background> 昆山
                </Button>
                <Button Margin="5,0,5,0">武当</Button>
            </StackPanel>
        </Border>
        <Label Content="换行面板 (WrapPanel)" HorizontalAlignment="Left" Margin="54,555,0,0" VerticalAlignment="Top"/>
        <!--WrapPanel流布局控件,默认对齐是垂直方向,Orientation元素布局方式 -->
        <Border BorderBrush="red"  BorderThickness="4" CornerRadius="12" Margin="56,560,569,26" Height="100">
            <WrapPanel Orientation="Horizontal" ItemHeight="30" ItemWidth="40" Margin="0,0,0,22"  >
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="海王星"/>
                <Button Content="森林星"/>
                <Button Content="森林星"/>
                <Button Content="森林星"/>
                <Button Content="森林星"/>
                <Button Content="森林星"/>
                <Button Content="森林星"/>
                <Button Content="森林星"/>
                <Button Content="森林星"/>
            </WrapPanel>

        </Border>
        <Label Content="表格式面板 (Grid)" HorizontalAlignment="Left" Margin="584,27,0,0" VerticalAlignment="Top"/>
        <Grid HorizontalAlignment="Left" Height="171" Margin="584,48,0,0"  VerticalAlignment="Top" Width="464" >
            <!--RowDefinitions 设置行:3行-->
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <!--RowDefinitions 设置列:3列-->
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <!--Row 的值表示行坐标,Column 的值表示列坐标-->
            <Button Grid.Row="0" Grid.Column="0">A1</Button>
            <Button Grid.Row="0" Grid.Column="1">A2</Button>
            <Button Grid.Row="0" Grid.Column="2">A3</Button>
            <Button Grid.Row="1" Grid.Column="0">A4</Button>
            <Button Grid.Row="1" Grid.Column="1">A5</Button>
            <Button Grid.Row="1" Grid.Column="2">A6</Button>
            <Button Grid.Row="2" Grid.Column="0">A7</Button>
            <Button Grid.Row="2" Grid.Column="1">A8</Button>
            <Button Grid.Row="2" Grid.Column="2">A9</Button>
        </Grid>
        <Label Content="表格式面板 (Grid)元素跨行设置 (RowSpan/ColumnSpan)" HorizontalAlignment="Left" Margin="592,248,0,0" VerticalAlignment="Top"/>
        <Grid HorizontalAlignment="Left" Height="100" Margin="603,282,0,0" VerticalAlignment="Top" Width="445">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <!--绿色的RowSpan跨2行-->
            <Rectangle  Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Fill="Green" />
            <!--黄色的RowSpan跨2行,ColumnSpan跨2列-->
            <Rectangle Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Fill="Yellow" />
            <!--红色的ColumnSpan跨2列-->
            <Rectangle Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Fill="Red" />
            <!--兰色的不跨行和列,位于第3行第3列-->
            <Rectangle Grid.Row="2" Grid.Column="2"   Fill="Blue" />
        </Grid>

        <!--菜单控件Menu,ToolTip提示对象,该属性用于获取或设置用户界面中为此元素显示的工具提示对象 -->
        <Menu HorizontalAlignment="Left" Height="75" Margin="603,403,0,0" VerticalAlignment="Top" Width="184">
            <MenuItem Header="文件" HorizontalAlignment="Left" ToolTip="文件菜单">
                <MenuItem Header="打开" />  
                <MenuItem Header="保存" />
            </MenuItem>
            <MenuItem Header="帮助" HorizontalAlignment="Left" ToolTip="帮助信息">
                <MenuItem Header="查看帮助" />
                <MenuItem Header="技术支持" />
                <Separator />
                <MenuItem Header="关于" />
            </MenuItem>
        </Menu>
        <!--画布控件Canvas -->
        <Canvas HorizontalAlignment="Left" Height="75" Margin="814,403,0,0" VerticalAlignment="Top" Width="230">
            <!---ViewBox这个控件通常和其他控件结合起来使用,  一个 Viewbox中只能放一个控件--> 
            <Viewbox Height="55" Canvas.Left="10" Canvas.Top="10" Stretch="Fill" Width="210">
                <TextBlock TextWrapping="Wrap" Text="我是内容"/>
            </Viewbox>
        </Canvas> 
        <Label Content="停靠面板 (DockPanel)" HorizontalAlignment="Left" Margin="584,486,0,0" VerticalAlignment="Top"/>
        <!--LastChildFill="False",表示是否始终填满剩余的空间-->
        <DockPanel HorizontalAlignment="Left" Height="175" LastChildFill="True" Margin="584,508,0,0" VerticalAlignment="Top" Width="460">
            <Rectangle DockPanel.Dock="Top" Width="200" Height="50" Fill="LightGray"/>
            <Rectangle DockPanel.Dock="Bottom"     Height="50" Fill="LightCoral"/>
            <Rectangle DockPanel.Dock="Left"  Width="200" Height="50" Fill="LightBlue"/>
            <Rectangle Width="40" Height="50" Margin="12" Fill="LightPink"/>
            <Rectangle DockPanel.Dock="Right"   Width="200" Height="50" Fill="LightGreen"/>
        </DockPanel>
    </Grid>
</Window>

运行起来,效果是:

 控件布局这块还有很多,每个又有很多设置效果,经过8次指导演练,已经真入门了,在实际应用中,可以上手做出效果了,只是一个细心的过程。其他没有讲的控件应该不复杂了。

 一步先步步先,开门红月月红,打响开门红,虎年我为王。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hqwest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值