MaterialSkin技术求助:获取下拉列表控件并保持单行文本框合理尺寸
解决方案:MaterialSkin v0.2.1 添加下拉列表并保持文本框尺寸一致
针对你遇到的MaterialSkin版本两难问题,这里提供三个可行的解决方向:
1. 基于原版v0.2.1自定义MaterialComboBox控件
直接继承WinForms的ComboBox,按照MaterialSkin v0.2.1的视觉风格重绘控件,确保高度与原版文本框(23px)一致,同时实现主题同步:
using MaterialSkin; using System.Drawing; using System.Windows.Forms; public class MaterialComboBox : ComboBox { private MaterialSkinManager skinManager => MaterialSkinManager.Instance; public MaterialComboBox() { // 设置与原版MaterialTextBox一致的高度 this.Height = 23; this.DrawMode = DrawMode.OwnerDrawFixed; this.ItemHeight = 23; this.FlatStyle = FlatStyle.Flat; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // 绘制Material风格的边框和下拉箭头 var borderColor = skinManager.ColorScheme.PrimaryColor; var arrowColor = Enabled ? skinManager.ColorScheme.TextColor : skinManager.ColorScheme.DisabledTextColor; // 绘制底部边框 using (var pen = new Pen(borderColor, 2)) { e.Graphics.DrawLine(pen, 0, this.Height - 2, this.Width, this.Height - 2); } // 绘制下拉箭头 var arrowRect = new Rectangle(this.Width - 20, (this.Height - 10) / 2, 10, 10); using (var brush = new SolidBrush(arrowColor)) { e.Graphics.FillPolygon(brush, new[] { new Point(arrowRect.Left, arrowRect.Top), new Point(arrowRect.Right, arrowRect.Top), new Point(arrowRect.Left + arrowRect.Width / 2, arrowRect.Bottom) }); } } protected override void OnEnabledChanged(System.EventArgs e) { base.OnEnabledChanged(e); this.Invalidate(); } }
使用时直接将这个控件拖到窗体,或者代码实例化,能完美匹配原版v0.2.1的文本框尺寸和风格。
2. 修改MaterialSkin.2源码调整文本框高度
如果倾向于使用带下拉控件的MaterialSkin.2分支,可修改源码解除高度限制:
- 下载MaterialSkin.2的源码,找到
MaterialTextBox和MaterialTextBox2类 - 移除或修改强制高度的代码(比如构造函数里的
Height = 50或相关布局逻辑) - 调整
Padding属性,将垂直内边距改小(比如设为new Padding(8, 2, 8, 2)) - 重新编译生成dll,替换项目中的NuGet引用
这样既能保留MaterialSkin.2的下拉控件,又能让文本框高度回到23px的合理尺寸。
3. 混合使用原版控件+风格适配的标准ComboBox
如果不想自定义或修改源码,可对标准WinForms ComboBox做风格适配:
- 设置
ComboBox的FlatStyle = FlatStyle.Flat - 取消边框,手动绘制与原版MaterialTextBox一致的底部边框
- 设置字体、颜色与MaterialSkin主题同步
- 调整高度为23px,确保视觉上和其他控件统一
这种方式成本最低,能快速解决风格不一致的问题。
内容的提问来源于stack exchange,提问作者Stewart
相关产品推荐
相关产品推荐

