You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms部署到其他电脑时锚定DataGridView无法按预期扩展

C# WinForms 嵌套SplitContainer跨设备布局异常问题

问题背景

开发环境为Visual Studio 2022,开发C# WinForms应用程序。应用内某用户控件嵌套了多层SplitContainer控件,容器面板内分为两种布局:

  • 第一种:面板内放置Dock属性设为Fill的DataGridView,控件填充整个面板,该部分在所有环境下运行均正常
  • 第二种:面板内先放置静态Label控件,Label下方放置Anchor属性设为四边锚定的DataGridView。设计预期为窗口尺寸调整时DataGridView自动适配大小,Label保持静态位置、始终可见。

开发阶段本地调试时该布局逻辑运行完全符合预期,但程序部署到部分同事的电脑运行时,DataGridView无法随窗口尺寸正常扩展,面板底部会出现空白区域,且该问题在不同设备上表现不一致,初步怀疑问题源于发布配置,或是控件本身遗漏了相关设置。

应用发布配置

  • 目标框架:net6.0-windows
  • 部署模式:独立部署(Self-contained)
  • 目标运行时:win-x86

参考截图

已尝试的修复方案

  1. 曾为项目添加app.manifest清单文件,加入如下高DPI感知配置,配置后问题未解决:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
  <windowsSettings>
    <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
  </windowsSettings>
</application>
  1. 上述方案无效后,移除了清单文件,直接修改csproj项目文件配置高DPI模式,修改后异常表现未发生变化,问题仍然存在:
<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>net6.0-windows</TargetFramework>
  <Nullable>enable</Nullable>
  <UseWindowsForms>true</UseWindowsForms>
  <ImplicitUsings>enable</ImplicitUsings>
  <ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
</PropertyGroup>

内容的提问来源于stack exchange,提问作者Ryan Frank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 17:57:35