winforms 在Windows窗体中创建自定义列表

35g0bw71  于 2023-01-14  发布在  Windows
关注(0)|答案(1)|浏览(118)

我正在尝试创建一个界面,其中一个表中的行可以以类似的方式查看(从电源应用程序)。有人知道一种方法,这可以在WinForms中实现?也许有一些自定义控件,我可以下载并添加到该项目?
理论上我可以一个一个的示例化控件,但是这需要一段时间/很难使其可滚动(或者至少我不知道怎么做)。谢谢你的建议。

nzrxty8p

nzrxty8p1#

对于一些轻量级的东西,使用流布局面板在可滚动列表中显示绑定表是非常容易的。下面显示了一个快速的概念验证。

  • 对于生产用途或大型表格,至少有三家大公司生产这种性质的复杂自定义WinForms组件,并拥有免费试用许可证。IMO的人“可能”想要权衡收益与成本。*

class CustomFlowLayoutTable : FlowLayoutPanel
{
    public CustomFlowLayoutTable()
    {
        AutoScroll = true;
        Products.ListChanged += (sender, e) =>
        {
            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    Controls.Add(Products[e.NewIndex]);
                    break;
                default:
                    break;
            }
        };
    }
    public BindingList<ProductCard> Products = new BindingList<ProductCard>();
}

产品卡

public partial class ProductCard : UserControl
{
    int _id = 0;
    public ProductCard()
    {
        InitializeComponent();
        Name = $"userControl{_id++}";  // No space, start with lowercase
        Padding = new Padding(0);
        Margin = new Padding(2);
        pictureBox.Padding = new Padding(8);
        labelExp.Click += (sender, e) => MessageBox.Show($"{this}");
        Name = $"userControl{_id++}";  // No space, start with lowercase
    }
    public string Description
    {
        get => labelDescription.Text;
        set
        {
            if (!Equals(labelDescription.Text, value))
            {
                labelDescription.Text = value;
                var imagePath = Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    "Images",
                    $"{Description}.png"
                );
                // Set the "Copy to Output Directory" property of all image files.
                if (File.Exists(imagePath))
                {
                    pictureBox.Image = Image.FromFile(imagePath);
                }
            }
        }
    }
    public string Category
    {
        get => labelCategory.Text;
        set => labelCategory.Text = value;
    }
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        Width = getWidth();
        // Respond to width changes of owner container.
        Parent.SizeChanged += (sender, e) => Width = getWidth();
    }
    int VSBW { get; } = SystemInformation.VerticalScrollBarWidth;

    private int getWidth() =>
        Parent.Width -
            Parent.Padding.Left -
            Parent.Padding.Right -
            Margin.Left -
            Margin.Right -
            VSBW;
    public override string ToString() =>
        $"{Category}{Environment.NewLine}{Description}";
}

主窗体

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        flowLayoutPanel.Products.Add(new ProductCard
        {
            Category = "Carpet",
            Description = "Caserta Stone Beige",
        });
        flowLayoutPanel.Products.Add(new ProductCard
        {
            Category = "Carpet",
            Description = "Caserta Sky Grey",
        });
        flowLayoutPanel.Products.Add(new ProductCard
        {
            Category = "Carpet",
            Description = "Ageless Beauty Clay",
        });
        flowLayoutPanel.Products.Add(new ProductCard
        {
            Category = "Carpet",
            Description = "Lush II Tundra",
        });
        flowLayoutPanel.Products.Add(new ProductCard
        {
            Category = "Carpet",
            Description = "Lush II Frosty Glade",
        });
        flowLayoutPanel.Products.Add(new ProductCard
        {
            Category = "Hardwood",
            Description = "Bolivian Rosewood",
        });
    }
}

相关问题