前台代码
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ExpenseItDemo"
xmlns:localValidation="clr-namespace:ExpenseItDemo.Validation"
x:Class="ExpenseItDemo.MainWindow"
DataContext="{StaticResource ExpenseData}"
Title="ExpenseIt Standalone"
MinWidth="480" MinHeight="260"
SizeToContent="WidthAndHeight"
Icon="Watermark.png"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<DataTemplate x:Key="EmployeeItemTemplate">
<TextBlock Text="{Binding XPath=@Name}" />
</DataTemplate>
<DataTemplate x:Key="CostCenterTemplate">
<TextBlock Text="{Binding XPath=@Name}" />
</DataTemplate>
</Window.Resources>
<Grid>
<!-- Watermark -->
<Image Style="{StaticResource WatermarkImage}" />
<DockPanel>
<!-- Menu Bar-->
<Menu AutomationProperties.Name="Expense It Demo" DockPanel.Dock="Top">
<!-- File Menu-->
<MenuItem Header="_File">
<MenuItem Command="local:MainWindow.CreateExpenseReportCommand"/>
<Separator />
<MenuItem Command="local:MainWindow.ExitCommand"/>
</MenuItem>
<!-- Help Menu-->
<MenuItem Header="_Help">
<MenuItem Command="local:MainWindow.AboutCommand"/>
</MenuItem>
</Menu>
<!-- Data Entry -->
<Grid Style="{StaticResource WindowContentGrid}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Email Address -->
<Label Style="{StaticResource Label}" Target="{Binding ElementName=emailTextBox}" Grid.Column="0"
Grid.Row="0">
_Email:
</Label>
<TextBox Name="emailTextBox" AutomationProperties.Name="Email" Style="{StaticResource InputText}" Grid.Column="1" Grid.Row="0">
<TextBox.Text>
<Binding Path="Alias" UpdateSourceTrigger="PropertyChanged">
<!-- SECURITY: Email alias must be valid email address eg xxx@xxx.com -->
<Binding.ValidationRules>
<localValidation:EmailValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
<TextBox.ToolTip>Enter email.</TextBox.ToolTip>
</TextBox>
<!-- Employee Number -->
<Label Style="{StaticResource Label}" Target="{Binding ElementName=employeeNumberTextBox}"
Grid.Column="0" Grid.Row="1">
Employee _Number:
</Label>
<TextBox Name="employeeNumberTextBox" AutomationProperties.Name="Employee Number" Style="{StaticResource InputText}" Grid.Column="1" Grid.Row="1">
<TextBox.Text>
<Binding Path="EmployeeNumber" UpdateSourceTrigger="PropertyChanged">
<!-- SECURITY: EmployeeNumber must be an int and 5 digits long -->
<Binding.ValidationRules>
<localValidation:NumberValidationRule IsFixedLength="True" Length="5" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
<TextBox.ToolTip>Enter employee number.</TextBox.ToolTip>
</TextBox>
<!-- Cost Center -->
<Label Style="{StaticResource Label}" Target="{Binding ElementName=costCenterTextBox}" Grid.Column="0"
Grid.Row="2">
_Cost Center:
</Label>
<ComboBox Name="costCenterTextBox"
AutomationProperties.Name="Cost Center"
Style="{StaticResource CostCenterList}"
Grid.Column="1" Grid.Row="2"
ItemTemplate="{StaticResource CostCenterTemplate}"
ItemsSource="{Binding Source={StaticResource CostCenters}}"
SelectedValue="{Binding Path=CostCenter}"
SelectedValuePath="@Number">
<ComboBox.ToolTip>
<TextBlock>Choose cost center.</TextBlock>
</ComboBox.ToolTip>
<ComboBox.ItemContainerStyle>
<Style>
<Setter Property="AutomationProperties.Name" Value="{Binding XPath=@Name}" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
<!-- Employee Type List -->
<Label Style="{StaticResource Label}" Target="{Binding ElementName=employeeTypeRadioButtons}"
Grid.Column="0" Grid.Row="3">
E_mployees:
</Label>
<ListBox Name="employeeTypeRadioButtons" AutomationProperties.Name="Employees" Style="{StaticResource HorizontalRadioList}" Grid.Column="1"
Grid.Row="3">
<ListBoxItem Style="{StaticResource HorizontalRadio}">
FTE
<ListBoxItem.ToolTip>
<TextBlock>FTE employee type</TextBlock>
</ListBoxItem.ToolTip>
</ListBoxItem>
<ListBoxItem Style="{StaticResource HorizontalRadio}">
CSG
<ListBoxItem.ToolTip>
<TextBlock>CSG employee type</TextBlock>
</ListBoxItem.ToolTip>
</ListBoxItem>
<ListBoxItem Style="{StaticResource HorizontalRadio}">
Vendor
<ListBoxItem.ToolTip>
<TextBlock>Vendor employee type</TextBlock>
</ListBoxItem.ToolTip>
</ListBoxItem>
</ListBox>
<!-- Employee List -->
<ListBox Style="{StaticResource EmployeeList}"
Grid.Column="1" Grid.Row="4"
ItemTemplate="{StaticResource EmployeeItemTemplate}"
ItemsSource="{Binding Source={StaticResource Employees}}"
ItemContainerStyle="{StaticResource EmployeeListItemContainerStyle}"
AutomationProperties.Name="Employee Name">
<ListBox.ToolTip>
<TextBlock>Choose employee name.</TextBlock>
</ListBox.ToolTip>
</ListBox>
<!-- Create Expense Report -->
<Button Name="createExpenseReportButton" Style="{StaticResource CommandButton}" Grid.Column="1"
Grid.Row="5" Command="local:MainWindow.CreateExpenseReportCommand"
AutomationProperties.AcceleratorKey="Ctrl+Shift+C">
Create Expense _Report
<Button.ToolTip>
<TextBlock>Opens a dialog for creating expense report</TextBlock>
</Button.ToolTip>
</Button>
</Grid>
</DockPanel>
</Grid>
</Window>
后台
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace ExpenseItDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
static MainWindow()
{
// Define CreateExpenseReportCommand
CreateExpenseReportCommand = new RoutedUICommand("_Create Expense Report..." ,"CreateExpenseReport", typeof(MainWindow));
CreateExpenseReportCommand.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control | ModifierKeys.Shift));
// Define ExitCommand
ExitCommand = new RoutedUICommand("E_xit", "Exit", typeof(MainWindow));
// Define AboutCommand
AboutCommand = new RoutedUICommand("_About ExpenseIt Standalone", "About", typeof(MainWindow));
}
public MainWindow()
{
Initialized += MainWindow_Initialized;
InitializeComponent();
employeeTypeRadioButtons.SelectionChanged += employeeTypeRadioButtons_SelectionChanged;
// Bind CreateExpenseReportCommand
var commandBindingCreateExpenseReport = new CommandBinding(CreateExpenseReportCommand);
commandBindingCreateExpenseReport.Executed += commandBindingCreateExpenseReport_Executed;
CommandBindings.Add(commandBindingCreateExpenseReport);
// Bind ExitCommand
var commandBindingExitCommand = new CommandBinding(ExitCommand);
commandBindingExitCommand.Executed += commandBindingExitCommand_Executed;
CommandBindings.Add(commandBindingExitCommand);
// Bind AboutCommand
var commandBindingAboutCommand = new CommandBinding(AboutCommand);
commandBindingAboutCommand.Executed += commandBindingAboutCommand_Executed;
CommandBindings.Add(commandBindingAboutCommand);
}
private void MainWindow_Initialized(object sender, EventArgs e)
{
// Select the first employee type radio button
employeeTypeRadioButtons.SelectedIndex = 0;
RefreshEmployeeList();
}
private void commandBindingCreateExpenseReport_Executed(object sender, ExecutedRoutedEventArgs e)
{
var dlg = new CreateExpenseReportDialogBox {Owner = this};
dlg.ShowDialog();
}
private void commandBindingExitCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Close();
}
private void commandBindingAboutCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(
"ExpenseIt Standalone Sample Application, by the WPF SDK",
"ExpenseIt Standalone",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
private void employeeTypeRadioButtons_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
RefreshEmployeeList();
}
/// <summary>
/// Select the employees who have the employment type that is specified
/// by the currently checked employee type radio button
/// </summary>
private void RefreshEmployeeList()
{
var selectedItem = (ListBoxItem) employeeTypeRadioButtons.SelectedItem;
// Get employees data source
var employeesDataSrc = (XmlDataProvider) FindResource("Employees");
// Select the employees who have of the specified employment type
var query = string.Format(CultureInfo.InvariantCulture, "/Employees/Employee[@Type='{0}']",
selectedItem.Content);
employeesDataSrc.XPath = query;
// Apply the selection
employeesDataSrc.Refresh();
}
#region Commands
public static RoutedUICommand CreateExpenseReportCommand;
public static RoutedUICommand ExitCommand;
public static RoutedUICommand AboutCommand;
#endregion
}
}