简体   繁体   中英

how to add all days of month into a Combo Box in C#

hello, i want to add all days of a month into a combo box and i do not know how to do it.
this is the xaml combobox code section:

                        <ComboBox.ItemTemplate>

                            <DataTemplate>

                                <TextBlock Text="{Binding dt}"/>

                            </DataTemplate>
                            
                        </ComboBox.ItemTemplate>
                    </ComboBox>

and this is the C# code to fill it:

        List<datepick> datepicks = new List<datepick>();

        
        datepicks.Add(new datepick() { dt = DateTime.Now.ToLongDateString() });
        
        

        cob_date_select.ItemsSource = datepicks;

You can use linq to get the dates like

private void BindDatesToComboBox()
        {
            List<string> datesOfMonth = new List<string>();
            datesOfMonth.AddRange(Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month))  // Days: 1, 2 ... 31 etc.
                    .Select(day => new DateTime(DateTime.Now.Year, DateTime.Now.Month, day).ToLongDateString()) // Map each day to a date
                    .ToList()); // Load dates into a list

            cob_date_select.ItemsSource = datesOfMonth;
        }

and then simply reduce the xaml code to

<ComboBox x:Name="cob_date_select" FontSize="20" Height="30">
        </ComboBox>

Hi you can achieve that by using the following code in the codebehind file:

There is a method that takes the year and a month as input and returns a list of dates.

    public MainWindow()
    {
        InitializeComponent();

        var dates = GetDates(2020, 12);

        cob_date_select.ItemsSource = dates;
        cob_date_select.DisplayMemberPath = "Value";
        cob_date_select.SelectedValuePath = "Id";
        cob_date_select.SelectedValue = "2"; // Second day of month
    }

    public static List<ComboDate> GetDates(int year, int month)
    {
        var dates = new List<ComboDate>();

        // Loop from the first day of the month until we hit the next month, moving forward a day at a time
        for (var date = new DateTime(year, month, 1); date.Month == month; date = date.AddDays(1))
        {
            dates.Add(new ComboDate { Id = date.Day, Value = date.Day });
        }

        return dates;
    }

    public class ComboDate
    {
        public int Id { get; set; }
        public int Value { get; set; }
    }

You have to generate a collection to feed combo box. It's worth to mention that UI part needs representation of the date (you want to use ToLongDateString() which uses application current culture), but from other side probably you will need to manipulate on selected date (using string representation will be cumbersome).

To have string representation of the DateTime and easy way of gathering object I made a wrapper.

<!-- XAML; DataContext is set to the instance of ExampleViewModel class -->
<ComboBox ItemsSource="{Binding Days}"/>
// ExampleViewModel.cs
public class ExampleViewModel
{
    public IList<UiDateTimeWrapper> Days { get; set; }

    public ExampleViewModel()
    {
        this.Days = Enumerable.Range(1, DateTime.DaysInMonth(2020, 12)).Select(x =>
            new UiDateTimeWrapper { 
                Date = new DateTime(2020, 12, x) 
            }).ToList();
    }
}
// UiDateTimeWrapper.cs
public class UiDateTimeWrapper
{
    public DateTime Date { get; set; }

    public override string ToString()
    {
        return Date.ToLongDateString();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM