Reading selected values of each combox within the itemscontrol

Hi all,

i've a itemscontrol that is boud to ADO.Net dataset and i'm using a combobox to display the contents of that dataset. on SelectionChanged event, i need to access the selected value of each combobox within that controls. can any one help me i'm stuck up, here is my code:

<ItemsControl Grid.Row="1" Grid.Column="0" Margin="20,0,20,20" Name="ic" ItemTemplate="{StaticResource BookItemTemplate}" ItemsSource="{Binding}" />

<DataTemplate x:Key="BookItemTemplate">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="200"/>

<ColumnDefinition Width="80"/>

<ColumnDefinition Width="100"/>

<ColumnDefinition/>

</Grid.ColumnDefinitions>

<TextBlock Text="{Binding Path=Object}" Grid.Column="0" FontWeight="SemiBold" />

<ComboBox Name="cboLevel" Grid.Column="1" Width="40" MinWidth="80" SelectionChanged="LevelChanged" >

<ComboBoxItem Content="1" />

<ComboBoxItem Content="Unsorted" />

</ComboBox>

<TextBlock Text="{Binding Path=Source}" Grid.Column="2" Margin="0,0,0,10"/>

</Grid>

</DataTemplate>

thanks




Answer this question

Reading selected values of each combox within the itemscontrol

  • yanivpinhas

    thanks for your reply.

    i might not be clear enough about my problem.

    the syntax you sent to me may retreive the value from a single combo: while my problem is that:

    let say if i've 3 rows in my datatable there will be 3 different comboboxes visible to user due to my datatemplate and on selectionchange from any combobox item i need to retreive the values from all three combobox not from only the current combobox.

    i could not find any property of itemscollection that know how many combobox is created.

    so if you can help me find out a loop that can iterate through each combobox it will be great, thanks.



  • perstam

    i can extend that code if i the name propertry are fixed like u said dropdownlist1, dropdownlist2 and dropdownlist3. but problem is that thes combobox is part of itemscontrol and that itemscontrol is bound to the database. i m using the template to generate combobox for each row in database. so if there are 3 rows it will have 3 combobox and if it has 1000 rows it will have 1000 combobox. i need to know how can i access each combobox within that collections. if i use items property of itemscontrol then it gives me the datarow not the combobox.

    i need to access collections of combobox within itemscontrol

    thanks



  • Dvlnblk

    Hi ASaleem,

    On thing that I have done in order to retrieve the selected value(s) is by utilizing the "Tag" property within the ComboBoxItem (http://msdn2.microsoft.com/en-us/library/system.windows.frameworkelement.tag.aspx).

    Then, I have been able to access the value like so:

    ((ComboBoxItem)(DropDownList.SelectedItem)).Tag;

    I hope this post helps, if it does, please select "Mark as answer", if not, please post a follow up question and I will be more than willing to look at it to see if I can help further.

    Chad Campbell



  • Giugio

    thanks smith, i had work out the solution myself in the afternoon but your help is very much appreciated. thanks



  • RavensAngel

    ASaleem, how did you figure out this problem I'm having the same problem. I have an ItemsControl and I've got CheckBox'es in the DataTemplate. I need to access the CheckBox values from the code.

    ItemsControl's Items Property only returned the object of type, XmlNode since I'm binding it from an Xml source. Can you tell me what your solution was

    Thanks



  • Joe Au

    Hi ASaleem,

    I am slightly confused by your statement. You state that there will be 3 different comboboxes available to the user, and when one combo box changes, you need to get the selected values from each combo box. Is this correct If so, then what you need to do is extend the example code I have given you in a manner as such (this assumes that the value you have bound the "Tag" property to is a string):

    string selectedValue1 = Convert.ToString(((ComboBoxItem)(DropDownList1.SelectedItem)).Tag);

    string selectedValue2 = Convert.ToString(((ComboBoxItem)(DropDownList2.SelectedItem)).Tag);

    string selectedValue3 = Convert.ToString(((ComboBoxItem)(DropDownList3.SelectedItem)).Tag);

    However, if you are looking for an "ItemsCollection", There is the "Items" property, which allows you to access the combo box items within the ComboBox. However, because you are using binding, you probably need to utilize the "SelectedValue" and the "DataContext" properties. I would recommend reviewing these topics, which the information is available here:

    If this adequately answers you question, please selected "Mark as Answer", otherwise, please post a followup question.

    Thanks,

    Chad Campbell



  • Le Saint

    Go to the second page of this thread http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=776130&SiteID=1&PageID=1 and look for the post from "footballism" which was marked as the answer (it has a green checkmark). That will show you how to access template-generated controls, such as each combobox in your itemscontrol.

    HTH



  • Reading selected values of each combox within the itemscontrol