Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to bind my object list to a listbox
    primarykey
    data
    text
    <p>I am trying to bind my object list to a listbox.</p> <p>Here is the object list definition:</p> <pre><code>class LoadFactory : INotifyCollectionChanged { public ObservableCollection&lt;Load&gt; Loads = new ObservableCollection&lt;Load&gt;(); 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&lt;Load&gt; GetLoads() { return new ObservableCollection&lt;Load&gt;(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"; } } </code></pre> <p>And here is my XAML:</p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;Grid Name="motherGrid"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Grid.Row="1" Click="Button_Click_1"&gt;Hello&lt;/Button&gt; &lt;ListBox Grid.Row="2" ItemsSource="{Binding}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;DataTemplate.Resources&gt; &lt;Style TargetType="TextBox"&gt; &lt;Setter Property="Margin" Value="3"&gt;&lt;/Setter&gt; &lt;Setter Property="FontSize" Value="20"&gt;&lt;/Setter&gt; &lt;Setter Property="Width" Value="70"&gt;&lt;/Setter&gt; &lt;Setter Property="Foreground" Value="Blue"&gt;&lt;/Setter&gt; &lt;/Style&gt; &lt;/DataTemplate.Resources&gt; &lt;StackPanel&gt; &lt;Grid&gt; &lt;TextBox Text="{Binding Path=.}"&gt;&lt;/TextBox&gt; &lt;/Grid&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>And my code behind:</p> <pre><code>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); } } </code></pre> <p>The problem is that the I cannot seem to get my loads bound to the text boxes.</p> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload