TOC

This article is currently in the process of being translated into Uzbek (~20% done).

A WPF application:

The Window

WPF ilovani yasashni boshlaganizda birinchi uchratadiganingiz Window klassi bo'ladi. U asosiy oynaning asosi bo'lib xizmat qiladi va oyna chegarasi, title bar, kichiklishtiruvchi, kattalashtiruvchi, yopuvchi tugmalardan iborat bo'ladi. WPF oynasi <Window> elementi XAML fayli bo'lgan asosdan va uning ortida turuvchi kod *.cs fayli birikmasidan iborat. Agar WPF ilovani yasashda Visual Studio(Express) ishlatayotgan bo'lsangiz, sizga oldindan avtomatik ravishda quyidagicha ko'rinishdagi oynani yasab beradi:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>

    </Grid>
</Window>

x:class atributi XAML fayliga qaysi klassni ishlatishni belgilaydi, bizni holda u Window1 va buni ham Visual Studio avtomatik yasab berdi. Window1 klassini loyihangiz fayllar ro'yxatidan XAML fayli ichidan topishingiz mumkin. Odatiy holda u quyidagicha ko'rinishda bo'ladi:

using System;
using System.Windows;
using System.Windows.Controls;
//…more using statements

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}

Ko'rib turganingizdek Window1 klassi partial tip sifatida e'lon qilingan, chunki kompilyatsiya vaqtida kod turgan file va XAML fayli birlashtirilib, sizga bir butun oyna ko'rinishini beradi. Aslida bu ishlarni InitializeComponent() ni chaqirilganda qilinadi, shuning uchun to'la funksiyali oyna ishlab turishi shart qilingan

If we go back to the XAML file, you will notice a couple of other interesting attributes on the Window element, like Title, which defines the title of the window (shown in the title bar) as well as the starting width and height. There are also a couple of namespace definitions, which we will talk about in the XAML chapters.

You will also notice that Visual Studio has created a Grid control for us inside the Window. The Grid is one of the WPF panels, and while it could be any panel or control, the Window can only have ONE child control, so a Panel, which in turn can contain multiple child controls, is usually a good choice. Later in this tutorial, we will have a much closer look into the different types of panels that you can use, as they are very important in WPF.

Important Window properties

The WPF Window class has a bunch of interesting attributes that you may set to control the look and behavior of your application window. Here's a short list of the most interesting ones:

Icon - Allows you to define the icon of the window, which is usually shown in the upper left corner, to the left of the window title.

ResizeMode - This controls whether and how the end-user can resize your window. The default is CanResize, which allows the user to resize the window like any other window, either by using the maximize/minimize buttons or by dragging one of the edges. CanMinimize will allow the user to minimize the window, but not to maximize it or drag it bigger or smaller. NoResize is the strictest one, where the maximize and minimize buttons are removed and the window can't be dragged bigger or smaller.

ShowInTaskbar - The default is true, but if you set it to false, your window won't be represented in the Windows taskbar. Useful for non-primary windows or for applications that should minimize to the tray.

SizeToContent - Decide if the Window should resize itself to automatically fit its content. The default is Manual, which means that the window doesn't automatically resize. Other options are Width, Height and WidthAndHeight, and each of them will automatically adjust the window size horizontally, vertically or both.

Topmost - The default is false, but if set to true, your Window will stay on top of other windows unless minimized. Only useful for special situations.

WindowStartupLocation - Controls the initial position of your window. The default is Manual, which means that the window will be initially positioned according to the Top and Left properties of your window. Other options are CenterOwner, which will position the window in the center of it's owner window, and CenterScreen, which will position the window in the center of the screen.

WindowState - Controls the initial window state. It can be either Normal, Maximized or Minimized. The default is Normal, which is what you should use unless you want your window to start either maximized or minimized.

There are lots of other attributes though, so have a look for yourself and then move on to the next chapter.