Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a list ( ItemsControl ) of questions. Each question has a a combo box, all combo boxes are identical, idea is to implement matching questions. What I want is to be able to bind selected item and selected question, so I know which combo box was used. I cant figure out how to do that with Combo Box as it doesnt take commands that could be used for other controls.

<ItemsControl ItemsSource="{Binding Path=Question.QuestionResponses}" Grid.Row="1">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <WebBrowser Margin="0,6,0,6"
                            Grid.Column="0"
                            Unloaded="WebBrowserUnload"
                            util:WebBrowserBehaviors.Body="{Binding QuestionResponse.AnswerText}"></WebBrowser>
                <ComboBox Margin="0,6,0,6"
                          Grid.Column="1"
                          ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext.Question.MatchesModel.Answers, Mode=OneWay}"
                          DisplayMemberPath="Answer">
                    <ComboBox.SelectedItem>
                        <MultiBinding Converter="{StaticResource MatchConverter}">
                            <Binding Path="."></Binding>
                            <Binding Path="."></Binding>
                        </MultiBinding>
                    </ComboBox.SelectedItem>
                </ComboBox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

In short, I want to pass two objects to my model/viewModel, so I can process selection. I've tried use converter to construct combined object, but I have no idea how to define target for <ComboBox.SelectedItem> when multi binding is used. Any help appreciated, I could not find anything online.

Edit I have a list of Combo Boxes, it can be 2 or 20 of them, depending on list size. Each combo box has same item source. I can bind selected item for each combo box, but I have no way to know which text box was used to select item. I need to know id of control that holds the combo box to identify it. I can get selected item and I can get object that holds combo box, what I can't do is to pass those 2 values to my model/viewmodel so I can bind selection with appropriate owner id. Content of combo boxes or their owner is completely irrelevant I just want to identity which combo box was used. I realize I can do this via code behind, but I prefer mvvm way, I could aslo replicate item source and contain control in it, that is a lot of duplicate data, but might make it easier to persist selected answers.

So, if I understand you correctly, you have a list of answers and a list of questions. You display the list of answers in the ItemsControl and want the user to select the right question for each answer? – grek40 Aug 9, 2017 at 11:13 Yep, more precisely, its question statements and users must select correct statement/answer from combo box. Problem is I have no idea how to bind it back using mvvm. I can bind a selection, but that's no good to me if I don't know which question/statement it belongs to. So I need a way to also pass back question object to get its ID – Aistis Taraskevicius Aug 9, 2017 at 11:17 Maybe you are looking for <Binding RelativeSource="{RelativeSource Self}"></Binding>, but I didnt quite get your question. – Rand Random Aug 9, 2017 at 11:20 Please re-explain. What do you expect to be initially displayed (how many items, what content per item) and what do you expect the user to do? – grek40 Aug 9, 2017 at 11:23

If anyone else get stuck on similar problem like this where you want selected statements to match selection from multiple combo boxes and you're wondering how to identity which combo box was used. I've overcame this by having a list of models for each combo box. This way you can store ID of the statement, it also allows to easier store selected items should you ever want to load selection from the database instead of only storing it.

  public class QuestionMatchesModel :BaseModel
    public ObservableCollection<MatchOptions> Answers { get; set; }
    private MatchOptions _selected;
    public MatchOptions Selected
        get => _selected;
            _selected = value;
            RaisePropertyChanged("Selection");
public class MatchOptions : BaseModel
    public long ResponseId { get; set; }
    public long MatchId { get; set; }
    public string Answer { get; set; }

Example Model that can be used, create a list out of this for each statement in your main model.

Lets suppose you have a list of items of type MatchItemViewModel and for each item, you have a description text SomeText and the user should select a matching question/answer/whatever from a combobox into property SelectedFromCombobox. The match items and the possible values to be displayed in the comboboxes are placed in a parent viewmodel ViewModel:

public class ViewModel
    private ObservableCollection<MatchItemViewModel> _ListItems = new ObservableCollection<MatchItemViewModel>();
    public ObservableCollection<MatchItemViewModel> ListItems
        get { return _ListItems; }
    private ObservableCollection<string> _ComboboxItems = new ObservableCollection<string>();
    public ObservableCollection<string> ComboboxItems
        get { return _ComboboxItems; }
public class MatchItemViewModel : INotifyPropertyChanged
    private string _SomeText;
    public string SomeText
        get { return _SomeText; }
        set { _SomeText = value; RaisePropertyChangedEvent(); }
    private string _SelectedFromCombobox;
    public string SelectedFromCombobox
        get { return _SelectedFromCombobox; }
        set { _SelectedFromCombobox = value; RaisePropertyChangedEvent(); }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChangedEvent([CallerMemberName]string prop = null)
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(prop));

For my tests, I initialized the data in my window constructor as follows:

public MainWindow()
    InitializeComponent();
    var vm = new ViewModel();
    vm.ListItems.Add(new MatchItemViewModel() { SomeText = "Red dogs are strange" });
    vm.ListItems.Add(new MatchItemViewModel() { SomeText = "I'm hungry" });
    vm.ComboboxItems.Add("What do you think of red dogs?");
    vm.ComboboxItems.Add("Current state of mind");
    grid1.DataContext = vm;

And this is the window content XAML:

<Grid x:Name="grid1" Margin="2">
    <Grid.Resources>
        <CollectionViewSource x:Key="ComboBoxItemsSource" Source="{Binding ComboboxItems}"/>
    </Grid.Resources>
    <ItemsControl ItemsSource="{Binding ListItems}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding SomeText}" MinWidth="100" VerticalAlignment="Center"/>
                    <ComboBox Margin="0,6,0,6"
                              Grid.Column="1"
                              ItemsSource="{Binding Source={StaticResource ComboBoxItemsSource}}"
                              IsSynchronizedWithCurrentItem="False"
                              SelectedItem="{Binding SelectedFromCombobox}">
                    </ComboBox>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

The important part is, in order to make the combobox items available to each list item, I create a CollectionViewSource named ComboBoxItemsSource outside the list and I reference this collection by {StaticResource ComboBoxItemsSource} in each list item. Also it's important to set IsSynchronizedWithCurrentItem="False" in the comboboxes, because otherwise all of them would always update to the same selected item.

How would you know which combo box was used? I need to associated selected answer with its owner. This will get me Selected item, for all combo boxes, but from what I see it still has no idea which one was it. – Aistis Taraskevicius Aug 9, 2017 at 11:44 @AistisTaraskevicius What do you mean which combobox was used? I think all comboboxes have the same content. The important part is: whenever a selection is made, the setter for SelectedFromCombobox of one specific MatchItemViewModel is called and it is linked to exactly this one specific item with a specific SomeText. If you can't identify your items from that much information, then you have a completely different problem. – grek40 Aug 9, 2017 at 11:47 Imagine I have a picture that has a human head, number one shows nose, number 2 shows eyes , number 3 shows mouth and so on. Then you have A question which ask you to link statements with the numbers. The head is and user is expected toselct number, mouth is , eyes are . So I need to know which selection belongs to which selection. – Aistis Taraskevicius Aug 9, 2017 at 11:49 @AistisTaraskevicius So you would have some picture (presumably outside the list, since you never mentioned it before) and then you have the following ListItems: { SomeText = "mouth is: "}, { SomeText = "eyes are: "}, { SomeText = "nose is: "} and you have the following ComboboxItems: "1", "2", "3" - you may want to store something like CorrectAnswer for each MatchItemViewModel so you can compare it to the selected answer. – grek40 Aug 9, 2017 at 12:45 I'll try to have a go and just have individual item sources (combo box item list) for combo box which also includes statement id, some data will be repeated, but its not much and this approach will hopefully, make it easier to store and retrieve selections too. – Aistis Taraskevicius Aug 9, 2017 at 12:58

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.