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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:39:12+00:00 2026-06-14T16:39:12+00:00

I am trying to bind my object list to a listbox. Here is the

  • 0

I am trying to bind my object list to a listbox.

Here is the object list definition:

class LoadFactory : INotifyCollectionChanged
{
    public ObservableCollection<Load> Loads = new ObservableCollection<Load>();


    public LoadFactory()
    {
        AddLoad(new Load(15));
        AddLoad(new Load(12));
        AddLoad(new Load(25));

    }

    public int LoadCount()
    {
        return Loads.Count();
    }

    public void AddLoad(Load load)
    {
        Loads.Add(load);
        if (CollectionChanged != null)
            CollectionChanged(this, new NotifyCollectionChangedEventArgs(new NotifyCollectionChangedAction()));
    }

    public event NotifyCollectionChangedEventHandler CollectionChanged;

    public ObservableCollection<Load> GetLoads()
    {
        return new ObservableCollection<Load>(Loads);
    }
}

public class Load
{
    public static int LoadCount=0;
    public int Index = 0;
    public Load(float LoadMagnitude)
    {
        magnitude = LoadMagnitude;
        Index = LoadCount;
        LoadCount++;
    }

    private float magnitude;

    public float Magnitude
    {
        get
        {
            return magnitude;
        }
        set
        {
            magnitude = value;
        }
    }

    public float ToFloat()
    {
        return magnitude;
    }

    public override string ToString()
    {
        return magnitude.ToString() + " ft";
    }
}

And here is my XAML:

<Window x:Class="Sunny3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Sunny3"
    Title="MainWindow" Height="500" Width="1000">
<Grid>
        <Grid Name="motherGrid">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Button Grid.Row="1" Click="Button_Click_1">Hello</Button>
        <ListBox Grid.Row="2" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <Style TargetType="TextBox">
                            <Setter Property="Margin" Value="3"></Setter>
                            <Setter Property="FontSize" Value="20"></Setter>
                            <Setter Property="Width" Value="70"></Setter>
                            <Setter Property="Foreground" Value="Blue"></Setter>
                        </Style>
                    </DataTemplate.Resources>
                    <StackPanel>
                        <Grid>
                            <TextBox Text="{Binding Path=.}"></TextBox>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

And my code behind:

public partial class MainWindow : Window
{
    LoadFactory LF;

    public MainWindow()
    {
        LF = new LoadFactory();
        InitializeComponent();
        this.DataContext = LF.Loads;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Load newone = new Load(154);
        LF.AddLoad(newone);
    }
}

The problem is that the I cannot seem to get my loads bound to the text boxes.

Any suggestions?

  • 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-14T16:39:13+00:00Added an answer on June 14, 2026 at 4:39 pm

    You need to make Loads a property, not a field.

    public ObservableCollection<Load> Loads { get; set; }
    
    public LoadFactory()
    {
        Loads = new ObservableCollection<Load>();
        AddLoad(new Load(15));
        AddLoad(new Load(12));
        AddLoad(new Load(25));
    }
    

    The text box binding should be to a property within the load class.

    <TextBox Text="{Binding Path=Magnitude}"></TextBox>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to bind List to Listbox. And at the Button1Click method new instance
I'm trying to bind a list of objects to a list box. Each object
I am trying to bind a combo box to a list of objects, and
Im trying to bind a list with datetime objects to my repeater. if (e.Item.ItemType
I'm trying to bind a list of data to a data grid, but can't
I'm trying to bind a TreeView control to the public folders the machine ASUS-PC
I'm trying to bind a combobox to a ObservableCollection.When the form is displayed the
I'm trying to bind a ListBox's SelectedItem data to a property. The following code
I'm trying to bind a list box in the simplest manner possible with the
I'm trying to display a ListBox inside of a GridViewColumn that needs to bind

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.