Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Item-Level Presentation Models for WPF

, 2 Apr 2011
Make your life easier by inserting a Presentation Model layer (aka ViewModel) between your domain-model collection contents and template-generated WPF objects.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

using HappyNomad.Examples.PresentationModel.GUI.Model;

namespace HappyNomad.Examples.PresentationModel.GUI {

	/// <summary>
	/// Interaction logic for Demo3.xaml
	/// </summary>
	public partial class Demo3 : UserControl {
		private Demo3ItemView rootWrapper;
		
		public Demo3() {
			InitializeComponent();

			//Build sample presentation-model objects:
			rootWrapper = new Demo3ItemView();
			sampleTreeView.ItemsSource = rootWrapper.SubItems;

			representedByComboBox.ItemsSource = App.SampleListContainer.SampleList;
			ICollectionView cv = CollectionViewSource.GetDefaultView( App.SampleListContainer.SampleList );
			cv.Filter = delegate( object item ) { return item is Representative; };
		}

		private void addButton_Click( object sender, RoutedEventArgs args ) {
			Demo3ItemView selected = (Demo3ItemView)sampleTreeView.SelectedItem; //get selected item
			Represented newItem = new Represented( editItemBox.Text, //create new item
				(Representative)representedByComboBox.SelectedItem );

			//Insert new item into domain-model tree structure:
			App.SampleListContainer.SampleList.Add( newItem );

			//Select new item in UI:
			Demo3ItemView newItemView = rootWrapper.FindDescendant( newItem );
			newItemView.Parent.IsExpanded = true; //Make sure the new item is visible
			newItemView.IsSelected = true; //and that it's selected.
			sampleTreeView.Focus(); //avoid some strange WPF behaviour
		}

		private void updateButton_Click( object sender, RoutedEventArgs args ) {
			BindingExpression be = editItemBox.GetBindingExpression( TextBox.TextProperty );
			be.UpdateSource(); //update sampleTreeView.SelectedItem.DomainItem.Name with editItemBox.Text's value
			rootWrapper.UpdatePosition( ((Demo3ItemView)editItemBox.DataContext).DomainItem );
			sampleTreeView.Focus();
		}

		private void deleteButton_Click( object sender, RoutedEventArgs args ) {
			Demo3ItemView selected = (Demo3ItemView)sampleTreeView.SelectedItem; //get selected item
			App.SampleListContainer.SampleList.Remove( selected.DomainItem );
			sampleTreeView.Focus();
		}
	}

	[ValueConversion( typeof(Demo3ItemView), typeof(Boolean) )]
	public class ItemSelectedConverter : IValueConverter {
		public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) {
			return value != null;
		}

		public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) {
			return null;
		}
	}
}

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.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Adrian Alexander

United States United States
Adrian loves facilitating suave user experiences via the latest and greatest GUI technologies such as Windows 8 Metro-style apps as well as WPF. More generally, he finds joy in architecting software that is easy to comprehend and maintain. He does so by applying design patterns at the top-level, and by incessantly refactoring code at lower levels. He's always interested in hearing about opportunities for full or part-time development work. He resides in Pennsylvania but can potentially travel anywhere in the country. (Writing about himself in the third-person is Adrian's new hobby.)

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 2 Apr 2011
Article Copyright 2008 by Adrian Alexander
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid