【问题标题】:What kind of list control allows each item to have a different height?什么样的列表控件可以让每个项目有不同的高度?
【发布时间】:2014-03-02 02:22:07
【问题描述】:
我应该使用什么控件来显示项目列表?我希望能够调整项目的高度,以便它的高度可以比同一个项目控制列表中的另一个项目占用更多的空间。
我查看了列表框,但您无法调整项目的大小。我考虑过为占位符制作空白条目以将其分组为同一项目,但如果可能的话,我宁愿不这样做。
此控件的用途是表示从一天开始(顶部)到一天结束(底部)的时间块。
【问题讨论】:
-
可以用ListView设置所有行的高度;见stackoverflow.com/questions/6563863/…。要单独调整每个项目,您需要一个所有者绘制的 ListBox。
-
-
represent chunks of time from the beginning of the day (the top) to the end of the day (the bottom). - 您可能想使用当前相关的 .Net Windows UI 技术检查 My Example 的类似内容。
标签:
c#
winforms
visual-studio-2010
windows-forms-designer
【解决方案1】:
我查看了列表框,但您无法调整项目的大小
但你可以。将 DrawMode 属性设置为 OwnerDrawVariable。
您当然需要告诉它每个项目需要多高,这需要实现 MeasureItem 事件。当然,您需要绘制它,因此您使用 MeasureItem 填充您保留的空间,这需要实现 DrawItem 事件。您会在 MeasureItem 的 MSDN Library article 中找到一个很好的示例。
【解决方案2】:
自定义面板就像一个魅力:
public class AutoPanel : Panel
{
public AutoPanel()
{
AutoScroll = true;
}
private int _nextOffset = 0;
public int ItemMarginX = 5;
public int ItemMarginY = 5;
public void Add(Control child)
{
child.Location = new Point(ItemMarginX, _nextOffset);
_nextOffset += (child.Height + ItemMarginY);
Controls.Add(child);
}
}
将其添加到您的表单并像这样添加项目:
panel1.Add(new Button { Text = "smaller item", Height = 20 });
panel1.Add(new Button { Text = "medium item", Height = 23 });
panel1.Add(new Button { Text = "larger item", Height = 32 });