Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Created January 27, 2016 23:11
Show Gist options
  • Save BrianJVarley/61719ef323bfea7027fb to your computer and use it in GitHub Desktop.
Save BrianJVarley/61719ef323bfea7027fb to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace ParkingTagPicker.ViewModels
{
public class ItemViewModel : INotifyPropertyChanged
{
private string _id;
/// <summary>
/// Sample ViewModel property; this property is used to identify the object.
/// </summary>
/// <returns></returns>
public string ID
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged("ID");
}
}
}
private string _councilAcronym;
/// <summary>
/// Sample ViewModel property; this property is used in the view to display its value using a Binding.
/// </summary>
/// <returns></returns>
public string CouncilAcronym
{
get
{
return _councilAcronym;
}
set
{
if (value != _councilAcronym)
{
_councilAcronym = value;
NotifyPropertyChanged("CouncilAcronym");
}
}
}
private string _councilFullName;
/// <summary>
/// Sample ViewModel property; this property is used in the view to display its value using a Binding.
/// </summary>
/// <returns></returns>
public string CouncilFullName
{
get
{
return _councilFullName;
}
set
{
if (value != _councilFullName)
{
_councilFullName = value;
NotifyPropertyChanged("CouncilFullName");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
<phone:PhoneApplicationPage x:Class="ParkingTagPicker.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
SupportedOrientations="Portrait"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
shell:SystemTray.IsVisible="True"
mc:Ignorable="d">
<!-- Data context is set to sample data above and LayoutRoot contains the root grid where all other page content is placed -->
<Grid x:Name="LayoutRoot" Background="#FF236A93">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--
LOCALIZATION NOTE:
To localize the displayed strings copy their values to appropriately named
keys in the app's neutral language resource file (AppResources.resx) then
replace the hard-coded text value between the attributes' quotation marks
with the binding clause whose path points to that string name.
For example:
Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
This binding points to the template's string resource named "ApplicationTitle".
Adding supported languages in the Project Properties tab will create a
new resx file per language that can carry the translated values of your
UI strings. The binding in these examples will cause the value of the
attributes to be drawn from the .resx file that matches the
CurrentUICulture of the app at run time.
-->
<!-- TitlePanel contains the name of the application and page title -->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" />
<TextBlock Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle1Style}"
Text="{Binding Path=LocalizedResources.ApplicationMainPageName,
Source={StaticResource LocalizedStrings}}" />
</StackPanel>
<!-- ContentPanel contains LongListSelector and LongListSelector ItemTemplate. Place additional content here -->
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<phone:LongListSelector x:Name="MainLongListSelector"
Margin="0,0,-12,0"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedCouncilItem}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}"
Text="{Binding CouncilAcronym}"
TextWrapping="Wrap" />
<TextBlock Margin="12,-6,12,0"
Style="{StaticResource PhoneTextSubtleStyle}"
Text="{Binding CouncilFullName}"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
<!--
Uncomment to see an alignment grid to help ensure your controls are
aligned on common boundaries. The image has a top margin of -32px to
account for the System Tray. Set this to 0 (or remove the margin altogether)
if the System Tray is hidden.
Before shipping remove this XAML and the image itself.
-->
<!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>
</phone:PhoneApplicationPage>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ParkingTagPicker.Resources;
using ParkingTagPicker.ViewModels;
using ParkingTagPicker.Interfaces;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
namespace ParkingTagPicker
{
public partial class MainPage : PhoneApplicationPage, INavigationCallback
{
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the LongListSelector control to the sample data
DataContext = App.ViewModel;
App.ViewModel.NavigationCallback = this;
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
// Load data for the ViewModel Items
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
//if (!App.ViewModel.IsDataLoaded && NetworkInterface.GetIsNetworkAvailable())
//{
// App.ViewModel.LoadCouncilNamesData();
//}
//else
//{
// //notify user to connect to wifi/data
// MessageBox.Show("No data connection found - please enable data/wifi in phone settings");
// await NavigateToDataSettings();
//}
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadCouncilNamesData();
}
}
public async Task NavigateToDataSettings()
{
await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-cellular:"));
}
void INavigationCallback.NavigateTo(string ItemID)
{
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + ItemID, UriKind.Relative));
}
// Sample code for building a localized ApplicationBar
//private void BuildLocalizedApplicationBar()
//{
// // Set the page's ApplicationBar to a new instance of ApplicationBar.
// ApplicationBar = new ApplicationBar();
// // Create a new button and set the text value to the localized string from AppResources.
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);
// // Create a new menu item with the localized string from AppResources.
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
}
}
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using ParkingTagPicker.Resources;
using ParkingTagPicker.DAL;
using ParkingTagPicker.Models;
using ParkingTagPicker.Extensions;
using System.Windows.Input;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Command;
using ParkingTagPicker.Interfaces;
namespace ParkingTagPicker.ViewModels
{
public class MainViewModel : ViewModelBase
{
//Dependency Injection private instances
private INavigationCallback _navCallBack = null;
public MainViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>();
}
/// <summary>
/// Creates and adds a few ItemViewModel objects into the Items collection.
/// </summary>
public void LoadCouncilNamesData()
{
//Load Council Names
this.Items.Add(new ItemViewModel() { ID = "0", CouncilAcronym = "DCC", CouncilFullName = "Dublin City Council" });
this.Items.Add(new ItemViewModel() { ID = "1", CouncilAcronym = "DLR", CouncilFullName = "Dún Laoghaire-Rathdown County Council"});
this.Items.Add(new ItemViewModel() { ID = "2", CouncilAcronym = "FCC", CouncilFullName = "Fingal County Council"});
this.Items.Add(new ItemViewModel() { ID = "3", CouncilAcronym = "SDC", CouncilFullName = "South Dublin County Council"});
this.Items.Add(new ItemViewModel() { ID = "4", CouncilAcronym = "ATC", CouncilFullName = "Arklow Town Council"});
this.Items.Add(new ItemViewModel() { ID = "5", CouncilAcronym = "DLH", CouncilFullName = "Dún Laoghaire Harbour Company" });
this.Items.Add(new ItemViewModel() { ID = "6", CouncilAcronym = "WTC", CouncilFullName = "Wicklow Town Council"});
this.Items.Add(new ItemViewModel() { ID = "7", CouncilAcronym = "TS", CouncilFullName = "Tallaght Stadium" });
this.Items.Add(new ItemViewModel() { ID = "8", CouncilAcronym = "GS", CouncilFullName = "Greystones" });
this.IsDataLoaded = true;
}
public ObservableCollection<ItemViewModel> Items { get; private set; }
public bool IsDataLoaded { get; private set; }
private ItemViewModel _selectedCouncilItem;
public ItemViewModel SelectedCouncilItem
{
get
{
return this._selectedCouncilItem;
}
set
{
this.SetProperty(ref this._selectedCouncilItem, value, () => this._selectedCouncilItem);
if (_selectedCouncilItem != null)
{
_navCallBack.NavigateTo(_selectedCouncilItem.ID);
}
}
}
public INavigationCallback NavigationCallback
{
get { return _navCallBack; }
set { _navCallBack = value; }
}
}
}
@BrianJVarley
Copy link
Author

Gist showing the MainVM, ItemVM (which models the item type), and the MainPage view and code behind. So far the break point doesn't trigger on the SelectedItem binding property of the list selector. I've tried debugging all the usuals and there are no binding errors at run time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment