PageRenderTime 89ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/Quickstarts/MVVM/MVVM/ViewModels/MultipleSelectionQuestionViewModel.cs

#
C# | 83 lines | 42 code | 7 blank | 34 comment | 6 complexity | beee3369e95029d9a39848673e65b9bb MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Collections.ObjectModel;
  20. using System.ComponentModel;
  21. using System.Linq;
  22. using MVVM.Model;
  23. namespace MVVM.ViewModels
  24. {
  25. /// <summary>
  26. /// View model for multiple selection questions.
  27. /// </summary>
  28. /// <remarks>
  29. /// The model for a multiple selection question expects a list of selections as its answer, but the desired
  30. /// View for this question consists of a series of check boxes, one for each option. In order to support this
  31. /// UI, this view model is a hierarchical view model, exposing a collection of child view models representing
  32. /// each option. However, rather than mapping the checked state on each child view model to presence of an option
  33. /// into the list of selections, the view model exposes the list of selections and expects the view to
  34. /// populate it appropriately, which the view does by using the
  35. /// <see cref="MVVM.Infrastructure.Behaviors.SynchronizeSelectedItems">SynchronizeSelectedItems</see> class.
  36. /// </remarks>
  37. public class MultipleSelectionQuestionViewModel : QuestionViewModel<MultipleSelectionQuestion>
  38. {
  39. private readonly ObservableCollection<string> selections;
  40. public MultipleSelectionQuestionViewModel(MultipleSelectionQuestion question)
  41. : base(PreAnswerQuestion(question))
  42. {
  43. this.selections = new ObservableCollection<string>();
  44. this.selections.CollectionChanged += this.OnSelectionsChanged;
  45. }
  46. /// <summary>
  47. /// Gets the list of selections.
  48. /// </summary>
  49. public IList<string> Selections
  50. {
  51. get { return this.selections; }
  52. }
  53. protected override void OnQuestionPropertyChanged(object sender, PropertyChangedEventArgs e)
  54. {
  55. if (e.PropertyName == "Response")
  56. {
  57. this.OnResponseChanged();
  58. }
  59. }
  60. private static MultipleSelectionQuestion PreAnswerQuestion(MultipleSelectionQuestion question)
  61. {
  62. if (question != null && question.Response == null)
  63. {
  64. question.Response = new string[0];
  65. }
  66. return question;
  67. }
  68. /// <summary>
  69. /// Overwrites the response value in the model, triggering validation and change updates.
  70. /// </summary>
  71. private void OnSelectionsChanged(object sender, EventArgs args)
  72. {
  73. this.Question.Response = this.selections.ToArray();
  74. }
  75. }
  76. }