Click here to Skip to main content
13,091,554 members (67,905 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.Demo2"


		<HierarchicalDataTemplate DataType="{x:Type uimodel:SampleTreeNodeView}" ItemsSource="{Binding Path=SubItems}">
			<TextBlock Text="{Binding Path=DomainItem.Name}"/>

		<!-- This Style binds a TreeViewItem to the Presentation Model: -->
		<Style TargetType="{x:Type TreeViewItem}">
			<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
			<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>



		<Grid DockPanel.Dock="Bottom" HorizontalAlignment="Center">
			<Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions>
			<TextBox Name="editItemBox" Margin="10"
			  Text="{Binding Path=DomainItem.Name, UpdateSourceTrigger=Explicit}"/>
			<WrapPanel Grid.Row="1">
				<Button Name="addButton" Content="Insert New" Click="addButton_Click" Padding="5,2" Margin="10,0,10,10"/>
				<Button Name="updateButton" Content="Update" Click="updateButton_Click" Padding="5,2" Margin="0,0,10,10"/>
				<Button Name="demoteButton" Content="Demote" Click="demoteButton_Click" Padding="5,2" Margin="0,0,10,10"/>
				<Button Name="deleteButton" Content="Delete" Click="deleteButton_Click" Padding="5,2" Margin="0,0,10,10"/>

		<TreeView Name="sampleTreeView"/> <!--ItemsSource is set in constructor-->

			<Binding ElementName="sampleTreeView" Path="SelectedItem"/>

			<Style TargetType="{x:Type Button}">
					<DataTrigger Binding="{Binding}" Value="{x:Null}">
						<Setter Property="IsEnabled" Value="False"/>



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
Web01 | 2.8.170813.1 | Last Updated 2 Apr 2011
Article Copyright 2008 by Adrian Alexander
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid