Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:23:08+00:00 2026-06-18T20:23:08+00:00

I need to get the values from my ComboBox es that reside in an

  • 0

I need to get the values from my ComboBoxes that reside in an ObservableCollection being iterated through by an ItemsControl. And I need to store them in a separate data structure, at least I think I do. Sounds pretty straightforward? I hope it is, I’ve been stuck on it all day.

Here’s my XAML:

<Window.Resources>
    <Style x:Key="IVCell" TargetType="{x:Type ItemsControl}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Variable.Name}"/>
                        <ComboBox x:Name="IVValues" ItemsSource="{Binding Values}"
                                  SelectedIndex="0"/>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

...
        <ItemsControl Name="IndependentVariables" Style="{StaticResource IVCell}" ItemsSource="{Binding IVCollection}"/>

where IVCollection is defined here, in my ViewModel:

public class MainViewModel : ViewModelBase
{
    public ObservableCollection<ExternalClass> IVCollection { get; set; }

    ...
}

Before you ask what ExternalClass is, know that that is what contains Variable and Values.

Here’s the problem. I need to get the SelectedIndex of ComboBox (so I would be changing the current code SelectedIndex="0"). And if my ExternalClass were what needed to contain those SelectedIndex values, that would be easy, since I already have access to its fields (Variable and Value). But I need those int values in

public class MainViewModel : ViewModelBase
{
    public ObservableCollection<int> SelectedIndices { get; set; }

    ...
}

or something like that. I had made a quick wrapper class to contain both SelectedIndices and IVCollection, but obviously that won’t work, because ItemsControl wants an ObservableCollection, not a class with two ObservableCollections.

And I guess the real meat of the question here (I could be wrong, that’s why I’m asking) is how do I access a property that’s outside of the scope of the Style/DataTemplate I’m currently in (e.g. IVCell)? I can do it in the code-behind with no problem, like so,

    private void IVValues_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        for (var i = 0; i < IndependentVariables.Items.Count; i++)
        {
            var uiElement = IndependentVariables.ItemContainerGenerator.ContainerFromIndex(i);
            var cBox = FindVisualChild<ComboBox>(uiElement); //Homebrew method
            //cBox.SelectedIndex
        }
    }

but once again, I’m trying to stay MVVM friendly. I’ve even tried using event commands with MVVM Light, etc., but that merely postpones the problem. I need to solve it. Or circumvent it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:23:09+00:00Added an answer on June 18, 2026 at 8:23 pm

    @sleiman and @Brandon
    Neither of your answers were 100%, but they were very helpful nonetheless. I reached my solution using elements from both of your answers, so I hesitate to accept one over the other.

    I used a combination of the wrapper class solution and the exposed property solution (along with a semi-unrelated but nonetheless necessary event-handling technique) to create the following:

    public class IVWrapper : INotifyPropertyChanged
    {
        public VariableValueList<double> IVCollection { get; set; }
        private int selectedIndex;
        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                selectedIndex = value;
                OnPropertyChanged("SelectedIndex");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
    
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    and

    public class MainViewModel : ViewModelBase
    {
    
        public ObservableCollection<IVWrapper> IVWrapperCollection { get; set; }
    
        ...
    
        private void InitializeIVs()
        {
            IVWrapperCollection.Clear();
            foreach (var iv in ...)
            {
                var toBeAdded = new IVWrapper {IVCollection = iv};
                toBeAdded.PropertyChanged += (sender, args) => Foo();
                IVWrapperCollection.Add(toBeAdded);
            }
        }
    
        public void Foo()
        {
            //Doin stuffs
        }
    }
    

    This additional integration with my ViewModel allows me to call a function whenever my selectionIndex changes. Neat! And clean!

    The corresponding changes in the XAML MainView:

                        <StackPanel>
                            <TextBlock Text="{Binding IVCollection.Variable.Name}"/>
                            <ComboBox ItemsSource="{Binding IVCollection.Values}"
                                      SelectedIndex="{Binding SelectedIndex}"/>
                        </StackPanel>
    
    ...
    
            <ItemsControl Name="IndependentVariables" Style="{StaticResource IVCell}" ItemsSource="{Binding IVWrapperCollection}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get in an action method the values from the selected checkboxes
In C# i need to get all values of a particular property from an
I need to get values of these check boxes with same name through HTTP
I have combobox with 2 values, ID and Name. I need to get ID
I have two combobox. I need get some value from first combobox1 after combobox1
I need to get the value from a checkbox which is not hidden. Here
I need to get the value from outside the form. My gsp looks like
I need to get the value from the parent form into the child; not
I need to get a value from another page. But I get this error
I need to get the biggest value from two fields: SELECT MAX(field1), MAX(field2) Now,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.