zoukankan      html  css  js  c++  java
  • WPF 窗口自适应

        窗口自适应就是说,当主窗口缩放的时候,内部的控件位置自动的调整,而不是隐藏掉。这主要依赖于Grid布局。

        1.比如这个groupbox 本身是在一个Grid的Row中的。缩放之后,左边的button不见了。 

        开始:

        

        缩放之后:

       

       <GroupBox Name="EmailGroupBox" Grid.Row="1" Grid.Column="0" Header="电子邮件" Margin="0,10,10,10" >
                        <StackPanel Name="Panel"  Orientation="Horizontal">
                            <Grid Name="MinGrid" Width="{Binding ElementName=Panel, Path=ActualWidth}">
                                <Grid.RowDefinitions>
                                    <RowDefinition MinHeight="20" Height="Auto"></RowDefinition>
                                    <RowDefinition MinHeight="1" Height="*"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="60" Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="*" MinWidth="100"></ColumnDefinition>
                                    <ColumnDefinition MinWidth="70" Width="Auto" ></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Label Grid.Row="0" Grid.Column="0"  HorizontalAlignment="Left" >邮件地址:</Label>
                                <TextBox   TabIndex="1"  Grid.Row="0" Grid.Column="1" MinWidth="100"  Text="{Binding ServerEmailAddress, UpdateSourceTrigger=LostFocus}"  HorizontalAlignment="Stretch"  VerticalContentAlignment=                            "Center"></TextBox>
                                <Button  Grid.Row="0"  TabIndex="2" Grid.Column="2" HorizontalAlignment="Stretch"  Margin="5 0 5 0"  Click="EmailConfigButton_Click">设置</Button>
                            </Grid>
    
                        </StackPanel>
                    </GroupBox>

    拿掉 StackPanel就可以缩放(本身也是多余的代码)。最小宽高都可以省略。因为这里的最小宽度无法阻挡主窗口的缩放,最小宽高直接放到子控件上面就行,不必放在Row或col上面。 2个row也是多余 修改之后:

       <GroupBox Name="EmailGroupBox" Grid.Row="1" Grid.Column="0" Header="电子邮件" Margin="0,10,10,10" >
                        <Grid Name="MinGrid" Width="{Binding ElementName=Panel, Path=ActualWidth}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition />
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Label Grid.Row="0" Grid.Column="0"  HorizontalAlignment="Left" >邮件地址:</Label>
                            <TextBox   TabIndex="1"  Grid.Row="0" Grid.Column="1" MinWidth="100"  Text="{Binding ServerEmailAddress, UpdateSourceTrigger=LostFocus}"  HorizontalAlignment="Stretch"  VerticalContentAlignment="Center"></TextBox>
                            <Button  Grid.Row="0"  TabIndex="2" Grid.Column="2" HorizontalAlignment="Stretch"  Margin="5 0 5 0"  Click="EmailConfigButton_Click">设置</Button>
                        </Grid>
                    </GroupBox>
    View Code

    但也不是说StackPanel 就不能缩放。下面的StackPanel是有缩放的效果的。 

     <StackPanel Margin="5" Orientation="Horizontal" Grid.Row="3">
                        <Button   TabIndex="4"  HorizontalAlignment="Left" Click="AddCustomer" Width="62" Height="28" >新建</Button>
                        <Button   HorizontalAlignment="Left" TabIndex="5"  Click="DeleteCustomer"  Width="62" Margin="20,0,0,0" Height="28" VerticalAlignment="Bottom" >删除</Button>
      </StackPanel>

     和groupbox组合的时候要注意。

       

  • 相关阅读:
    Redis-cluster集群
    MySQL-binlog日志格式
    MySQL-备份与恢复
    PG-内存参数配置标准
    [POJ1724]Roads
    [POJ1062][最短路]昂贵的聘礼
    我的小博客美化记(仍在持续修改和增加,敬请持续关注)
    [离散化模板][并查集][洛谷1955]程序自动化分析
    python调用百度接口解析/逆解析地址
    datax安装部署
  • 原文地址:https://www.cnblogs.com/stoneniqiu/p/4272521.html
Copyright © 2011-2022 走看看