Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I bind to a List<T> using DataContext?
    primarykey
    data
    text
    <p>I'm self-learning C#, OOP, and WPF so the potential for stuff ups is staggering.</p> <p>So given that, can someone please explain why after clicking the button in my tiny test example the Name property appears in the TextBox but the ListBox shows nothing?</p> <h2><strong>XAML:</strong></h2> <pre><code>&lt;Window x:Class="BindingTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="BindingTest" Height="250" Width="300"&gt; &lt;Grid Name="mainGrid"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="50" /&gt; &lt;RowDefinition Height="50" /&gt; &lt;RowDefinition Height="100" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Grid.Row="0" Name="MakeIntListButton" Click="MakeIntListButton_Click"&gt;Make and Display Integer List&lt;/Button&gt; &lt;TextBox Grid.Row="1" Text ="{Binding Path=Name}" /&gt; &lt;ListBox Grid.Row="2" ItemsSource="{Binding Path=MyIntegers}" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <h2><strong>C#:</strong></h2> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace BindingTest { /// &lt;summary&gt; /// Interaction logic for Window1.xaml /// &lt;/summary&gt; public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void MakeIntListButton_Click(object sender, RoutedEventArgs e) { AClass InstanceOfAClass = new AClass(); InstanceOfAClass.MyIntegers.Add(6); InstanceOfAClass.MyIntegers.Add(7); InstanceOfAClass.MyIntegers.Add(42); InstanceOfAClass.Name = "Fred"; mainGrid.DataContext =InstanceOfAClass ; } } public class AClass { public string Name {get;set;} public List&lt;int&gt; MyIntegers = new List&lt;int&gt;(); } } </code></pre>
    singulars
    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