Skip to content

DataBinding in WP7–The Quick and Easy Way

September 16, 2011

Here is something I found for those who wish to perform databinding on a  List Box using ItemsSource using static data the quick and easy way!

I started by added a static reference to the MainViewModel in the App class of App.xaml.cs

 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;
  }
 }

My two lists in the MainViewModel class of MainViewModel.cs looked like this:

 public MainViewModel()
 {
  // Insert code required on object creation below this point
  Items1 = new ObservableCollection<ItemViewModel>() {
  new ItemViewModel() { LineOne = "runtime one", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu", },
  new ItemViewModel() { LineOne = "runtime two", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus", },
  new ItemViewModel() { LineOne = "runtime three", LineTwo = "Habitant inceptos interdum lobortis", LineThree = "Habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent", },
  };

  Items2 = new ObservableCollection<ItemViewModel>() {
  new ItemViewModel() { LineOne = "runtime one", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu", },
  new ItemViewModel() { LineOne = "runtime two", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus", },
  };

 }

 public ObservableCollection<ItemViewModel> Items1 { get; private set; }

 public ObservableCollection<ItemViewModel> Items2 { get; private set; }

Now I changed the initialisation of DataContext in MainPage.xaml.cs to reference the static object

  if (DataContext == null)
  DataContext = App.ViewModel;

Removed the direct reference to the Items1 list in MainPage.xaml so it only had the binding

  <ListBox x:Name="ListBoxOne" ItemsSource="{Binding}" MouseLeftButtonUp="ListBoxOne_MouseLeftButtonUp" Style="{StaticResource PhoneListBox}">

And now I can use my conditions check to set the ItemsSource using the following code in MainPage.xaml.cs

  ListBoxOne.ItemsSource = App.ViewModel.Items2;
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: