ExpenseIt Demo

This sample application is often used in talks and laboratory exercises to introduce the core Windows Presentation Foundation (WPF) technologies to developers. It demonstrates XBAPs, Navigation, Layout, Data Binding, Controls, and Styles.

C# (149.7 KB)
 
 
 
 
 
(0)
4,450 times
Add to favorites
2/1/2011
E-mail Twitter del.icio.us Digg Facebook
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="HomePage.xaml"
             x:Class="ExpenseIt.App"
             xmlns:l="clr-namespace:Editing"
             xmlns:MappingPIGen1="clr-namespace:ExpenseIt"
             xmlns:localValidation="clr-namespace:ExpenseIt.Validation"
             >
  <Application.Resources>
    <ExpenseReport x:Key="ExpenseData"
                   xmlns="clr-namespace:ExpenseIt"
                   Alias="Someone@example.com"
                   EmployeeNumber="57304"
                   CostCenter="4032">
      <ExpenseReport.LineItems>
        <LineItem Type="Meal"
                  Description="Mexican Lunch"
                  Cost="12" />
        <LineItem Type="Meal"
                  Description="Italian Dinner"
                  Cost="45" />
        <LineItem Type="Education"
                  Description="Developer Conference"
                  Cost="90" />
        <LineItem Type="Travel"
                  Description="Taxi"
                  Cost="70" />
        <LineItem Type="Travel"
                  Description="Hotel"
                  Cost="60" />
      </ExpenseReport.LineItems>
    </ExpenseReport>
    <XmlDataProvider x:Key="CostCenters"
                     XPath="/CostCenters/*">
      <x:XData>
        <CostCenters xmlns="">
          <CostCenter Number="4032"
                      Name="Sales" />
          <CostCenter Number="4034"
                      Name="Marketing" />
          <CostCenter Number="5061"
                      Name="Human Resources" />
          <CostCenter Number="5062"
                      Name="Research and Development" />
        </CostCenters>
      </x:XData>
    </XmlDataProvider>
    <XmlDataProvider x:Key="Employees"
                     XPath="/Employees/*">
      <x:XData>
        <Employees xmlns="">
          <Employee Name="Terry Adams"
                    Type="FTE"
                    EmployeeNumber="1" />
          <Employee Name="Claire O&apos;Donnell"
                    Type="FTE"
                    EmployeeNumber="12345" />
          <Employee Name="Palle Peterson"
                    Type="FTE"
                    EmployeeNumber="5678" />
          <Employee Name="Amy E. Alberts"
                    Type="CSG"
                    EmployeeNumber="99222" />
          <Employee Name="Stefan Hesse"
                    Type="Vendor"
                    EmployeeNumber="-" />
        </Employees>
      </x:XData>
    </XmlDataProvider>
    <Style x:Key="ButtonInFlow"
       TargetType="{x:Type Button}">
      <Setter Property="Margin"
              Value="5" />
    </Style>
    <Style x:Key="Button"
   TargetType="{x:Type Button}">
      <Setter Property="Padding"
              Value="5" />
    </Style>
    <Style x:Key="EmployeeList"
           TargetType="{x:Type ListBox}">
      <Setter Property="Margin"
              Value="0,0,75,0" />
    </Style>
    <Style x:Key="HorizontalRadio"
           TargetType="{x:Type ListBoxItem}">
      <Setter Property="Margin"
              Value="10" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate>
            <RadioButton Focusable="false"
                         Content="{TemplateBinding ContentPresenter.Content}"
                         IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}" />
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    <Style x:Key="HorizontalRadioList"
           TargetType="{x:Type ListBox}">
      <Setter Property="IsTabStop"
              Value="False" />
      <Setter Property="Grid.Column"
              Value="1" />
      <Setter Property="Grid.Row"
              Value="4" />
      <Setter Property="Height"
              Value="40" />
      <Setter Property="Margin"
              Value="10" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ListBox}">
            <StackPanel KeyboardNavigation.TabNavigation="Once"
                        IsItemsHost="True"
                        Orientation="Horizontal" />
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <Style x:Key="InputText"
           TargetType="{x:Type TextBox}">
      <Setter Property="Height"
              Value="25px" />
      <Setter Property="FontFamily"
              Value="Trebuchet MS" />
      <Setter Property="Foreground"
              Value="#0066CC" />
      <Setter Property="FontSize"
              Value="10pt" />
      <Setter Property="Margin"
              Value="10,10,20,10" />
      <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
          <Setter Property="ToolTip"
            Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
        <Trigger Property="Validation.HasError" Value="false">
          <Setter Property="ToolTip"
            Value="{Binding RelativeSource={RelativeSource Self}, Path=ToolTip.Content}"/>
        </Trigger>
      </Style.Triggers>
    </Style>

    <Style x:Key="Label"
           TargetType="{x:Type Label}">
      <Setter Property="Foreground"
              Value="DarkSlateBlue" />
      <Setter Property="FontSize"
              Value="10pt" />
      <Setter Property="FontFamily"
              Value="arial" />
      <Setter Property="FontWeight"
              Value="bold" />
      <Setter Property="Margin"
              Value="0,3,10,0" />
      <Setter Property="VerticalAlignment"
              Value="Center" />
      <Setter Property="HorizontalAlignment"
              Value="Right" />
    </Style>
    <Style x:Key="DisplayText"
           TargetType="{x:Type TextBlock}">
      <Setter Property="Foreground"
              Value="#4E87D4" />
      <Setter Property="FontSize"
              Value="14pt" />
      <Setter Property="FontFamily"
              Value="arial" />
      <Setter Property="FontWeight"
              Value="bold" />
      <Setter Property="Margin"
              Value="10,0,0,0" />
      <Setter Property="HorizontalAlignment"
              Value="left" />
      <Setter Property="VerticalAlignment"
              Value="center" />
    </Style>
    <Style x:Key="TitleText"
           TargetType="{x:Type TextBlock}">
      <Setter Property="FontSize"
              Value="22pt" />
      <Setter Property="Foreground"
              Value="#4E87D4" />
      <Setter Property="FontFamily"
              Value="Trebuchet MS" />
      <Setter Property="Margin"
              Value="120,5,5,5" />
      <Setter Property="Grid.Row"
              Value="0" />
      <Setter Property="Grid.Column"
              Value="0" />
      <Setter Property="Grid.ColumnSpan"
              Value="2" />
      <Setter Property="FontWeight"
              Value="bold" />
    </Style>
    <Style x:Key="TableLabel"
           TargetType="{x:Type TextBlock}">
      <Setter Property="Foreground"
              Value="white" />
      <Setter Property="VerticalAlignment"
              Value="Center" />
      <Setter Property="HorizontalAlignment"
              Value="Center" />
      <Setter Property="FontWeight"
              Value="bold" />
      <Setter Property="FontSize"
              Value="10pt" />
    </Style>
    <Style x:Key="TotalExpenses"
           TargetType="{x:Type TextBlock}">
      <Setter Property="FontSize"
              Value="18pt" />
      <Setter Property="Foreground"
              Value="White" />
      <Setter Property="Margin"
              Value="0,10,30,10" />
    </Style>
    <Style x:Key="TotalExpensesFlow"
           TargetType="{x:Type StackPanel}">
      <Setter Property="Orientation"
              Value="Horizontal" />
      <Setter Property="HorizontalAlignment"
              Value="Right" />
    </Style>
    <Style x:Key="TotalExpensesLabel"
           TargetType="{x:Type TextBlock}">
      <Setter Property="FontFamily"
              Value="Trebuchet MS" />
      <Setter Property="FontSize"
              Value="14pt" />
      <Setter Property="Margin"
              Value="10,10,0,10" />
      <Setter Property="Foreground"
              Value="White" />
    </Style>
    <Style x:Key="TotalRectangle"
           TargetType="{x:Type Rectangle}">
      <Setter Property="Stroke">
        <Setter.Value>
          <LinearGradientBrush StartPoint="0,0"
                               EndPoint="0,1">
            <GradientBrush.GradientStops>
              <GradientStopCollection>
                <GradientStop Color="#4E87D4"
                              Offset="0" />
                <GradientStop Color="#73B2F5"
                              Offset="1" />
              </GradientStopCollection>
            </GradientBrush.GradientStops>
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
      <Setter Property="StrokeThickness"
              Value="1" />
      <Setter Property="Fill">
        <Setter.Value>
          <LinearGradientBrush StartPoint="0,0"
                               EndPoint="0,1">
            <GradientBrush.GradientStops>
              <GradientStopCollection>
                <GradientStop Color="#73B2F5"
                              Offset="0" />
                <GradientStop Color="#4E87D4"
                              Offset="1" />
              </GradientStopCollection>
            </GradientBrush.GradientStops>
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
      <Setter Property="RadiusX"
              Value="10" />
      <Setter Property="RadiusY"
              Value="10" />
    </Style>
    <Style x:Key="WatermarkImage"
           TargetType="{x:Type Image}">
      <Setter Property="Source"
              Value="watermark.png" />
      <Setter Property="HorizontalAlignment"
              Value="left" />
      <Setter Property="VerticalAlignment"
              Value="top" />
      <Setter Property="Grid.Row"
              Value="0" />
      <Setter Property="Grid.RowSpan"
              Value="5" />
      <Setter Property="Grid.ColumnSpan"
              Value="2" />
      <Setter Property="Width"
              Value="230" />
      <Setter Property="Opacity"
              Value="0.5" />
    </Style>
    <DataTemplate x:Key="EmployeeItemTemplate">
      <TextBlock Text="{Binding XPath=@Name}" />
    </DataTemplate>

    <Style x:Key="ExpenseScroller"
           TargetType="{x:Type ItemsControl}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ItemsControl}">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
              <StackPanel Orientation="vertical"
                          IsItemsHost="true" />
            </ScrollViewer>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <DataTemplate x:Key="ExpenseTemplate">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="33*" />
          <ColumnDefinition Width="33*" />
          <ColumnDefinition Width="33*" />
        </Grid.ColumnDefinitions>
        <TextBox Text="{Binding Path=Type}"
                 Grid.Column="0" />
        <TextBox Text="{Binding Path=Description}"
                 Grid.Column="1" />
        <TextBox
                 Grid.Column="2" >
          <TextBox.Text>
            <Binding Path="Cost" UpdateSourceTrigger="PropertyChanged">
              <!-- SECURITY: Cost must be an int -->
              <Binding.ValidationRules>
                <localValidation:NumberValidationRule />
              </Binding.ValidationRules>
            </Binding>
          </TextBox.Text>
        </TextBox>

      </Grid>
    </DataTemplate>

    <DataTemplate x:Key="CostCenterTemplate">
      <TextBlock Text="{Binding XPath=@Name}" />
    </DataTemplate>
    <Style x:Key="ExpenseChart"
           TargetType="{x:Type ItemsControl}">
      <Setter Property="DataContext"
              Value="{DynamicResource ExpenseData}" />
      <Setter Property="ItemsSource"
              Value="{Binding Path=LineItems}" />
      <Setter Property="ItemTemplate"
              Value="{DynamicResource ExpenseChartBar}" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ItemsControl}">
            <Grid Width="{TemplateBinding Width}"
                  Height="{TemplateBinding Height}">
              <Rectangle Stroke="Black"
                         StrokeThickness="1">
                <Rectangle.Fill>
                  <LinearGradientBrush StartPoint="0,0"
                                       EndPoint="0,1">
                    <GradientBrush.GradientStops>
                      <GradientStopCollection>
                        <GradientStop Color="RoyalBlue"
                                      Offset="0" />
                        <GradientStop Color="#99ccff"
                                      Offset="1" />
                      </GradientStopCollection>
                    </GradientBrush.GradientStops>
                  </LinearGradientBrush>
                </Rectangle.Fill>
              </Rectangle>
              <Viewbox Margin="1"
                       Stretch="Uniform">
                <StackPanel Orientation="Horizontal"
                            IsItemsHost="True" />
              </Viewbox>
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    <DataTemplate x:Key="ExpenseChartBar">
      <Grid VerticalAlignment="Bottom"
            Width="40">
        <Grid.RowDefinitions>
          <RowDefinition />
          <RowDefinition Height="10" />
        </Grid.RowDefinitions>
        <Rectangle Grid.Row="0"
                   Height="{Binding Path=Cost}"
                   Margin="10,10,10,0"
                   Fill="#33000000"
                   RadiusX="2"
                   RadiusY="2">
          <Rectangle.RenderTransform>
            <TranslateTransform X="1"
                                Y="1" />
          </Rectangle.RenderTransform>
        </Rectangle>
        <Rectangle Grid.Row="0"
                   Height="{Binding Path=Cost}"
                   Margin="10,10,10,0"
                   RadiusX="2"
                   RadiusY="2"
                   Stroke="black"
                   StrokeThickness="0.5">
          <Rectangle.Fill>
            <RadialGradientBrush>
              <GradientBrush.GradientStops>
                <GradientStopCollection>
                  <GradientStop Color="LimeGreen"
                                Offset="0" />
                  <GradientStop Color="DarkGreen"
                                Offset="1" />
                </GradientStopCollection>
              </GradientBrush.GradientStops>
            </RadialGradientBrush>
          </Rectangle.Fill>
        </Rectangle>
        <Rectangle Grid.Row="0"
                   Height="{Binding Path=Cost}"
                   Margin="11,12,11,0"
                   RadiusX="1"
                   RadiusY="1">
          <Rectangle.Fill>
            <LinearGradientBrush StartPoint="0,0"
                                 EndPoint="0,1">
              <GradientBrush.GradientStops>
                <GradientStopCollection>
                  <GradientStop Color="#aaffffff"
                                Offset="0" />
                  <GradientStop Color="transparent"
                                Offset="1" />
                </GradientStopCollection>
              </GradientBrush.GradientStops>
            </LinearGradientBrush>
          </Rectangle.Fill>
        </Rectangle>
        <Viewbox Grid.Row="1"
                 Width="30"
                 Margin="2">
          <TextBlock Grid.Row="1"
                     Width="100"
                     FontSize="10pt"
                     FontFamily="Arial"
                     TextAlignment="center"
                     TextTrimming="WordEllipsis"
                     Text="{Binding Path=Description}" />
        </Viewbox>
        <TextBlock Margin="0,5,0,3"
                   FontSize="4pt"
                   FontFamily="Arial"
                   TextAlignment="center"
                   Foreground="white"
                   Text="{Binding Path=Cost}" />
      </Grid>
    </DataTemplate>
  </Application.Resources>
</Application>