Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am populating a combobox in WPF like this

_cviewPreferencesPresenter.ListOfOVUnits();
SQL
cmbOVUnits.SelectedItem = _cstrSelectdOVUnit;
            _cstrDisplayedOVUnitID = cmbOVUnits.SelectedValue.ToString();


After the combobox loading i want to see the default value selected but i am not getting any value cmbOVUnits.SelectedItem is null.But the combobox is getting populated.

When i select some value in the combobox and click on apply button the value is not pertaining it is going to first value.

C#
public void PopulateOVList(List<KeyValuePair<string, string>> lstOVData)
    {
        try
        {
            cmbOVUnits.DataContext = lstOVData;
            cmbOVUnits.DisplayMemberPath = "Value";
            cmbOVUnits.SelectedValuePath = "Key";
        }
        catch (FlukeException)
        {
            throw;
        }
        catch (Exception excException)
        {
            if (FlukeConstants.IsLoggerEnabled)
            {
                Common.LogApplicationExceptionMsg(excException);
            }
            throw new FlukeException(SeverityLevel.Fatal, LogLevel.Debug, excException,
                    FlukeExceptionConstants.FLK_ERR_COMMON_APPLICATIONEXCEPTION);
        }
    }


here i can see cmbOVUnits cmbOVUnits = {System.Windows.Controls.ComboBox Items.Count:0} is '0', even i though i am getting values in lstOVData.The DataContext have the values like cmbOVUnits.DataContext = Count = 14.

So obviously cstrDisplayedOVUnitID value will be null.How can i solve this?
Posted
Updated 22-Sep-14 19:02pm
v3

1 solution

Your first and main problem is: you work with data as with strings. Populate semantic data, not just strings. If you use the data in such controls in any way, the item object should better be not strings.

Combo box's list items can be any objects. The other problem is: the items can be not selected. You can check up, for example, SelectedIndex. If it returns the value < 0, nothing is selected. You can use this property to select an item programmatically. It is more reliable, because, for example, from looking at you code one cannot be sure that _cstrSelectdOVUnit is a valid object for selection, because I don't know if you add this object to the lost or not.

Please see and read carefully: ^].

—SA
 
Share this answer
 
v2
Comments
chandra sekhar 23-Sep-14 1:02am    
I have updated the question.
Sergey Alexandrovich Kryukov 23-Sep-14 1:35am    
Nothing essentially new, but thank you. I already answered.
As to my concern of the correct selected object assigned, you did not answer: what is _cstrSelectdOVUnit?
The problem is: you are using different property "path" (Key and Value) and that makes SelectedItem and SelectedValue different objects. Is it intentional and do you take it into account?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900