WPF 窗口自适应
2015-02-04 15:38 stoneniqiu 阅读(4082) 评论(0) 编辑 收藏 举报窗口自适应就是说,当主窗口缩放的时候,内部的控件位置自动的调整,而不是隐藏掉。这主要依赖于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>
但也不是说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组合的时候要注意。
关注书山有路,用自己的知识体系去丈量世界!
书山有路群:452450927
书山有路群:452450927
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义