user4901669
user4901669

Reputation:

Programmatically creating WPF windows

I am just starting to learn C# (Coming from Java) and I prefer to develop my Windows manually, but all of the MSDN guides I have found involve using the XAML view. Where can I find a tutorial that explains how to do this manually?

Edit: it appears that this is not recommended, so how would I add logic such as a game loop or drawing while a certain condition is true?

Upvotes: 5

Views: 13670

Answers (3)

marsh-wiggle
marsh-wiggle

Reputation: 2803

It's not fun, but it's possible to build a Windows completely at runtime without XAML.

Here's a little example:

To be called with:

{
    CrisisWhatCrisis crisisWhatCrisis = new CrisisWhatCrisis();
    crisisWhatCrisis.ShowDialog();
}

enter image description here

The class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Test
{
    public class CrisisWhatCrisis : Window
    {
        public Grid RootGrid { get; private set; }

        public CrisisWhatCrisis()
        {
            this.WindowStyle = WindowStyle.ThreeDBorderWindow;

            this.RootGrid = new Grid()
            { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch };

            // Create a sqare grid with 20 pixel borders 
            this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });
            this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(200) }); 
            this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
            this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });

            this.RootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20) });
            this.RootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(200) });
            this.RootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20) });

            // Create a new Textbox and place it in the middle of the root grid
            TextBox TextBox_Test = new TextBox()
            { Text = "ABC", Background = Brushes.Yellow, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Top };

            Grid.SetColumn(TextBox_Test, 1);
            Grid.SetRow(TextBox_Test, 1);
            this.RootGrid.Children.Add(TextBox_Test);

            Grid GridForButtons = new Grid()
            { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch };

            Button Button_Close = new Button() { Content = "Close" };
            Button_Close.Click += Button_Close_Click;

            // Add the button to the grid which has one cell by default
            Grid.SetColumn(Button_Close, 0);
            Grid.SetRow(Button_Close, 0);
            GridForButtons.Children.Add(Button_Close);

            // add the button grid to the RootGrid
            Grid.SetRow(GridForButtons, 2);
            Grid.SetColumn(GridForButtons, 1);
            this.RootGrid.Children.Add(GridForButtons);

            // Add the RootGrid to the content of the window
            this.Content = this.RootGrid;

            // fit the window size to the size of the RootGrid
            this.SizeToContent = SizeToContent.WidthAndHeight;
        }

        private void Button_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

Upvotes: 3

Athari
Athari

Reputation: 34285

You would totally love Applications = Code + Markup by Charles Petzold. He starts with C# code and only later introduces XAML.

That being said, you shouldn't do that. XAML, even with all its verbosity, hides a lot of infrastracture, often in non-obvious ways. Trivial code like this:

<Control Foo="Text" FooExt.Bar="{Binding Text}" Grid.Column="0">

can become unreadable mess like this:

var ctl = new Control();
ctl.BeginInit();
ctl.Foo = "Text";
var prop = FooExt.BarProperty;
var binding = new Binding("Text") { Source = dataContext };
BindingOperations.SetBinding(ctl, prop, binding);
Grid.SetColumn(ctl, 0);
ctl.EndEnit();

You won't enjoy writing this code. WPF is designed to be used together with XAML.


WPF doesn't use "paint" events or similar, on the lowest level you "draw" something once, it's stored as a vector image and the framework takes care of redrawing it when necessary. On higher level, you add controls, primitives etc. and change their properties. Again, the framework takes care of updating the view. On even higher level, you just create models and rules for creating controls and let the framework do the rest.

WPF is quite different from "traditional" way of working with controls. You should read a good book or at least several thorough tutorials before asking questions, because you don't seem to understand the basics.

Upvotes: 7

serendip
serendip

Reputation: 56

Using C#, You can select WPF, WinForm. If you saw xaml, It's WPF. If you want to create window in code behind(not xaml), just do below.

MainWindow window = new MainWindow();
window.Show();

but I recommand to use xaml, because WPF give many useful methods. I recommend book that wpf unleashed by Adam Nathan

Upvotes: 0

Related Questions