Click here to Skip to main content
15,892,737 members
Articles / DevOps / Testing

Composite Application Reloaded

Rate me:
Please Sign up or sign in to vote.
4.88/5 (38 votes)
11 May 2011CPOL12 min read 118.1K   1.5K   95  
A much simpler composite application library.
<UserControl 
  x:Class="DemoApp.View.AllCustomersView"
	x:Name="root"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
  >
  <UserControl.Resources>
	<CollectionViewSource
	  x:Key="CustomerGroups" 
	  Source="{Binding Path=AllCustomers}"
	  >
	  <CollectionViewSource.GroupDescriptions>
		<PropertyGroupDescription PropertyName="Customer.IsCompany" />
	  </CollectionViewSource.GroupDescriptions>
	  <CollectionViewSource.SortDescriptions>
		<!-- 
		Sort descending by IsCompany so that the 'True' values appear first,
		which means that companies will always be listed before people.
		-->
		<scm:SortDescription PropertyName="Customer.IsCompany" Direction="Descending" />
		<scm:SortDescription PropertyName="DisplayName" Direction="Ascending" />
	  </CollectionViewSource.SortDescriptions>
	</CollectionViewSource>

	<GroupStyle x:Key="CustomerGroupStyle">
	  <GroupStyle.HeaderTemplate>
		<DataTemplate>
		  <TextBlock 
			x:Name="txt" 
			Background="{StaticResource Brush_HeaderBackground}"
			FontWeight="Bold"
			Foreground="White"
			Margin="1"
			Padding="4,2,0,2"
			Text="People" 
			/>
		  <DataTemplate.Triggers>
			<DataTrigger Binding="{Binding Path=Name}" Value="True">
			  <Setter TargetName="txt" Property="Text" Value="Companies" />
			</DataTrigger>
		  </DataTemplate.Triggers>
		</DataTemplate>
	  </GroupStyle.HeaderTemplate>
	</GroupStyle>

	<Style x:Key="CustomerItemStyle" TargetType="{x:Type ListViewItem}">
	  <!-- 
	  Stretch the content of each cell so that we can 
	  right-align text in the Total Sales column. 
	  -->
	  <Setter Property="HorizontalContentAlignment" Value="Stretch" />
	  <!-- 
	  Bind the IsSelected property of a ListViewItem to the 
	  IsSelected property of a CustomerViewModel object.
	  -->
	  <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
	  <Style.Triggers>
		<MultiTrigger>
		  <MultiTrigger.Conditions>
			<Condition Property="ItemsControl.AlternationIndex" Value="1" />
			<Condition Property="IsSelected" Value="False" />
			<Condition Property="IsMouseOver" Value="False" />
		  </MultiTrigger.Conditions>
		  <Setter Property="Background" Value="#EEEEEEEE" />
		</MultiTrigger>
	  </Style.Triggers>
	</Style>
  </UserControl.Resources>

  <DockPanel>
	<Grid DockPanel.Dock="Bottom" Margin="0,2,4,2">
	  <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" VerticalAlignment="Center">
		<TextBlock Text="Total selected sales: " />
		<ContentPresenter
		  Content="{Binding Path=TotalSelectedSales}"
		  ContentStringFormat="c"
		  />
	  </StackPanel>
	</Grid>

	<ListView 
	  AlternationCount="2" 
	  DataContext="{StaticResource CustomerGroups}" 
	  ItemContainerStyle="{StaticResource CustomerItemStyle}"
	  ItemsSource="{Binding}"
	  >
	  <ListView.GroupStyle>
		<StaticResourceExtension 
		  ResourceKey="CustomerGroupStyle" 
		  />
	  </ListView.GroupStyle>

	  <ListView.View>
		<GridView>
			
		  <GridViewColumn 
			Header="Name" 
			DisplayMemberBinding="{Binding Path=DisplayName}" 
			/>
			
		  <GridViewColumn 
			Header="E-mail" 
			DisplayMemberBinding="{Binding Path=Customer.Email}" 
			/>
			
		  <GridViewColumn Header="Total Sales">
			<GridViewColumn.CellTemplate>
			  <DataTemplate>
				<ContentPresenter 
				  Content="{Binding Path=Customer.TotalSales}" 
				  ContentStringFormat="c"
				  HorizontalAlignment="Right"
				  />
			  </DataTemplate>
			</GridViewColumn.CellTemplate>
		  </GridViewColumn>

		<GridViewColumn Header="Edit">
			<GridViewColumn.CellTemplate>
				<DataTemplate>
					<Button
						Content="Edit"
						Command="{Binding DataContext.EditCommand, ElementName=root}"
						CommandParameter="{Binding}"
						/>
				</DataTemplate>
			</GridViewColumn.CellTemplate>
		</GridViewColumn>

				</GridView>
	  </ListView.View>
	</ListView>
  </DockPanel>
</UserControl>

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior) http://www.ansibleww.com.au
Australia Australia
The Australia born French man who went back to Australia later in life...
Finally got over life long (and mostly hopeless usually, yay!) chronic sicknesses.
Worked in Sydney, Brisbane, Darwin, Billinudgel, Darwin and Melbourne.

Comments and Discussions