'Set a list of static properties of my class as DataSource of ComboBox

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();


Solution 1:[1]

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);

Solution 2:[2]

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 => new PaperSize(210,297);
   public static PaperSize A5 => new PaperSize(148,210);
   public static PaperSize A6Rotated => new PaperSize(148,105);
   public static PaperSize JapaneseDoublePostcardRotated => new PaperSize(648,917);
   public static PaperSize JapanesePostcardRotated => new PaperSize(31,44);
   public static PaperSize B5JisRotated => 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;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Glenn
Solution 2