当前位置:   article > 正文

【WPF】管理系统主界面_wpf 管理系统

wpf 管理系统

在这里插入图片描述
前台代码

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189

后台代码

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;
        }

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/825886
推荐阅读
相关标签
  

闽ICP备14008679号