Click here to Skip to main content
13,352,408 members (35,988 online)
Click here to Skip to main content


75 bookmarked
Posted 24 Aug 2008

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.
<UserControl x:Class="HappyNomad.Examples.PresentationModel.GUI.Demo1"


		<Style x:Key="EditableContentControl" TargetType="{x:Type ContentControl}">
				<DataTemplate x:Key="defaultTemplate">
						<Button Name="editButton" Content="Edit" DockPanel.Dock="Right" Margin="2"/>
						<TextBlock Text="{Binding Path=DomainItem.Name}" Margin="2,3"/>
				<DataTemplate x:Key="editTextTemplate">
						<StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
							<Button Name="saveButton" Content="Save" Margin="2,2,1,2"/>
							<Button Name="cancelButton" Content="Cancel" Margin="1,2,2,2"/>
						<TextBox Text="{Binding Path=UnsavedName, UpdateSourceTrigger=PropertyChanged}" Margin="-3,0,0,0"
						  FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
			<Setter Property="ContentTemplate" Value="{StaticResource defaultTemplate}"/>
			<EventSetter Event="Button.Click" Handler="editableContentControl_ButtonClick"/>
		<DataTemplate DataType="{x:Type uimodel:Demo1ItemView}">
			<ContentControl Content="{Binding}" Style="{StaticResource EditableContentControl}"/>
		<!-- This Style binds a ListBoxItem to the Presentation Model: -->
		<Style TargetType="{x:Type ListBoxItem}">
			<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>


	<ListBox Name="sampleListBox" HorizontalContentAlignment="Stretch"/>


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.


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


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.)

You may also be interested in...

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