Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get data from a View Model into the View
    primarykey
    data
    text
    <p>I posted a similar question earlier, but I was having an issue with getting data from the ViewModel into the View. The issue lies with getting the data out of the object where it is stored when it is time to bind to the View. I created a class that declares 3 items which I use to help populate an ObservableCollection of items that will be bound to a ListBox in the view. I am not sure if I am going about this correctly, so to illustrate I will show below:</p> <p>ListItem.cs (this is the custom class I defined to help populate the collection of items)</p> <pre><code>public string Favicon { get; set; } public string Name { get; set; } public string Address { get; set; } </code></pre> <p>MainPage.xaml.cs (here I want to save the data for each item to be added in the ObservableCollection)</p> <pre><code>void addToFavorites_Click(object sender, EventArgs e) { var favoriteItem = new ListItem { Favicon = "/Image/1.jpg", Name = "item1", Address = "some address" }; Settings.FavoritesList.Value.Add(favoriteItem); } </code></pre> <p>Settings.cs (the settings class used to store the FavoritesList ObservableCollection)</p> <pre><code>public class Settings { public static Setting&lt;ObservableCollection&lt;ListItem&gt;&gt; FavoritesList = new Setting&lt;ObservableCollection&lt;ListItem&gt;&gt;("Favorites", new ObservableCollection&lt;ListItem&gt;()); } </code></pre> <p>Now I am attempting to call this stored ObservableCollection <code>FavoritesList</code> in my ViewModel so that I may bind it to a view in another page.</p> <p>MainViewModel.cs</p> <pre><code>public ObservableCollection&lt;ListItem&gt; FavoriteItems { get; private set; } public MainViewModel() { FavoriteItems = Settings.FavoritesList.Value; } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p>And then on navigation to my FavoritesPage.xaml, I would like to bind the ViewModel to the View to be displayed in a listbox</p> <p>FavoritesPage.xaml</p> <pre><code>&lt;ListBox x:Name="FavoritesListBox" ItemsSource="{Binding FavoriteItems}" SelectionChanged="FavoritesListBox_SelectionChanged"&gt; &lt;StackPanel Orientation="Horizontal" Margin="12,0,12,0"&gt; &lt;Image x:Name="favicon" Source="{Binding Favicon}" Width="50" Height="50"/&gt; &lt;StackPanel&gt; &lt;TextBlock x:Name="favoritesName" Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"/&gt; &lt;TextBlock x:Name="favoritesAddress" Text="{Binding Address}" Margin="12,0,0,0"/&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/ListBox&gt; </code></pre> <p>FavoritesPage.xaml.cs</p> <pre><code>public FavoritesPage() { InitializeComponent(); // Set the data context of the listbox control to the sample data DataContext = App.ViewModel; } </code></pre> <p>Now for some reason I cannot set <code>DataContext = App.ViewModel;</code>. I believe I narrowed the problem to when I initially saved the values in the MainPage.xaml.cs using the ListItem class. I am unsure of how to populate the ListPicker from here? Am I doing something wrong somewhere, or should I do something different to set the datacontext correctly?</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.
 

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