locked
WPF ComboBox binding value display issue RRS feed

  • Question

  • I have a combobox bind to a bit field
    <ComboBox Name="cboHasHeader" SelectedIndex="{Binding Path=HasHeader}" >
         <ComboBoxItem>No</ComboBoxItem>
          <ComboBoxItem>Yes</ComboBoxItem>
    </ComboBox>
    If i use SelectedIndex like above, it will show the combobox with selectedItem correctly.

    But what if i have an integer field with 1,2,3,.....
    If it is 1, i want to show "Excel", 2 show "Word", 3 show "Powerpoint" and so on.
    Please help. Thanks.
    Wednesday, August 12, 2009 4:03 PM

Answers

  • Hi dragonz1,
    You can bind your result from the database instead of the list I sent you. It will be very similar.

    Probably you are getting some DataTable so you can do:

    combobox.ItemsSource = datatable;

    and then with the DisplayMemeberPath to the name of the column you want. the SelectedItem will return you a DataRow.

    Regards,
    Federico Benitez
    My blog
    • Marked as answer by dragonz1 Thursday, August 13, 2009 2:46 PM
    Wednesday, August 12, 2009 6:22 PM

All replies

  • Hi dragonz1,
    I dont understand exactly what you want to do.

    But if you want to be able to set values to the ComboBoxItems you can use the Tag property that allows you to store any object that you want. Then you just do cboHasHeader.SelectedItem.Tag and you get the object that you stored.

    Please let me know if this was what you were trying to do.

    Regards,
    Federico Benitez
    My blog
    Wednesday, August 12, 2009 4:16 PM
  • Hi Federico,
    Yes, i'd like to set values to my ComboBoxItems. Let's say if my SourceType has a value of '2', i want the combobox to show "Word" instead of 2.
    I know i shouldn't use SelectedIndex. Are you suggesting put a Tag like below?

    <ComboBox Name="cboType" SelectedIndex="{Binding Path=SourceType}" >
         <ComboBoxItem Tag="1">Excel</ComboBoxItem>
          <ComboBoxItem Tag="2">Word</ComboBoxItem>
    <ComboBoxItem Tag="3">Powerpoint</ComboBoxItem>
    </ComboBox>

    Thanks.
    Wednesday, August 12, 2009 4:36 PM
  • Hi dragonz1,
    Yes you can do that, or store the source object in the Tag so you can access to more properties. It depends what you need...

    Regards,
    Federico Benitez
    My blog
    Wednesday, August 12, 2009 4:42 PM
  • But i tried with SelectedItem="{Binding Path=SourceType}" and SelectedValue="{Binding Path=SourceType}"
    and my SourceType has a value of '2', it will not show anything in the combobox.
    Wednesday, August 12, 2009 4:53 PM
  • I'm confused with your problem.


    why you shouldn't use SelectIndex?


    In your Combobox you add a list of string, right? Or, what kind of object do you send to the combobox?

    If you use another type, you need to set the DisplayMemberPath, that define the property in your object that you want see in combobox.

    But,i think....if you have


    1-> Excel
    2 -> Word
    3-> PowerPoint


    this is like an enumeratio? right?

    try to create a Convert that implements IValueConverter and look for "WPF combobox enumeration" and you can find a lot of examples.


    Or, try to use the SelectedValuePath



    Wednesday, August 12, 2009 5:14 PM
  • Hi,
    Now I think I understood what you want. The easiest thing to do for you is to add a second property to your SourceType class.

    public SourceType
    {
       public int IntValue{get; set;}
       public string StringValue{get; set;}
    }

    Then you create a List with all your SourceTypes like this:

    List<SourceType> list = new List<SourceType>()
    {
       new SourceType(){ IntValue=1, StringValue="Excel"},
       new SourceType(){ IntValue=2, StringValue="Word"}
    }

    Then you set the list to the combobox ItemsSource

    comboBox.ItemsSource = list;

    After that you specify the DisplayMemberPath to the property you want to bind:

    <

     

    ComboBox Grid.Row="1" x:Name="comboBox" DisplayMemberPath="StringValue">

     

     

     

    </ComboBox>

    And then with the SelectedItem you will get the SourceType object.

    Hope it helps.

    Regards,

    combobox.ItemsSource = list;

    and in the bindings you put:


    Federico Benitez
    My blog
    Wednesday, August 12, 2009 5:18 PM
  • If i use SelectedIndex, when my value from database is 2, it will show "Powerpoint" instead of "Word", which is incorrect.
    My data column is an integer type. It is ok to hardcoded the combobox items. I thought this should be simple to set Item name and value.
    But it took me so long. :(
    Wednesday, August 12, 2009 6:19 PM
  • Hi dragonz1,
    You can bind your result from the database instead of the list I sent you. It will be very similar.

    Probably you are getting some DataTable so you can do:

    combobox.ItemsSource = datatable;

    and then with the DisplayMemeberPath to the name of the column you want. the SelectedItem will return you a DataRow.

    Regards,
    Federico Benitez
    My blog
    • Marked as answer by dragonz1 Thursday, August 13, 2009 2:46 PM
    Wednesday, August 12, 2009 6:22 PM
  • Thanks Federico for your reply, what if i don't have the description column in my database.
    The database column SourceType only has integer number(1,2,3....). 
     
    I populate my data with BindingListCollectionView,
    set this.DataContext = linqQuery;

    if i do textbox, i can see the value = '2'
    <TextBox Text="{Binding Path=SourceType}"/>


    Wednesday, August 12, 2009 7:00 PM
  • From where are you getting the "Excel", "Word", etc values?
    Federico Benitez
    My blog
    Wednesday, August 12, 2009 7:26 PM
  • Those values are going to be hardcoded in my combobox. Like,

    <ComboBox Name="cboType">
         <ComboBoxItem>Excel</ComboBoxItem>
          <ComboBoxItem>Word</ComboBoxItem>
    <ComboBoxItem>Powerpoint</ComboBoxItem>
    </ComboBox>

    just don't know how to bind it to my SourceType integer field.
    • Proposed as answer by markmnl Monday, October 26, 2009 8:00 AM
    • Unproposed as answer by markmnl Friday, May 11, 2012 5:14 AM
    Wednesday, August 12, 2009 7:40 PM
  • Ahhh. As far as I know you cannot bind something if you define it manually in the XAML.
    The easiest way is that you create another table in the DB or another field and you bring them together from there. Or you make a join in memory with LinQ or DataTables...

    Regards,
    Federico Benitez
    My blog
    Wednesday, August 12, 2009 7:43 PM
  • Thanks Federico, I got my problem solved.
    I created a table to hold the itemsSource. Or should i create the enum?
    Thursday, August 13, 2009 2:48 PM
  • Hi dragonz1,
    I think a table is OK. If you are going to use those values in other places and you have a class where you put all your enums you can do it as an enum. Its a matter of preference and depends on how you are dealing with your solution.

    Regards,
    Federico Benitez
    My blog
    Thursday, August 13, 2009 3:03 PM
  • Simply set SelectedValuePath attribute to "Content":

    <ComboBox Name="cboType" SelectedValuePath="Content" ...>
         <ComboBoxItem>Excel</ComboBoxItem>
          <ComboBoxItem>Word</ComboBoxItem>
    <ComboBoxItem>Powerpoint</ComboBoxItem>
    </ComboBox>

    By not specifying the SelectedValuePath you were attempting to bind to the entire ComboBoxItem object for which no conversion exists in which case the binding engine fails silently. Alternatively you could write your own convertor which would convert the ComboBoxItem you selected to the appropriate value and type for the object you are binding to, see how here: http://msdn.microsoft.com/en-gb/library/ms752091.aspx.
    • Proposed as answer by markmnl Monday, October 26, 2009 8:16 AM
    Monday, October 26, 2009 8:16 AM