简体   繁体   中英

How can I set my comboBox values to a default value that they contain?

I've tried both of these:

comboBoxToHour.SelectedValue = comboBoxToHour.Items.IndexOf("11");
comboBoxToHour.SelectedIndex = comboBoxToHour.Items.IndexOf("11");

...and neither one works and, in fact, neither does this:

comboBoxFromHour.SelectedValue = "11";

...nor this:

comboBoxFromHour.SelectedIndex = 12;

...which I find really odd.

UPDATE

I tried Ross' suggested fix, but with this:

if (App.roamingSettings.Values.ContainsKey("FromMonthSection1"))
{
    if (comboBoxFromMonth.Items != null)
    {
        var item = comboBoxFromMonth.Items.FirstOrDefault(p => (p as ComboBoxItem).Content.ToString() == App.roamingSettings.Values["FromMonthSection1"].ToString());

...I get, "Object reference not set to an instance of an object." on the last line.

UPDATE 2

As it turns out, a simple way works after all. It turns out that this:

if (App.roamingSettings.Values.ContainsKey("FromMonthSection1"))
{
    if (comboBoxFromMonth != null)
    {
        var item = comboBoxFromMonth.Items.FirstOrDefault(p => (p as ComboBoxItem).Content.ToString() == App.roamingSettings.Values["FromMonthSection1"].ToString());
        if (item != null)
        {
            comboBoxFromMonth.SelectedItem = item;
        }
    }
}

...fails with, " System.NullReferenceException was unhandled by user code HResult=-2147467261 Message=Object reference not set to an instance of an object. "

...on the line assigning to item.

However, this:

if ((App.roamingSettings.Values.ContainsKey("FromMonthSection1")) &&
    (!string.IsNullOrWhiteSpace(App.roamingSettings.Values["FromMonthSection1"].ToString())))
{
    comboBoxFromMonth.SelectedItem = App.roamingSettings.Values["FromMonthSection1"].ToString();
}

...works.

Perhaps you want to use FindString to workout the index:

comboBoxToHour.FindString("Name of a item")

eg

comboBoxToHour.DisplayMember = "Name";
comboBoxToHour.ValueMember = "Value";
comboBoxToHour.DataSource = aList; 
comboBoxToHour.SelectedIndex = comboBoxToHour.FindString("Name of a item")

or since I've bound the combobox to a datasource I can use the SelectedValue:

comboBoxToHour.SelectedValue = "5";

It looks like you are not using a viewmodel so the .Items is a list of ComboBoxItem's. You are asking basically to compare a comboboxitem to a string, which it can't find, so it's selected the selected index to -1;

This code can achieve what you would like:-

var item = comboBoxToHour.Items.FirstOrDefault(p => (p as ComboBoxItem).Content.ToString() == "4");
if (item != null)
{
    comboBoxToHour.SelectedItem = item;
}

Hopefully that will help.

To display first item of combobox :

List ModeOptions=new List() {"--Select Mode--","Driving","Walking","Transit"};

comboBoxOptions.ItemsSource = ModeOptions;

comboBoxOptions.SelectedItem = ModeOptions[0];

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