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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:40:18+00:00 2026-06-17T16:40:18+00:00

I would like to assign datacontext and static values in a single Combobox .

  • 0

I would like to assign datacontext and static values in a single Combobox. I tried like this,

this.courseList.DataContext = ldc.Courses.OrderBy(x => x.CourseID);
this.courseList.Items.Add("All");
this.courseList.Items.Add("Others");

But, it shows InvalidOperationException was Unhandeled that Items collection must be empty before using ItemsSource.

Then I tried like this,

this.courseList.Items.Add("All");       
foreach (var items in ldc.Courses.OrderBy(x => x.CourseID))
 {
   this.courseList.Items.Add(items);
 }
this.courseList.Items.Add("Others");       

It works like this Image
enter image description here
Since I used an ItemTemplate in XAML design for this Combobox so it doesn’t shows exactly in my way for this two value All and Others.

Here is the ItemTemplate

    <DataTemplate x:Key="CourseTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=CourseID}"/>
            <TextBlock Text=" : "/>
            <TextBlock Text="{Binding Path=CourseName}"/>
        </StackPanel>
    </DataTemplate>

...
 <ComboBox Name="courseList" VerticalAlignment="Top" SelectionChanged="courseList_SelectionChanged" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource CourseTemplate}"/>

I want it to view according to DataTemplate for database entries and simple strings All and Others for All and Others respectively like the image below.
enter image description here

Any suggestions. Thank you.

  • 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-17T16:40:19+00:00Added an answer on June 17, 2026 at 4:40 pm

    As the error says, you can only use ItemsSource or Items directly, not a mix of the two. Since you essentially have two different types of data that you want to show differently you can add them all to a single bound collection and then let the templates do the work of rendering them differently. Step 1 is to build the single collection:

    this.courseList.ItemsSource = new object[] { "All" }.Concat(ldc.Courses.OrderBy(x => x.CourseID)).Concat(new[] { "Others" });
    

    You now have 2 types of data in the collection: String and your object which I’ll assume is Course. The strings will show up as you want without any extra work but you need to apply the DataTemplate only to the Course objects. The simplest way is just to replace your x:Key with a DataType:

    <DataTemplate DataType="{x:Type data:Course}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=CourseID}"/>
            <TextBlock Text=" : "/>
            <TextBlock Text="{Binding Path=CourseName}"/>
        </StackPanel>
    </DataTemplate>
    

    where the data xmlns corresponds to your Course object’s namespace.

    Next just remove the ItemTemplate attribute from your ComboBox and you should get what you’re looking for.

    If you have other places in the same scope where you’re also binding Course objects but want them to display differently you can either scope the DataType template more locally (i.e. a Grid.Resources) or use a DataTemplateSelector on your ComboBox.

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

Sidebar

Related Questions

I would like to assign a static list of items in a SelectList() to
I would like to assign a static function to a variable so that I
I would like to assign an enum field in model. Something like this: fields:[
i would like to assign new values to the list i pass to my
I would like to assign a variable instead of the string in my following
I have a Customers table and would like to assign a Salesperson to each
I am fetching an array and I would like to assign attributes of items
I have a JQuery plugin function where I would like to assign selectors from
I would like to try to assign a variable value to input hidden field
I would like to assign a value in a class property dynamically (that is

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.