赞
踩
前台代码
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="ExpenseItDemo.CreateExpenseReportDialogBox" xmlns:localValidation="clr-namespace:ExpenseItDemo.Validation" DataContext="{StaticResource ExpenseData}" Icon="Watermark.png" Title="Create Expense Report" MinWidth="600" MinHeight="500" SizeToContent="WidthAndHeight" ShowInTaskbar="False" WindowStartupLocation="CenterOwner"> <Grid> <!-- Watermark --> <Image Style="{StaticResource WatermarkImage}" /> <Grid Style="{StaticResource WindowContentGrid}"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="50" /> <RowDefinition /> <RowDefinition Height="50" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <!-- Report Details --> <Grid Grid.Row="0" Grid.ColumnSpan="3"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <!-- Alias --> <Label Style="{StaticResource Label}" Target="{Binding ElementName=aliasTextBox}" Grid.Column="2" Grid.Row="0"> Email _Alias: </Label> <TextBox Name="aliasTextBox" AutomationProperties.Name="Email Alias" Style="{StaticResource ReadOnlyText}" Grid.Column="3" Grid.Row="0" Text="{Binding Path=Alias}"> <TextBox.ToolTip> <TextBlock>email alias</TextBlock> </TextBox.ToolTip> </TextBox> <!-- Employee Number --> <Label Style="{StaticResource Label}" Target="{Binding ElementName=employeeNumberTextBox}" Grid.Column="2" Grid.Row="1"> Employee _Number: </Label> <TextBox Name="employeeNumberTextBox" AutomationProperties.Name="Employee Number" Style="{StaticResource ReadOnlyText}" Grid.Column="3" Grid.Row="1" Text="{Binding Path=EmployeeNumber}"> <TextBox.ToolTip> <TextBlock>employee number</TextBlock> </TextBox.ToolTip> </TextBox> <!-- Cost Center --> <Label Style="{StaticResource Label}" Target="{Binding ElementName=costCenterTextBox}" Grid.Column="2" Grid.Row="2"> _Cost Center: </Label> <TextBox Name="costCenterTextBox" AutomationProperties.Name="Cost Center" Style="{StaticResource ReadOnlyText}" Grid.Column="3" Grid.Row="2" Text="{Binding Path=CostCenter}"> <TextBox.ToolTip> <TextBlock>cost center</TextBlock> </TextBox.ToolTip> </TextBox> </Grid> <!-- Separator Rectangle --> <Rectangle Style="{StaticResource TopSeparatorRectangle}" Grid.Row="1" Grid.ColumnSpan="3" /> <!-- Function Buttons --> <StackPanel Grid.Row="2" Grid.Column="2" Grid.RowSpan="2"> <!-- Add Expense Button --> <Button Style="{StaticResource FunctionButton}" Click="addExpenseButton_Click" Name="addExpenseButton"> Add _Expense <Button.ToolTip> <TextBlock>add expense</TextBlock> </Button.ToolTip> </Button> <!-- View Chart Button--> <Button Style="{StaticResource FunctionButton}" Click="viewChartButton_Click" Name="viewExpenseButton" AutomationProperties.Name="View Chart"> <Button.ToolTip> <TextBlock>View chart</TextBlock> </Button.ToolTip> <DockPanel KeyboardNavigation.TabNavigation="None"> <Label DockPanel.Dock="Top" Target="{Binding ElementName=viewExpenseButton}" HorizontalAlignment="Center"> _View Chart </Label> <ItemsControl Style="{StaticResource ExpenseChartSmall}" /> </DockPanel> </Button> </StackPanel> <!-- Expense Report List --> <Rectangle Style="{StaticResource TotalRectangle}" Grid.Row="2" Grid.RowSpan="2" Grid.ColumnSpan="2" /> <DataGrid Style="{DynamicResource DataGridStyle}" Grid.RowSpan="2" Grid.Row="2" Grid.ColumnSpan="2" Width="Auto" ColumnWidth="*" ItemsSource="{Binding LineItems}" HeadersVisibility="Column" Name="expenseDataGrid1" AutoGenerateColumns="False" CanUserAddRows="False" AutomationProperties.Name="Expense Report"> <DataGrid.ToolTip> <TextBlock>Expense Report</TextBlock> </DataGrid.ToolTip> <DataGrid.Columns> <DataGridTextColumn Header="Expense type" Binding="{Binding Type}" CellStyle="{StaticResource LeftAlignedCell}" /> <DataGridTextColumn Header="Description" Binding="{Binding Description}" CellStyle="{StaticResource LeftAlignedCell}" /> <DataGridTextColumn Header="Cost" CellStyle="{StaticResource RightAlignedCell}" HeaderStyle="{StaticResource RightAlignedColumnHeader}"> <DataGridTextColumn.Binding> <Binding Path="Cost" UpdateSourceTrigger="PropertyChanged"> <!-- SECURITY: Cost must be an int --> <Binding.ValidationRules> <localValidation:NumberValidationRule /> </Binding.ValidationRules> </Binding> </DataGridTextColumn.Binding> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> <!-- Total Expenses --> <Rectangle Style="{StaticResource TotalRectangle}" Grid.Row="4" Grid.ColumnSpan="2" /> <StackPanel Style="{StaticResource TotalExpensesFlow}" Grid.Row="4" Grid.ColumnSpan="2"> <TextBlock Style="{StaticResource TotalExpenses}"> Total Expenses ($): <TextBlock.ToolTip> <TextBlock>Total expenses</TextBlock> </TextBlock.ToolTip> </TextBlock> <TextBlock Style="{StaticResource TotalExpenses}" Text="{Binding Path=TotalExpenses, UpdateSourceTrigger=PropertyChanged}" /> </StackPanel> <!-- Separator Rectangle --> <Rectangle Style="{StaticResource BottomSeparatorRectangle}" Grid.Row="5" Grid.ColumnSpan="3" /> <!-- Command Buttons --> <StackPanel Style="{StaticResource CommandButtonPanel}" Grid.Row="6" Grid.ColumnSpan="3"> <!-- Ok Button --> <Button Style="{StaticResource CommandButton}" Click="okButton_Click" IsDefault="True"> _OK <Button.ToolTip> <TextBlock>OK</TextBlock> </Button.ToolTip> </Button> <!-- Cancel Button --> <Button Style="{StaticResource CommandButton}" Click="cancelButton_Click" IsCancel="True"> _Cancel <Button.ToolTip> <TextBlock>Cancel</TextBlock> </Button.ToolTip> </Button> </StackPanel> </Grid> </Grid> </Window>
后台代码
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; namespace ExpenseItDemo { /// <summary> /// Interaction logic for CreateExpenseReportDialogBox.xaml /// </summary> public partial class CreateExpenseReportDialogBox : Window { public CreateExpenseReportDialogBox() { InitializeComponent(); } private void addExpenseButton_Click(object sender, RoutedEventArgs e) { var app = Application.Current; var expenseReport = (ExpenseReport) app.FindResource("ExpenseData"); expenseReport?.LineItems.Add(new LineItem()); DataGridRow row =null; // Dispatching this at loaded priority so the new row has been added before our code runs // Grab the last row in the datagrid and search up the visual tree to get the DataGridCellsPresenter for the first cell in the last row this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, new Action(() => { row = expenseDataGrid1.ItemContainerGenerator.ContainerFromIndex(expenseDataGrid1.Items.Count - 1) as DataGridRow; if (row != null) { expenseDataGrid1.SelectedItem = row.DataContext; DataGridCell cell = GetCell(expenseDataGrid1, row, 0); if (cell != null) { expenseDataGrid1.CurrentCell = new DataGridCellInfo(cell); Keyboard.Focus(cell); } } })); } private static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column) { if (rowContainer != null) { DataGridCellsPresenter presenter = RecursiveVisualChildFinder(rowContainer); if (presenter != null) return presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell; } return null; } private static DataGridCellsPresenter RecursiveVisualChildFinder(DependencyObject myObj) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myObj); i++) { // Retrieve child visual at specified index value. DependencyObject childObj = VisualTreeHelper.GetChild(myObj, i); // Do processing of the child visual object. if(childObj is DataGridCellsPresenter) { return (DataGridCellsPresenter)childObj; } // Enumerate children of the child visual object. return RecursiveVisualChildFinder(childObj); } return null; } private void viewChartButton_Click(object sender, RoutedEventArgs e) { var dlg = new ViewChartWindow {Owner = this}; dlg.Show(); } bool isValid(DependencyObject parent) { if (System.Windows.Controls.Validation.GetHasError(parent)) return false; for(int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); i++) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); if (!isValid(child)) return false; } return true; } private void okButton_Click(object sender, RoutedEventArgs e) { if(!isValid(expenseDataGrid1)){ MessageBox.Show( "Please, fix the errors.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { MessageBox.Show( "Expense Report Created!", "ExpenseIt Standalone", MessageBoxButton.OK, MessageBoxImage.Information); DialogResult = true; } } private void cancelButton_Click(object sender, RoutedEventArgs e) { DialogResult = false; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。