Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to bind the view model to the view
    primarykey
    data
    text
    <p>I am having trouble binding my <code>View Model</code> to my <code>View</code>. I am a beginner with <code>MVVM</code>, but I believe I am implementing my system (almost) correctly. I have a <code>Model</code> that contains data, which I am getting in my <code>View Model</code>, and then when my page is navigated to, I am attempting to grab that <code>View Model</code> data and binding it to the <code>View</code>. </p> <p>My issue is that I have a <code>ListBox</code> in my <code>View</code> with 3 objects per item, and I cannot seem to bind to it correctly for each item in my list.</p> <p><strong>MainPage.xaml</strong></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><strong>MainPage.xaml.cs</strong></p> <pre><code>public FavoritesPage() { InitializeComponent(); // Set the data context of the listbox control to the sample data FavoritesListBox.DataContext = App.ViewModel; } </code></pre> <p><strong>App.xaml.cs</strong></p> <pre><code>private static MainViewModel viewModel = null; public static MainViewModel ViewModel { get { // Delay creation of the view model until necessary if (viewModel == null) viewModel = new MainViewModel(); return viewModel; } } </code></pre> <p><strong>MainViewModel.cs</strong></p> <pre><code>public ObservableCollection&lt;ItemViewModel&gt; FavoriteItems { get; private set; } public MainViewModel() { //FavoriteItems = new ObservableCollection&lt;ItemViewModel&gt;(); FavoriteItems = Settings.FavoritesList.Value; } </code></pre> <p><strong>Settings.cs (The Model)</strong></p> <pre><code>public static Setting&lt;ObservableCollection&lt;ItemViewModel&gt;&gt; FavoritesList = new Setting&lt;ObservableCollection&lt;ItemViewModel&gt;&gt;( "Favorites", new ObservableCollection&lt;ItemViewModel&gt;()); </code></pre> <p><strong>ItemViewModel.cs</strong></p> <pre><code>private string _favicon; public string Favicon { get { return _favicon; } set { if (value != _favicon) { _favicon = value; NotifyPropertyChanged("Favicon"); } } } private string _name; public string Name { get { return _name; } set { if (value != _name) { _name = value; NotifyPropertyChanged("Name"); } } } private string _address; public string Address { get { return _address; } set { if (value != _address) { _address = value; NotifyPropertyChanged("Address"); } } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p>..and this is where and how I am saving each item (which should have three properties listed in the <code>ItemViewModel</code></p> <pre><code>void addToFavorites_Click(object sender, EventArgs e) { var favoriteItem = new ItemViewModel{ Favicon = "", Name = "", Address = TheBrowser.currentUrl() }; Settings.FavoritesList.Value.Add(favoriteItem); } </code></pre> <p>Where <code>FavoritesList</code> is populated using an <code>ItemViewModel</code> containing 3 objects. The list is being populated correctly because during debugging I can see the entities in <code>FavoritesList</code>, but I am having an issue calling these entities in the <code>view model</code> to show up in my <code>ListBox</code> in the <code>view</code>? </p> <p>I believe I am binding incorrectly but I'm not sure how to fix this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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