|
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using ModelLib;
using MvvmFoundation.Wpf;
namespace TransformingModelToVM
{
public class CommunityViewModel : ObservableObject
{
#region Methods
public CommunityViewModel()
{
_memberCommandsInternal = new ObservableCollection<CommandViewModel>();
this.MemberCommands = new ReadOnlyObservableCollection<CommandViewModel>(_memberCommandsInternal);
// Create the command that is used by each CommandViewModel in MemberCommands.
_selectMemberCommand = new RelayCommand<Person>(this.SelectMember);
// Create the data source and begin a call to fetch and transform Person objects.
var dataSource = new PersonDataSource();
dataSource.RetrieveAndTransformAsync(
_memberCommandsInternal, // output
this.CreateCommandForPerson, // transform
() => this.IsLoadingData = false // onCompleted
);
this.IsLoadingData = true;
}
void SelectMember(Person person)
{
// In a real app this method would do something with the selected Person.
string msg = String.Format("You selected '{0}'.", FormatName(person));
MessageBox.Show(msg, "Congratulations!");
}
CommandViewModel CreateCommandForPerson(Person person)
{
return new CommandViewModel(_selectMemberCommand, FormatName(person), person);
}
static string FormatName(Person person)
{
return String.Format("{0}, {1}", person.LastName, person.FirstName);
}
#endregion // Methods
#region Properties
/// <summary>
/// Returns true if the community member data is
/// currently being loaded from a data source.
/// </summary>
public bool IsLoadingData
{
get { return _isLoadingData; }
private set
{
if (value.Equals(_isLoadingData))
return;
_isLoadingData = value;
base.RaisePropertyChanged("IsLoadingData");
}
}
/// <summary>
/// Returns a read-only collection of command objects, each of which represents a
/// member of the community. When a command executes, it 'selects' the associated member.
/// </summary>
public ReadOnlyObservableCollection<CommandViewModel> MemberCommands { get; private set; }
#endregion // Properties
#region Fields
bool _isLoadingData;
readonly ObservableCollection<CommandViewModel> _memberCommandsInternal;
readonly ICommand _selectMemberCommand;
#endregion // Fields
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.