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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:16:16+00:00 2026-05-29T12:16:16+00:00

Sorry if this is elementary, but I’ve not found a good example precisely describing

  • 0

Sorry if this is elementary, but I’ve not found a good example precisely describing what I need to do to enable the following scenario:

I have two classes:

public class Thing:DependencyObject
{

    // Fields
    private string name = "";


    // Properties
    public string Name
    {
        get { return name; }
        set { name = value; }
    }


    // Dependency Properties

    public int Count
    {
        get { return (int)GetValue(CountProperty); }
        set { SetValue(CountProperty, value); }
    }

    public static readonly DependencyProperty CountProperty =
        DependencyProperty.Register("Count", typeof(int), typeof(Thing), null);


    // Methods
    public override string ToString()
    {
        return DateTime.Now.ToString() + " " + this.Name + " " + this.Count.ToString();

    }

    // Constructors

    public Thing(string name)
    {
        this.Name = name;
    }

}

and a class to contain Thing objects

public class Things: DependencyObjectCollection
{

}

The MainPage.xaml.cs file creates a couple of Thing objects and adds them to a Things collection.

public partial class MainPage : PhoneApplicationPage
{
Things Things = new Things();

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Thing thingA = new Thing("A");
        Thing thingB = new Thing("B");
        Things.Add(thingA);
        Things.Add(thingB);


    }

    private void Action_Click(object sender, RoutedEventArgs e)
    {
        this.Things[0].Count += 1;
        Debug.WriteLine(this.Things[0].ToString());
    }
}

My question is how to write binding code such that the ListBox displays the Things contained in the Things object AND the display of the Thing objects as strings in the ListBox are automatically updated when a Thing object’s DependencyProperty Count is changed.

Specifically, what do I do the following XAML to make the above scenario happen.
That is, when I add a new Thing object to the Things object, a new item is added to the ListBox. and when I change an existing item with the Things object, the change appear in the ListBox.

In Windows Phone 7, the MainPage contains a ListBox:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <ListBox x:Name="ThingsBox" />
            <Button x:Name="Action" Content="Action" Click="Action_Click"/>
        </StackPanel>
    </Grid>
</Grid>
  • 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-29T12:16:17+00:00Added an answer on May 29, 2026 at 12:16 pm

    Firstly, I would recommend using plain old objects (that implement INotifyPropertyChanged) for your model. The use of dependency objects is overkill. See this related question:

    INotifyPropertyChanged vs. DependencyProperty in ViewModel

    In order to bind your collection of objects to a list in such a way that the UI automatically updates as you add / remove objects, you should create an ObservableCollection and set it as the ItemsSource of the ListBox:

    ObservableCollection<Thing> ThingsCollection { get; set }
    
    <MainPage x:Name="_mainPage">
        <ListBox ItemsSource="{Binding ThingsCollection}" />
    </MainPage>
    

    The key here is that ObservableCollection implements INotifyCollectionChanged, which raises events when the collection is changed. The ListBox handles these events in order to keep the UI synchronized.

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

Sidebar

Related Questions

I am sorry if this is a duplicate or too elementary, but how do
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry if this is an elementary question but I've just started to consider whether
Sorry this might be a simple question, but I could not figure it out.
Sorry this is going to sound like a ridiculous question but can methods (not
First, sorry this is so long. I probably don't need all the code, but
Sorry this is not a very well defined question, I am thinking about an
Ok sorry this might seem like a dumb question but I cannot figure this
Sorry if this is a little off-topic for regular stackoverflow questions, but we're tearing

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.