PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms 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//===================================================================================
17using System;
18using System.Collections.Generic;
19using System.Collections.ObjectModel;
20using System.ComponentModel;
21using System.Linq;
22using MVVM.Model;
23
24namespace MVVM.ViewModels
25{
26    /// <summary>
27    /// View model for multiple selection questions.
28    /// </summary>
29    /// <remarks>
30    /// The model for a multiple selection question expects a list of selections as its answer, but the desired 
31    /// View for this question consists of a series of check boxes, one for each option. In order to support this
32    /// UI, this view model is a hierarchical view model, exposing a collection of child view models representing 
33    /// each option. However, rather than mapping the checked state on each child view model to presence of an option
34    /// into the list of selections, the view model exposes the list of selections and expects the view to 
35    /// populate it appropriately, which the view does by using the 
36    /// <see cref="MVVM.Infrastructure.Behaviors.SynchronizeSelectedItems">SynchronizeSelectedItems</see> class.
37    /// </remarks>
38    public class MultipleSelectionQuestionViewModel : QuestionViewModel<MultipleSelectionQuestion>
39    {
40        private readonly ObservableCollection<string> selections;
41
42        public MultipleSelectionQuestionViewModel(MultipleSelectionQuestion question)
43            : base(PreAnswerQuestion(question))
44        {
45            this.selections = new ObservableCollection<string>();
46            this.selections.CollectionChanged += this.OnSelectionsChanged;
47        }
48
49        /// <summary>
50        /// Gets the list of selections.
51        /// </summary>
52        public IList<string> Selections
53        {
54            get { return this.selections; }
55        }
56
57        protected override void OnQuestionPropertyChanged(object sender, PropertyChangedEventArgs e)
58        {
59            if (e.PropertyName == "Response")
60            {
61                this.OnResponseChanged();
62            }
63        }
64
65        private static MultipleSelectionQuestion PreAnswerQuestion(MultipleSelectionQuestion question)
66        {
67            if (question != null && question.Response == null)
68            {
69                question.Response = new string[0];
70            }
71
72            return question;
73        }
74
75        /// <summary>
76        /// Overwrites the response value in the model, triggering validation and change updates.
77        /// </summary>
78        private void OnSelectionsChanged(object sender, EventArgs args)
79        {
80            this.Question.Response = this.selections.ToArray();
81        }
82    }
83}