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 8032065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:16:56+00:00 2026-06-05T01:16:56+00:00

I have a dataGrid, that uses an observableCollection to bind the ItemsSource property (MVVM

  • 0

I have a dataGrid, that uses an observableCollection to bind the ItemsSource property (MVVM pattern). So in my viewmodel I have a property that is an observableCollection (myCollection). However, this dataGrid can show information of two different types, that their are decide in runtime.

Normally, I use the the observableCollection in this away:

ObservableCollection<myType> myCollection = new ObservableCollection<myType>();

But now, I have a string as parameter that say me what type I need, so I would like to do something like that:

if(parameter == "TypeA")
{
    myCollection = new ObservableCollection<TypeA>();
}

if(parameter == "TypeB")
{
    myCollection = new ObservableCollection<TypeB>();
}

is it possible to do that?

  • 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-05T01:16:58+00:00Added an answer on June 5, 2026 at 1:16 am

    If you make TypeA and TypeB derived from a common base class or interface, you can keep the same collection.

    But if you want two different collections, you you can raise the collection property to notify about the changed type.

    IEnumerable MyCollection
    {
        get
        {
            if(CurrentType == typeof(TypeB)
                return myTypeBCollection;
            else if(CurrentType == typeof(TypeA)
                return myTypeACollection;
        }
    }
    

    So your bind MyCollection in your view to ItemsSource, and raise that the property has changed.

    Remember that you might need a different DataTemplate, where a DataTemplateSelector might come in handy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Silverlight 4 application that uses a DataGrid. This DataGrid is bound
I have a DataGrid that is setup like this: <DataGrid AutoGenerateColumns=True GridLinesVisibility=Horizontal IsReadOnly=True ItemsSource={Binding
I have a datagrid whose itemsSource is bound to a multiconverter which uses a
I have a working WPF dialog that uses DataGrid. The DataGrid is set to
I have a WPF app that uses DataGrid to display some data. When I
I have a datagrid that displays a list of custom objects. This list contains
I have a datagrid that I want to add a button/s to at runtime.
I have a DataGrid that needs to show a gradient background on mouseover of
I have a DataGrid that is showing some data via a PagedCollectionView with one
I have a datagrid that I have filled with data from a sql database.

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.