C# 中的右对齐组合框
默认情况下,C# 组合框中的项目左对齐。 除了重写 DrawItem 方法和设置组合框绘制模式之外,是否有任何选项可用于更改此理由 --> DrawMode.OwnerDrawFixed?
干杯
By default the items in the C# Combobox are left aligned.
Are there any options available to change this justification apart from overriding DrawItem method and setting the combobox drawmode --> DrawMode.OwnerDrawFixed?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不介意另一侧的放置小部件,则可以将控件样式设置为
RightToLeft = RightToLeft.Yes
。或
设置
DrawMode = OwnerDrawFixed;
并挂钩DrawItem
事件,然后像
You could just set the control style to
RightToLeft = RightToLeft.Yes
if you don't mind the drop widget on the other side as well.or
set
DrawMode = OwnerDrawFixed;
and hook theDrawItem
event,then something like
在 WPF 中,这就像指定 ItemContainerStyle 一样简单。在 Windows 窗体中,这有点棘手。如果没有自定义绘图,您可以在 ComboBox 上设置 RightToLeft 属性,但不幸的是,这也会影响下拉按钮。
由于 Windows 窗体使用本机 ComboBox,并且 Windows 没有类似 的 ComboBox 样式ES_RIGHT 会影响文本对齐,我认为您唯一的选择是诉诸所有者绘制。从 ComboBox 派生一个类并添加 TextAlignment 属性或其他内容可能是个好主意。然后,只有当 TextAlignment 居中或右对齐时,您才会应用绘图。
In WPF this would be as easy as specifying an ItemContainerStyle. In Windows Forms it's a little trickier. Without custom drawing, you could set the RightToLeft property on the ComboBox but this would unfortunately also affect the drop down button.
Since Windows Forms uses a native ComboBox, and Windows doesn't have a ComboBox style like ES_RIGHT that affects the text alignment, I think your only option is to resort to owner draw. It would probably be a good idea to derive a class from ComboBox and add a TextAlignment property or something. Then you would only apply your drawing if TextAlignment was centered or right aligned.
您必须“DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed”并且
你自己的绘制方法是这样的。
You must "DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed" and
your own draw method like this.
不幸的是,如果组合框设置为 DropDownList,则 OwnDrawItem 的方法似乎不起作用。然后人们会看到白色背景而不是预期的灰色。因此,我使用了 WPF 组合框,它看起来也与“正常”组合框不完全相同,但足够接近。
对我来说,实现两个属性、一种方法和一个通风口就足够了,如下所示:
Unfortunately the approach with OwnDrawItem seems not to work if the combo box is set to DropDownList.Then one sees a white background instead of the expected gray. So I came with using a WPF combo box, with also does not look exactly as the "normal" combo box but is close enough.
For me it was sufficient to implement two properties, one method and one vent as follows: