Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First I would recommend not using static members, it just seems to work better without.</p> <p>Minor changes to ShoppingListItems:</p> <pre><code>public class ShoppingListItems { public ObservableCollection&lt;Item&gt; ItemCollection { get; set; } public ShoppingListItems() { ItemCollection = new ObservableCollection&lt;Item&gt;(); AddAnItem("test"); AddAnItem("test2"); AddAnItem("test3"); } public void AddAnItem(string name) { ItemCollection.Add(new Item() { ItemName = name }); } } </code></pre> <p>Things are non-static, and List&lt;> is ObservableCollection&lt;> instead.</p> <p>In the xaml:</p> <pre><code>&lt;Grid&gt; &lt;Grid.Resources&gt; &lt;DataTemplate x:Key="itemLayout" DataType="{x:Type local:Item}"&gt; &lt;TextBlock Text="{Binding ItemName}" FontSize="50" /&gt; &lt;/DataTemplate&gt; &lt;/Grid.Resources&gt; &lt;ListBox x:Name="itemsListBox" Margin="2" ItemsSource="{Binding}" ItemTemplate="{DynamicResource itemLayout}" IsSynchronizedWithCurrentItem="True"&gt; &lt;/ListBox&gt; &lt;/Grid&gt; </code></pre> <p>In the code-behind (the .cs file that corresponds to the xaml file):</p> <pre><code> private ShoppingListItems _shoppingList; public MainWindow() { InitializeComponent(); _shoppingList = new ShoppingListItems(); itemsListBox.DataContext = _shoppingList.ItemCollection; } </code></pre> <p>EDIT:</p> <p>What I did was put the data template up in the grid resources, while you put the data template as part of the list box resources. As far as I can tell the only real difference is if the template is part of the grid resources it could be used in multiple places. Say you wanted two shopping lists, maybe one for things you need and another for things you've already bought, and you wanted them formatted the same way, it would be best to put the template up in the grid so both lists could reference it. Since you only have one list it probably doesn't matter, either way is fine.</p> <p>As for the Singleton pattern, see: <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow">Singleton on Wikipedia</a></p> <p>The simplest approach would have your ShoppingListItems look like this:</p> <pre><code>public class ShoppingListItems { private static ShoppingListItems _instance = new ShoppingListItems(); public ObservableCollection&lt;Item&gt; ItemCollection { get; private set; } public static ShoppingListItems Instance { get { return _instance; } } private ShoppingListItems() { ItemCollection = new ObservableCollection&lt;Item&gt;(); AddAnItem("test"); AddAnItem("test2"); AddAnItem("test3"); } public void AddAnItem(string name) { ItemCollection.Add(new Item() { ItemName = name }); } } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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