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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:55:51+00:00 2026-05-28T16:55:51+00:00

I would like to bind a list box to an observable collection in code

  • 0

I would like to bind a list box to an observable collection in code behind. This is what I am using for the binding:

Binding binding = new Binding();
binding.Source = symTable;
substanceList.SetBinding(ListBox.ItemsSourceProperty, binding);

symTable inherits from ObservableCollection, the Count property gets updated appropriatelly so I know I am adding elements correctly, but the list box isn’t. I didn’t know what to set the path to, since in XAML it is bound to the whole list.

Note: When adding individual items to the ListBox they get shown, so it is not a display issue. I also tried:

this.Resources.Add("symTable", symTable);

in the window constructor and then this:

but it says the resource cannot be resolved.

I also tried adding it as a resource in XAML but it didn’t work again:
//in the window’s resources.

<s:SymbolTable x:Key="symTable"/> 
...
<ListBox x:Name="substanceList" ItemsSource="{Binding Source={StaticResource symTable}}"/>

and then in code behind:

symTable = (SymbolTable)this.FindResource(“symTable”);

Does anyone know any other way to do this in code behind or XAML, I think the ElementName refers to objects defined in code behind.

Here is part of the class definition for symTable:

public class SymbolTable : ObservableCollection<Substance>
{
    Dictionary<string, Substance> symbolTable;
    ...
    public Substance Insert(Substance s)
    {

        if (!symbolTable.ContainsKey(s.Name))
        {
            symbolTable.Add(s.Name, s);
            Items.Add(s);
        }

        return symbolTable[s.Name];
    }

Note alright so I just noticed the most weird thing, Items.Add wasn’t raising the INotifyChanged event. I used Items.Add in my Insert method, I am guessing that Items.Add doesn’t raise a INotifyChanged event so the ListBox wasn’t getting updated, but when did Add instead of Items.Add then it worked. Do you know if this is indeed the case?

  • 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-05-28T16:55:51+00:00Added an answer on May 28, 2026 at 4:55 pm

    Ok, following the comments on your question, here is how to declare SymbolTable as resource in XAML and how to bind a ListBox to it. Note the XAML namespace declaration ‘local’, which refers to the local namespace/assembly, which is named ListBindingTest in my test project.

    <Window x:Class="ListBindingTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:ListBindingTest"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:SymbolTable x:Key="symTable"/>
        </Window.Resources>
        <Grid>
            <ListBox ItemsSource="{Binding Source={StaticResource symTable}}"/>
        </Grid>
    </Window>
    

    You may access the SymbolTable resource in code behind in your window class like this:

    SymbolTable st = (SymbolTable)Resources["symTable"];
    st.Add(new Substance());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know what is the best practice between using bind tag
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to bind a list control (like a ListView) in a view
How to optimize this code? ParentDoglist, ChildDoglistis - Ilist. dogListBox - List Box foreach
I would like to bind a TreeView control I have defined in XAML to
I would like to bind an Image to some kind of control an delete
I would like to bind C-` (control-backquote) but I could not do it. The
I have a GridView that I would like to bind to an ObjectDataSource. I
In a WPF based project I would like to bind the content of a
Hi I have a text field which I would like to bind to a

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.