Click here to Skip to main content
15,893,644 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 49.1K   572   11  
This article explains how to write unit tests for MVVM using Catel.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Catel.Windows.Controls"
                    xmlns:localconverters="clr-namespace:Catel.Windows.Data.Converters">

    <!-- Converters -->
    <localconverters:CountCollapsedConverter x:Key="CountCollapsedConverter"/>

	<!-- Brushes -->
	<SolidColorBrush x:Key="InfoBarMessageBackgroundBrush" Color="LightYellow" />
	<SolidColorBrush x:Key="InfoBarMessageSeparatorBrush" Color="Black" />
	<SolidColorBrush x:Key="InfoBarMessageErrorTextBackgroundBrush" Color="LightYellow" />
	<SolidColorBrush x:Key="InfoBarMessageErrorTextColorBrush" Color="Red" />
	<SolidColorBrush x:Key="InfoBarMessageWarningTextColorBrush" Color="Orange" />

	<!-- Style -->
	<Style TargetType="local:InfoBarMessageControl">
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="{x:Type local:InfoBarMessageControl}">
					<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
						
						<!-- Yellow message bar -->
						<DockPanel x:Name="messageDockPanel" LastChildFill="True">
							<StackPanel DockPanel.Dock="Top" Background="{StaticResource InfoBarMessageBackgroundBrush}"
                                  Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:InfoBarMessageControl}}, Path=MessageCount, Converter={StaticResource CountCollapsedConverter}}">
								<!-- Actual text -->
								<TextBlock Name="infoBarTextBlock" Padding="4,2,4,2"
                                   Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:InfoBarMessageControl}}, Path=InfoMessage}" />

								<!-- Line -->
								<Line Name="infoBarSeparator" Margin="0" DockPanel.Dock="Bottom" Stroke="{StaticResource InfoBarMessageSeparatorBrush}" 
                                      X1="0" X2="1" Stretch="UniformToFill" Width="{Binding ElementName=messageDockPanel, Path=ActualWidth}" />
							</StackPanel>

							<!-- Actual content -->
							<ContentPresenter />
						</DockPanel>

						<!-- Popup -->
						<Popup IsOpen="{Binding ElementName=infoBarTextBlock, Path=IsMouseOver, Mode=OneWay}" 
                               PlacementTarget="{Binding ElementName=infoBarSeparator}"
                               Placement="Bottom" 
							   Width="{Binding ElementName=messageDockPanel, Path=ActualWidth}"
                               AllowsTransparency="True" PopupAnimation="Fade">
							<Grid Background="{StaticResource InfoBarMessageErrorTextBackgroundBrush}"
								  DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:InfoBarMessageControl}}}">
								
								<!-- Column definitions -->
								<Grid.ColumnDefinitions>
									<ColumnDefinition Width="*" />
								</Grid.ColumnDefinitions>
								
								<!-- Row definitions -->
								<Grid.RowDefinitions>
									<RowDefinition Height="Auto" />
									<RowDefinition Height="Auto" />
									<RowDefinition Height="Auto" />
								</Grid.RowDefinitions>

								<!-- List of errors -->
								<ItemsControl Grid.Row="0" x:Name="errorMessagesItemsControl" ItemsSource="{Binding ErrorMessageCollection}" Height="Auto" VerticalAlignment="Top"
											  Visibility="{Binding ElementName=errorMessagesItemsControl, Path=Items.Count, Converter={StaticResource CountCollapsedConverter}}">
									<ItemsControl.ItemTemplate>
										<DataTemplate>
											<Grid>
												<!-- Column definitions -->
												<Grid.ColumnDefinitions>
													<ColumnDefinition Width="Auto" />
													<ColumnDefinition Width="*" />
												</Grid.ColumnDefinitions>
												
												<!-- Content -->
                                                <Image Grid.Column="0" Source="/Catel.Windows;component/Resources/Images/Error.png" Margin="2,2,4,2" Width="16" Height="16"/>
                                                <TextBlock Grid.Column="1" Foreground="{StaticResource InfoBarMessageErrorTextColorBrush}" Text="{Binding}" TextWrapping="Wrap" />
											</Grid>
										</DataTemplate>
									</ItemsControl.ItemTemplate>
								</ItemsControl>

                                <!-- List of warnings -->
                                <ItemsControl Grid.Row="1" x:Name="warningMessagesItemsControl" ItemsSource="{Binding WarningMessageCollection}" Height="Auto" VerticalAlignment="Top" 
											  Visibility="{Binding ElementName=warningMessagesItemsControl, Path=Items.Count, Converter={StaticResource CountCollapsedConverter}}">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <!-- Column definitions -->
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="*" />
                                                </Grid.ColumnDefinitions>

                                                <!-- Content -->
                                                <Image Grid.Column="0" Source="/Catel.Windows;component/Resources/Images/Warning.png" Margin="2,2,4,2" Width="16" Height="16"/>
                                                <TextBlock Grid.Column="1" Foreground="{StaticResource InfoBarMessageWarningTextColorBrush}" Text="{Binding}" TextWrapping="Wrap" />
                                            </Grid>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>

                                <!-- Line -->
								<Line Grid.Row="2" Margin="0" Stroke="{StaticResource InfoBarMessageSeparatorBrush}" X1="0" X2="1" 
									  Stretch="UniformToFill" Width="{Binding ElementName=messageDockPanel, Path=ActualWidth}" />

							</Grid>
						</Popup>
						
					</Grid>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</ResourceDictionary>

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
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions