Home > Software design >  How to bind class properties into combox and select items
How to bind class properties into combox and select items

Time:01-26

I need to bind ComboBox to class properties and pass its selected values when button click.

my class as follows

public sealed class PaperSize
{
    public PaperSize(Length width, Length height);

    public static PaperSize A4 { get; }
    public static PaperSize A5 { get; }
    public static PaperSize A6Rotated { get; }
    public static PaperSize JapaneseDoublePostcardRotated { get; }
    public static PaperSize JapanesePostcardRotated { get; }
    public static PaperSize B5JisRotated { get; }
    public string Width { get; }
    public string Height { get; }
}

I tried following code snippet to bind properties into Combobox.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        comboBox1.DataSource = new BindingList<PropertyInfo>(typeof(PaperSize).GetProperties());
    }

    private void btnDownload_Click(object sender, EventArgs e)
    {
        string html = "test";
        var pdf = Pdf.From(html).OfSize(PaperSize.A4Transverse).WithOutline().Portrait().Content();
    }
}

My requirement is, when I click the download button, I need to set Papersize from selected element from the combobox to this code line. Currently I set value as .OfSize(PaperSize.A4). instead of that I need to set combo box selected value. how can I do it?

var pdf = Pdf.From(html).OfSize(PaperSize.A4).WithOutline().Portrait().Content();

CodePudding user response:

You will need to expose the options as a collection to perform the model binding. Consider adding this to your class:

// store a name for each PaperSize that can be bound to in the UI
public string Name { get; set; }

// expose a static collection that can be bound to as the datasource
public static IEnumerable<PaperSize> StaticPaperSizes
{
    get
    {
        return new PaperSize[] { A4, A5, ... };
    }
}

Then, ComboBox.SelectedItem could simply contain the name of the PaperSize. You can use a LINQ query to select the size:

string selectedSize = combobox.SelectedItem as string;
var size = PaperSize.StaticPaperSizes.SingleOrDefault(s => s.Name == selectedSize);

CodePudding user response:

Assuming you have a class like this:

public sealed class PaperSize
{
    public PaperSize(int width, int height)
    {
        Width = width;
        Height = height;
    }
    public static PaperSize A4 { get; } = new PaperSize(210, 297);
    public static PaperSize A5 { get; } = new PaperSize(148, 210);
    public static PaperSize A6Rotated { get; } = new PaperSize(148, 105);
    public static PaperSize JapaneseDoublePostcardRotated { get; } = new PaperSize(648, 917);
    public static PaperSize JapanesePostcardRotated { get; } = new PaperSize(31, 44);
    public static PaperSize B5JisRotated { get; } = new PaperSize(105, 148);
    public int Width { get; }
    public int Height { get; }
}

The you can load the combo box like this:

comboBox1.DataSource = typeof(PaperSize).GetProperties(
     System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
    .Where(x => x.PropertyType == typeof(PaperSize))
    .Select(x => new { Name = x.Name , Value = x.GetValue(null) }).ToList();
comboBox1.DisplayMember= "Name";
comboBox1.ValueMember = "Value";

And it will show the items like this:

enter image description here

And later get the value like this:

PaperSize size = (PaperSize)comboBox1.SelectedValue;
  •  Tags:  
  • Related