PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-toolkit/PhoneToolkitSample8/Samples/LongListMultiSelectorSample.xaml.cs

https://bitbucket.org/jeremejevs/milk-manager
C# | 519 lines | 335 code | 52 blank | 132 comment | 31 complexity | 354cf630981d70c58016fe487d7b47ab MD5 | raw file
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. // All other rights reserved.
  5. using Microsoft.Phone.Controls;
  6. using Microsoft.Phone.Shell;
  7. using PhoneToolkitSample.Data;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Text;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. namespace PhoneToolkitSample.Samples
  16. {
  17. /// <summary>
  18. /// Sample code for LongListMultiSelector
  19. /// </summary>
  20. public partial class LongListMultiSelectorSample : PhoneApplicationPage
  21. {
  22. class PivotCallbacks
  23. {
  24. public Action Init {get;set;}
  25. public Action OnActivated{get;set;}
  26. public Action<CancelEventArgs> OnBackKeyPress {get;set;}
  27. }
  28. Dictionary<object, PivotCallbacks> _callbacks;
  29. /// <summary>
  30. /// Initializes the dictionary of delegates to call when each pivot is selected
  31. /// </summary>
  32. public LongListMultiSelectorSample()
  33. {
  34. InitializeComponent();
  35. this.Loaded += LongListMultiSelectorSample_Loaded;
  36. _callbacks = new Dictionary<object,PivotCallbacks>();
  37. _callbacks[MultiselectLbxItem] = new PivotCallbacks {
  38. Init = CreateEmailApplicationBarItems,
  39. OnActivated = OnEmailPivotItemActivated,
  40. OnBackKeyPress = OnEmailBackKeyPressed
  41. };
  42. _callbacks[BuddiesPivotItem] = new PivotCallbacks
  43. {
  44. OnActivated = SetupBuddiesApplicationBar,
  45. OnBackKeyPress = OnBuddiesBackKeyPressed,
  46. };
  47. _callbacks[GridModeItem] = new PivotCallbacks
  48. {
  49. Init = CreatePicturesApplicationBarItems,
  50. OnActivated = OnPicturesPivotItemActivated,
  51. OnBackKeyPress = OnGridBackKeyPressed
  52. };
  53. _callbacks[DataboundPivotItem] = new PivotCallbacks
  54. {
  55. OnActivated = SetupBoundBuddiesApplicationBar
  56. };
  57. foreach (var callbacks in _callbacks.Values)
  58. {
  59. if (callbacks.Init != null)
  60. {
  61. callbacks.Init();
  62. }
  63. }
  64. }
  65. void LongListMultiSelectorSample_Loaded(object sender, RoutedEventArgs e)
  66. {
  67. if (this.NavigationContext.QueryString.ContainsKey("multiselect"))
  68. {
  69. MessageBox.Show(
  70. @"The MultiSelectList has been deprecated in Windows Phone 8 in favor of LongListMultiSelector which is built on top of the more performant LongListSelector.
  71. This sample and the sample code demonstrates how to use the new LongListMultiSelector control.");
  72. }
  73. }
  74. /// <summary>
  75. /// Setup the application bar buttons according to the active Pivot Item
  76. /// </summary>
  77. /// <param name="sender"></param>
  78. /// <param name="e"></param>
  79. private void OnPivotSelectionChanged(object sender, SelectionChangedEventArgs e)
  80. {
  81. PivotCallbacks callbacks;
  82. if (_callbacks.TryGetValue(SamplePivot.SelectedItem, out callbacks) && (callbacks.OnActivated != null))
  83. {
  84. callbacks.OnActivated();
  85. }
  86. }
  87. /// <summary>
  88. /// Defers back treatment to active pivot function
  89. /// </summary>
  90. /// <param name="e"></param>
  91. protected override void OnBackKeyPress(CancelEventArgs e)
  92. {
  93. base.OnBackKeyPress(e);
  94. if (CurrentPicture != null)
  95. {
  96. CurrentPicture = null;
  97. e.Cancel = true;
  98. }
  99. else
  100. {
  101. PivotCallbacks callbacks;
  102. if (_callbacks.TryGetValue(SamplePivot.SelectedItem, out callbacks) && (callbacks.OnBackKeyPress != null))
  103. {
  104. callbacks.OnBackKeyPress(e);
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Resets the application bar
  110. /// </summary>
  111. void ClearApplicationBar()
  112. {
  113. while (ApplicationBar.Buttons.Count > 0)
  114. {
  115. ApplicationBar.Buttons.RemoveAt(0);
  116. }
  117. while (ApplicationBar.MenuItems.Count > 0)
  118. {
  119. ApplicationBar.MenuItems.RemoveAt(0);
  120. }
  121. }
  122. #region MultiselectListbox item
  123. ApplicationBarIconButton select;
  124. ApplicationBarIconButton delete;
  125. ApplicationBarMenuItem markAsRead;
  126. ApplicationBarMenuItem markAsUnread;
  127. /// <summary>
  128. /// Creates ApplicationBar items for email list
  129. /// </summary>
  130. private void CreateEmailApplicationBarItems()
  131. {
  132. select = new ApplicationBarIconButton();
  133. select.IconUri = new Uri("/Toolkit.Content/ApplicationBar.Select.png", UriKind.RelativeOrAbsolute);
  134. select.Text = "select";
  135. select.Click += OnSelectClick;
  136. delete = new ApplicationBarIconButton();
  137. delete.IconUri = new Uri("/Toolkit.Content/ApplicationBar.Delete.png", UriKind.RelativeOrAbsolute);
  138. delete.Text = "delete";
  139. delete.Click += OnDeleteClick;
  140. markAsRead = new ApplicationBarMenuItem();
  141. markAsRead.Text = "mark as read";
  142. markAsRead.Click += OnMarkAsReadClick;
  143. markAsUnread = new ApplicationBarMenuItem();
  144. markAsUnread.Text = "mark as unread";
  145. markAsUnread.Click += OnMarkAsUnreadClick;
  146. }
  147. /// <summary>
  148. /// Called when Email pivot item is activated : makes sure that selection is disabled and updates the application bar
  149. /// </summary>
  150. void OnEmailPivotItemActivated()
  151. {
  152. if (EmailList.IsSelectionEnabled)
  153. {
  154. EmailList.IsSelectionEnabled = false; // Will update the application bar too
  155. }
  156. else
  157. {
  158. SetupEmailApplicationBar();
  159. }
  160. }
  161. /// <summary>
  162. /// Configure ApplicationBar items for email list
  163. /// </summary>
  164. private void SetupEmailApplicationBar()
  165. {
  166. ClearApplicationBar();
  167. if (EmailList.IsSelectionEnabled)
  168. {
  169. ApplicationBar.Buttons.Add(delete);
  170. ApplicationBar.MenuItems.Add(markAsRead);
  171. ApplicationBar.MenuItems.Add(markAsUnread);
  172. UpdateEmailApplicationBar();
  173. }
  174. else
  175. {
  176. ApplicationBar.Buttons.Add(select);
  177. }
  178. ApplicationBar.IsVisible = true;
  179. }
  180. /// <summary>
  181. /// Updates the email Application bar items depending on selection
  182. /// </summary>
  183. private void UpdateEmailApplicationBar()
  184. {
  185. if (EmailList.IsSelectionEnabled)
  186. {
  187. bool hasSelection = ((EmailList.SelectedItems != null) && (EmailList.SelectedItems.Count > 0));
  188. delete.IsEnabled = hasSelection;
  189. markAsRead.IsEnabled = hasSelection;
  190. markAsUnread.IsEnabled = hasSelection;
  191. }
  192. }
  193. /// <summary>
  194. /// Back Key Pressed = leaves the selection mode
  195. /// </summary>
  196. /// <param name="e"></param>
  197. protected void OnEmailBackKeyPressed(CancelEventArgs e)
  198. {
  199. if (EmailList.IsSelectionEnabled)
  200. {
  201. EmailList.IsSelectionEnabled = false;
  202. e.Cancel = true;
  203. }
  204. }
  205. /// <summary>
  206. /// Passes the email list in selection mode
  207. /// </summary>
  208. /// <param name="sender"></param>
  209. /// <param name="e"></param>
  210. void OnSelectClick(object sender, EventArgs e)
  211. {
  212. EmailList.IsSelectionEnabled = true;
  213. }
  214. /// <summary>
  215. /// Deletes selected items
  216. /// </summary>
  217. /// <param name="sender"></param>
  218. /// <param name="e"></param>
  219. void OnDeleteClick(object sender, EventArgs e)
  220. {
  221. IList source = EmailList.ItemsSource as IList;
  222. while (EmailList.SelectedItems.Count > 0)
  223. {
  224. source.Remove((EmailObject)EmailList.SelectedItems[0]);
  225. }
  226. }
  227. /// <summary>
  228. /// Mark all items as read
  229. /// </summary>
  230. /// <param name="sender"></param>
  231. /// <param name="e"></param>
  232. void OnMarkAsReadClick(object sender, EventArgs e)
  233. {
  234. foreach (EmailObject obj in EmailList.SelectedItems)
  235. {
  236. obj.Unread = false;
  237. }
  238. EmailList.IsSelectionEnabled = false;
  239. }
  240. /// <summary>
  241. /// Mark all items as unread
  242. /// </summary>
  243. /// <param name="sender"></param>
  244. /// <param name="e"></param>
  245. void OnMarkAsUnreadClick(object sender, EventArgs e)
  246. {
  247. foreach (EmailObject obj in EmailList.SelectedItems)
  248. {
  249. obj.Unread = true;
  250. }
  251. EmailList.IsSelectionEnabled = false;
  252. }
  253. /// <summary>
  254. /// Adjusts the user interface according to the number of selected emails
  255. /// </summary>
  256. /// <param name="sender"></param>
  257. /// <param name="e"></param>
  258. private void OnEmailListSelectionChanged(object sender, SelectionChangedEventArgs e)
  259. {
  260. UpdateEmailApplicationBar();
  261. }
  262. /// <summary>
  263. ///
  264. /// </summary>
  265. /// <param name="sender"></param>
  266. /// <param name="e"></param>
  267. private void OnEmailListIsSelectionEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
  268. {
  269. SetupEmailApplicationBar();
  270. }
  271. /// <summary>
  272. /// Tap on an item : depending on the selection state, either unselect it or consider it as read
  273. /// </summary>
  274. /// <param name="sender"></param>
  275. /// <param name="e"></param>
  276. private void OnItemContentTap(object sender, System.Windows.Input.GestureEventArgs e)
  277. {
  278. EmailObject item = ((FrameworkElement)sender).DataContext as EmailObject;
  279. if (item != null)
  280. {
  281. item.Unread = false;
  282. }
  283. }
  284. #endregion
  285. #region Buddies
  286. /// <summary>
  287. /// Setup the application bar for the Buddies Pivot items : simply hide it in this sample
  288. /// </summary>
  289. void SetupBuddiesApplicationBar()
  290. {
  291. ApplicationBar.IsVisible = false;
  292. buddies.IsSelectionEnabled = false;
  293. }
  294. /// <summary>
  295. /// Back Key Pressed = leaves the selection mode
  296. /// </summary>
  297. /// <param name="e"></param>
  298. protected void OnBuddiesBackKeyPressed(CancelEventArgs e)
  299. {
  300. if (buddies.IsSelectionEnabled)
  301. {
  302. buddies.IsSelectionEnabled = false;
  303. e.Cancel = true;
  304. }
  305. }
  306. #endregion
  307. #region Grid Mode
  308. ApplicationBarIconButton picturesEnableSelect;
  309. ApplicationBarIconButton picturesUpload;
  310. /// <summary>
  311. /// One time initialization for grid Pivot
  312. /// </summary>
  313. private void CreatePicturesApplicationBarItems()
  314. {
  315. picturesEnableSelect = new ApplicationBarIconButton();
  316. picturesEnableSelect.IconUri = new Uri("/Toolkit.Content/ApplicationBar.Select.png", UriKind.RelativeOrAbsolute);
  317. picturesEnableSelect.Text = "select";
  318. picturesEnableSelect.Click += OnPicturesEnableSelectClick;
  319. picturesUpload = new ApplicationBarIconButton();
  320. picturesUpload.IconUri = new Uri("/Toolkit.Content/ApplicationBar.Upload.png", UriKind.RelativeOrAbsolute);
  321. picturesUpload.Text = "upload";
  322. picturesUpload.Click += OnPicturesUploadClick;
  323. }
  324. /// <summary>
  325. /// Called when Picture pivot item is activated : makes sure that selection is disabled and updates the application bar
  326. /// </summary>
  327. private void OnPicturesPivotItemActivated()
  328. {
  329. if (GridSelector.IsSelectionEnabled)
  330. {
  331. GridSelector.IsSelectionEnabled = false; // Will also update the Application Bar
  332. }
  333. else
  334. {
  335. SetupPicturesApplicationBar();
  336. }
  337. }
  338. /// <summary>
  339. /// Setups the application bar for the Pivot
  340. /// </summary>
  341. private void SetupPicturesApplicationBar()
  342. {
  343. ClearApplicationBar();
  344. if (GridSelector.IsSelectionEnabled)
  345. {
  346. ApplicationBar.Buttons.Add(picturesUpload);
  347. UpdatePicturesApplicationBar();
  348. }
  349. else
  350. {
  351. ApplicationBar.Buttons.Add(picturesEnableSelect);
  352. }
  353. ApplicationBar.IsVisible = true;
  354. }
  355. /// <summary>
  356. /// Enables / disables the Upload button according to selection
  357. /// </summary>
  358. void UpdatePicturesApplicationBar()
  359. {
  360. picturesUpload.IsEnabled = ((GridSelector.SelectedItems != null) && (GridSelector.SelectedItems.Count > 0));
  361. }
  362. /// <summary>
  363. /// Opens the selection
  364. /// </summary>
  365. /// <param name="sender"></param>
  366. /// <param name="e"></param>
  367. void OnPicturesEnableSelectClick(object sender, EventArgs e)
  368. {
  369. GridSelector.EnforceIsSelectionEnabled = true;
  370. }
  371. /// <summary>
  372. /// Back Key Pressed = leaves the selection mode
  373. /// </summary>
  374. /// <param name="e"></param>
  375. protected void OnGridBackKeyPressed(CancelEventArgs e)
  376. {
  377. if (GridSelector.IsSelectionEnabled)
  378. {
  379. GridSelector.EnforceIsSelectionEnabled = false;
  380. e.Cancel = true;
  381. }
  382. }
  383. /// <summary>
  384. /// Updates the application bar when the selection is opened or closed
  385. /// </summary>
  386. /// <param name="sender"></param>
  387. /// <param name="e"></param>
  388. private void OnGridSelectorIsSelectionEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
  389. {
  390. SetupPicturesApplicationBar();
  391. }
  392. /// <summary>
  393. /// Simulates upload of the pictures
  394. /// </summary>
  395. /// <param name="sender"></param>
  396. /// <param name="e"></param>
  397. void OnPicturesUploadClick(object sender, EventArgs e)
  398. {
  399. StringBuilder builder = new StringBuilder("Uploading:\n");
  400. foreach (Picture picture in GridSelector.SelectedItems)
  401. {
  402. builder.AppendLine(picture.City);
  403. }
  404. MessageBox.Show(builder.ToString());
  405. }
  406. /// <summary>
  407. /// Updates the application bar when the picture selection changes
  408. /// </summary>
  409. /// <param name="sender"></param>
  410. /// <param name="e"></param>
  411. private void OnGridSelectorSelectionChanged(object sender, SelectionChangedEventArgs e)
  412. {
  413. UpdatePicturesApplicationBar();
  414. }
  415. /// <summary>
  416. /// Called when a picture is tapped : open it
  417. /// </summary>
  418. /// <param name="sender"></param>
  419. /// <param name="e"></param>
  420. private void OnPictureItemTap(object sender, System.Windows.Input.GestureEventArgs e)
  421. {
  422. FrameworkElement fe = sender as FrameworkElement;
  423. if (fe != null)
  424. {
  425. CurrentPicture = fe.DataContext as Picture;
  426. }
  427. }
  428. #endregion
  429. #region Picture display
  430. Picture _currentPicture = null;
  431. /// <summary>
  432. /// Sets or gets the current displayed picture
  433. /// </summary>
  434. public Picture CurrentPicture
  435. {
  436. set
  437. {
  438. _currentPicture = value;
  439. ZoomGrid.DataContext = _currentPicture;
  440. bool hasPicture = (_currentPicture != null);
  441. ZoomGrid.Visibility = hasPicture ? Visibility.Visible : Visibility.Collapsed;
  442. ApplicationBar.IsVisible = !hasPicture;
  443. SamplePivot.IsEnabled = !hasPicture;
  444. }
  445. get { return _currentPicture; }
  446. }
  447. /// <summary>
  448. /// Tap on the picture : hide it and revert to grid mode
  449. /// </summary>
  450. /// <param name="sender"></param>
  451. /// <param name="e"></param>
  452. private void OnZoomGridTap(object sender, System.Windows.Input.GestureEventArgs e)
  453. {
  454. CurrentPicture = null;
  455. }
  456. #endregion
  457. #region Databinding
  458. /// <summary>
  459. /// Hide the application bar
  460. /// </summary>
  461. void SetupBoundBuddiesApplicationBar()
  462. {
  463. ApplicationBar.IsVisible = false;
  464. }
  465. #endregion
  466. }
  467. }