Note that there are some explanatory texts on larger screens.

plurals
  1. POWP7 - Unable to databind to the same object from two pages
    primarykey
    data
    text
    <ol> <li>Page1 creates and databinds to a new instance of Foo (call it theFoo)</li> <li>theFoo.Name is set via textbox from Page1</li> <li>theFoo is saved to a globally accessible data structure (list of Foos, whatever)</li> <li>navigate from Page1 to Page2</li> <li>Page2 databinds to the global list of Foos, to display all Foo instances</li> </ol> <p>When I do this, I can verify that the Foo instance is added to the global list. But Page2 never shows any Foos.</p> <p>If I manually add Foos to the global list (in code instead of from Page1), then navigate to Page2 without ever navigating to Page1 at all, I see Foos displayed in Page2.</p> <p>What's the issue here?</p> <p>Update:</p> <p>Here's some relevant code...</p> <p><b>Item.cs (Data and global storage structure)</b></p> <pre><code>public class Item { public string Name { get; set; } } internal static class ItemRepos { private static List&lt;Item&gt; _items = new List&lt;Item&gt;(); public static Item New() { return new Item(); } public static int Count { get { return _items.Count; } } public static IEnumerable&lt;Item&gt; GetAll() { return _items; } public static Item Get( string name ) { return _items.SingleOrDefault( item =&gt; item.Name == name ); } public static void Save( Item item ) { if ( _items.Contains( item ) == false ) { _items.Add( item ); } } public static void Remove( Item item ) { _items.Remove( item ); } } </code></pre> <p><b>Relevant Page1.xaml fragment</b></p> <pre><code>&lt;TextBlock Text="Name:" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" /&gt; &lt;TextBox x:Name="txtName" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Name}" /&gt; </code></pre> <p><b>Page1.xaml.cs</b></p> <pre><code>public partial class ItemDetail : PhoneApplicationPage { public ItemDetail() { InitializeComponent(); } protected override void OnNavigatedTo( NavigationEventArgs e ) { base.OnNavigatedTo( e ); this.DataContext = ItemRepos.New(); } private void Nav( object sender, EventArgs e ) { NavigationService.Navigate( new Uri( "/Page2.xaml", UriKind.RelativeOrAbsolute ) ); } private void Save( object sender, EventArgs e ) { ItemRepos.Save( (Item) this.DataContext ); } } </code></pre> <p><b>Relevant Page2.xaml fragment</b></p> <pre><code>&lt;controls:PivotItem Header="A-Z"&gt; &lt;ListBox x:Name="listAZ" ItemsSource="{Binding}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/controls:PivotItem&gt; </code></pre> <p><b>Page2.xaml.cs</b></p> <pre><code>public partial class ViewItems : PhoneApplicationPage { public ViewItems() { InitializeComponent(); } protected override void OnNavigatedTo( NavigationEventArgs e ) { base.OnNavigatedTo( e ); this.DataContext = ItemRepos.GetAll(); } } </code></pre>
    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