0

I got a combo box in wpf form I set the ItemSource to the collection of Dictionary of (Pet Type) and just display the Value and hid the Key

public void BindComboBoxes()
{
    this.cboTypes.ItemsSource = new BindingSource(CommonMgr.GetPetTypesDropDown(false), null);
    this.cboTypes.DisplayMemberPath = "Value";
    this.cboTypes.SelectedValuePath = "Key";
}

Then whenever I type to encode a new Breed Object, and type a text in the cboTypes of something that doesn't exist in its items(not in the db), my program will ask if the end user wants to add that new PetType in the db, if yes, then it will do so.

Then i update the cboTypes using the BindComboBoxes method again, set the cboTypes.Text into the new item and assign the Key to the designated field, but the problem is, it says, it was null. it worked fine in the windows form though. Here's my code:

public Breed GetPageEntity()
{
    Breed setEntity = new Breed();
    bool doesExist = false;
    setEntity.Id = DefaultValue.GetInt(this.txtId.Text);
    setEntity.BreedName = DefaultValue.GetString(this.txtName.Text);


    try
    {
        setEntity.PetTypeId = DefaultValue.GetInt(this.cboTypes.SelectedValue.ToString());
    }
    catch (Exception)
    {
        var addAnother = MessageBox.Show(String.Format("{0}: This type is not in the database. \nAdd {0} to the database?",
            this.cboTypes.Text), "Pet Type Cannot Be Found", MessageBoxButtons.OKCancel);

        if (addAnother == System.Windows.Forms.DialogResult.OK)
        {
            petTypeMgr.Entity = this.PetTypeAdder(cboTypes.Text);
            string temp = this.cboTypes.Text;
            petTypeMgr.Insert((petTypeMgr.Entity), fUser.Entity.Id, ref doesExist);
            //cboTypes.ItemsSource = null;
            //cboTypes.Items.Clear();
            BindComboBoxes();
            cboTypes.Text = temp;

            //SelectedValue became null
            setEntity.PetTypeId = DefaultValue.GetInt(this.cboTypes.SelectedValue);
        }

    }

    setEntity.Description = DefaultValue.GetString(this.txtDescription.Text);
    setEntity.SortOrder = DefaultValue.GetInt(txtSortOrder.Text);
    setEntity.StatusId = true;
    return setEntity;
}
2
  • cboTypes.SelectedValue is null, because you are resetting the binding with BindComboBoxes(); and aren't setting the SelectedValue again. cboTypes.Text does not set anything but what text you see. Commented Jun 26, 2014 at 22:51
  • you might be right. i just did a roundabout for that, i programmed to search the value of temp in the combobox and set the SelectedValue to the number of that temp. Commented Jun 27, 2014 at 18:30

2 Answers 2

1

You'll find it much easier if you data bind to properties in the code behind:

// Implement INotifyPropertyChanged interface properly here

private Dictionary<string, Pet> yourProperty = new Dictionary<string, Pet>();

public Dictionary<string, Pet> YourProperty
{
    get { return yourProperty; }
    set
    {
        yourProperty = value;
        NotifyPropertyChanged("YourProperty");
    }
}

private KeyValuePair<string, int> yourSelectedProperty;

public KeyValuePair<string, int> YourSelectedProperty
{
    get { return yourSelectedProperty; }
    set
    {
        yourSelectedProperty = value;
        NotifyPropertyChanged("YourSelectedProperty");
    }
}

Then in the XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <ComboBox ItemsSource="{Binding YourProperty}" DisplayMemberPath="Value"
    SelectedValuePath="Key" SelectedItem="{Binding YourSelectedProperty}" />
    <TextBlock Grid.Column="1" Text="{Binding YourSelectedProperty.Key}" />
</Grid>

You only need to set your ItemsSource once like this. Once it is data bound to a collection property, you can just make changes to the collection and they will update in the UI automatically. So, assuming that your GetPetTypesDropDown method returns the correct type, you should be able to update the ComboBox items like this:

YourProperty = CommonMgr.GetPetTypesDropDown(false);

Alternatively, you could equally do something like this to update it:

YourProperty = new Dictionary<string, int>();
foreach (YourDataType dataType in CommonMgr.GetPetTypesDropDown(false))
{
    YourProperty.Add(dataType.Key, dataType.Value);
}
Sign up to request clarification or add additional context in comments.

3 Comments

what's the NotifyPropertyChanged? it turned red when i copied it.
Read the answer: Implement INotifyPropertyChanged interface properly here.
aww... missed it under my nose ^_^
0

Why don't you just bind Breed to the Combobox?

In the Breed class override the ToString() method so that the box shows what you want it to.

class Breeds
{
 //Variables
 public void override ToString()
 {
   return Breedname;
 }
}

Set the combobox

List<Breeds> breedlist = new List<Breeds>();
this.cboTypes.ItemsSource = breedlist;

Read the combobox

if(cboTypes.SelectedItem != null)
{
 Breeds breed = (Breeds)cboTypes.SelectedItem;
//Do stuff
}
else
{
 //Create new breed
}

2 Comments

my bad, sorry, u misunderstood my Object. this is Breed object BreedId, BreedName, petTypeId, PetTypeName, Description, (i was referring to PetType object) so example I add these Id = (auto generate), BreedName = Rabbit, PetTypeId = (cannot be seen), PetTypeName (cboTypes) = Rodent, Description = The cousin of Bugs Bunny, ... if Rodent is not in the database, a message box will say that it doesn't and asks if I wanted to add it to the PetTypes Table. If Yes, it will be added, but the last line of that Catch block says the SelectedValue = null; so the catch throws another exception.
My mistake. Sorry, I put a comment on your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.