Example #1
0
        public MainWindow()
        {
            InitializeComponent();

            operations = new Dictionary <string, Func <double, double, double> >
            {
                { "+", (x, y) => x + y },
                { "-", (x, y) => x - y },
                { "*", (x, y) => x * y },
                { "/", (x, y) => x / y },
                { "^", Pow },
            };

            var digitsGrid = new Grid();

            int colSize = 3;

            for (int i = 0; i < colSize; i++)
            {
                digitsGrid.ColumnDefinitions.Add(new ColumnDefinition());
            }

            int digitBase = 10;
            int rowSize   = digitBase / colSize + 1;

            for (int i = 0; i < rowSize; i++)
            {
                digitsGrid.RowDefinitions.Add(new RowDefinition());
            }

            for (int i = 0; i < digitBase; i++)
            {
                var tmp = new Button
                {
                    Content    = i.ToString("D"),
                    FontFamily = new FontFamily("Monospace"),
                    FontSize   = 18
                };
                tmp.Click += DigitOnClick;
                tmp.AddTo(digitsGrid, i / colSize, i % colSize);
            }

            MainGrid.ColumnDefinitions.Add(new ColumnDefinition());
            MainGrid.ColumnDefinitions.Add(new ColumnDefinition {
                MinWidth = 50
            });
            MainGrid.RowDefinitions.Add(new RowDefinition());
            MainGrid.RowDefinitions.Add(new RowDefinition {
                MinHeight = 150
            });
            MainGrid.RowDefinitions.Add(new RowDefinition());


            digitsGrid.AddTo(MainGrid, row: 1, rowSpan: 2);

            var operationPanel = new StackPanel
            {
                Orientation         = Orientation.Vertical,
                VerticalAlignment   = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
            };

            foreach (var operation in operations)
            {
                var button = new Button
                {
                    Content    = operation.Key,
                    FontFamily = new FontFamily("Monospace"),
                    FontSize   = 18,
                    Width      = 50,
                };
                button.Click += OperationOnClick;
                button.AddTo(operationPanel);
            }

            operationPanel.AddTo(MainGrid, row: 1, column: 1);

            var result = new Button
            {
                Content    = "Result",
                FontFamily = new FontFamily("Monospace"),
            };

            result.Click += ResultOnClick;
            result.AddTo(MainGrid, row: 2, column: 1);
        }