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

  • Home
  • SEARCH
  • 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 134177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:33:59+00:00 2026-05-11T06:33:59+00:00

I have a WPF ListBox, and I’ve added some ‘FooBar’ objects as items (by

  • 0

I have a WPF ListBox, and I’ve added some ‘FooBar’ objects as items (by code). FooBars aren’t WPF objects, just dumb class with an overwritten ToString() function.

Now, when I change a property which influences the ToString, I’d like to get the ListBox to update.

  1. How can I do this ‘quick and dirty’ (like repaint).
  2. Is dependency properties the way to go on this?
  3. Is it worth it/always advisable, to create a wpf wrapper class for my FooBars?

Thanks…

  • 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. 2026-05-11T06:34:00+00:00Added an answer on May 11, 2026 at 6:34 am

    Your type should implement INotifyPropertyChanged so that a collection can detect the changes. As Sam says, pass string.Empty as the argument.

    You also need to have the ListBox‘s data source be a collection that provides change notification. This is done via the INotifyCollectionChanged interface (or the not-so-WPF IBindingList interface).

    Of course, you need the INotifyCollectionChanged interface to fire whenever one of the member INotifyPropertyChanged items fires its event. Thankfully there are a few types in the framework that provide this logic for you. Probably the most suitable one is ObservableCollection<T>. If you bind your ListBox to an ObservableCollection<FooBar> then the event chaining will happen automatically.

    On a related note, you don’t have to use a ToString method just to get WPF to render the object in the way that you want. You can use a DataTemplate like this:

    <ListBox x:Name='listBox1'>     <ListBox.Resources>         <DataTemplate DataType='{x:Type local:FooBar}'>             <TextBlock Text='{Binding Path=Property}'/>         </DataTemplate>     </ListBox.Resources> </ListBox> 

    In this way you can control the presentation of the object where it belongs — in the XAML.

    EDIT 1 I noticed your comment that you’re using the ListBox.Items collection as your collection. This won’t do the binding required. You’re better off doing something like:

    var collection = new ObservableCollection<FooBar>(); collection.Add(fooBar1);  _listBox.ItemsSource = collection; 

    I haven’t checked that code for compilation accuracy, but you get the gist.

    EDIT 2 Using the DataTemplate I gave above (I edited it to fit your code) fixes the problem.

    It seems strange that firing PropertyChanged doesn’t cause the list item to update, but then using the ToString method isn’t the way that WPF was intended to work.

    Using this DataTemplate, the UI binds correctly to the exact property.

    I asked a question on here a while back about doing string formatting in a WPF binding. You might find it helpful.

    EDIT 3 I’m baffled as to why this is still not working for you. Here’s the complete source code for the window I’m using.

    Code behind:

    using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows;  namespace StackOverflow.ListBoxBindingExample {     public partial class Window1     {         private readonly FooBar _fooBar;          public Window1()         {             InitializeComponent();              _fooBar = new FooBar('Original value');              listBox1.ItemsSource = new ObservableCollection<FooBar> { _fooBar };         }          private void button1_Click(object sender, RoutedEventArgs e)         {             _fooBar.Property = 'Changed value';         }     }      public sealed class FooBar : INotifyPropertyChanged     {         public event PropertyChangedEventHandler PropertyChanged;          private string m_Property;          public FooBar(string initval)         {             m_Property = initval;         }          public string Property         {             get { return m_Property; }             set             {                 m_Property = value;                 OnPropertyChanged('Property');             }         }          private void OnPropertyChanged(string propertyName)         {             var handler = PropertyChanged;             if (handler != null)                 handler(this, new PropertyChangedEventArgs(propertyName));         }     } } 

    XAML:

    <Window x:Class='StackOverflow.ListBoxBindingExample.Window1'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:local='clr-namespace:StackOverflow.ListBoxBindingExample'     Title='Window1' Height='300' Width='300'>     <DockPanel LastChildFill='True'>         <Button Click='button1_Click' DockPanel.Dock='Top'>Click Me!</Button>         <ListBox x:Name='listBox1'>             <ListBox.Resources>                 <DataTemplate DataType='{x:Type local:FooBar}'>                     <TextBlock Text='{Binding Path=Property}'/>                 </DataTemplate>             </ListBox.Resources>         </ListBox>     </DockPanel> </Window> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try --std=gnu99. The default for GCC is '--std=gnu89' which means… May 11, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer To create an instance in JavaScript you need to write… May 11, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer That's what partial classes are for. Just add your stuff… May 11, 2026 at 6:55 pm

Related Questions

I have a WPF ListBox, and I've added some 'FooBar' objects as items (by
I have a WPF ListBox bound to a data object. Inside the listbox are
I have a WPF ListBox with a defined DataTemplate. In that template, I have
I have a WPF listbox which displays messages. It contains an avatar on the
I have a WPF form with a ListBox of items bound to a method

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.