TOC

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

WPF aplikacija:

The Window

Kada pravite WPF aplikaciju, prva stvar sa kojom ćete se susresti jeste Window klasa. Ona služi kao osnova prozora i pruža vam standardni okvir, naslovnu traku, i dugmad za uvećanje, smanjenje, kao i dugme za zatvaranje aplikacije. WPF prozor je kombinacija XAML (.xaml) fajla, gdje je <Window> element korijen, a CodeBehind (.cs) fajl. Ukoliko koristite Visual Studio (Express) i kreirate novu WPF aplikaciju, ona će onda kreirati podrazumijevani prozor za vas, koji će otprilike izgledati ovako:

<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 atribut govori XAML fajlu koju klasu da koristi, u ovom slučaju to je Window1, koju je Visual Studio takođe sam kreirao za nas. Možete je pronaći u stablu projekta u VS-u, kao podgranak čvora XAML fajla. Podrazumijevano, to otprilike izgleda ovako:

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

Kao što možete vidjeti, klasa Window1 je definisana kao djelimična, jer je iskombinovana sa vašom XAML datotekom tokom vremena izvršenja kako bi vam pružila potpunu prozorsku aplikaciju. Ovo je zapravo ono što poziv funkcije InitializeComponent() radi, što je razlog zašto je neophodan da bi ste dobili funkcionalan prozor.

Ukoliko se vratimo nazad na XAML fajl, primijetićete još nekoliko interesantnih atributa na elementu Window, kao što je Title, koji definiše naslov prozora (prikazanog na naslovnoj liniji) kao i početne dimenzije širine i visine. Takođe, postoji nekoliko definicija imenskog prostora (namespace-a) o kojima ćemo govoriti u poglavljima o XAML-u.

Takođe, primijetićete da je Visual Studio unutar prozora kreirao koordinatnu mrežu, odnosno Grid. Grid je jedna od WPF panela, i iako bi to mogao biti bilo koji panel ili kontrola, prozor može imati SAMO JEDNU pod kontrolu, pa je Panel, koji može sadržati više pod kontrola, uobičajeno dobar izbor. U ovom tutorijalu ćemo kasnije detaljnije pogledati različite vrste panela koje možete koristiti, jer su vrlo važni u WPF-u.

Važna svojstva prozora

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.