TOC

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

UserControls & CustomControls:

Introduction

Zatím jsme se v tomto průvodci věnovali pouze prvkům uživatelského rozhraní, které jsou součástí knihoven WPF. S nimi si možná vystačíte velmi dlouho, protože jsou velmi flexibilní a mohou být upraveny pomocí stylů a šablon pro téměř libovolné využití. Přesto se nejspíš dostanete do situace, kdy Vám tvorba vlastních prvků může značně zjednodušit práci. V jiných knihovnách pro tvorbu uživatelských rozhraní to často bývá obtížné, nicméně WPF tento proces usnadňuje a nabízí dvě možné cesty: Uživatelské ovládací prvky (User Control) a Vlastní ovládací prvky (Custom Control).

UserControls

Uživatelské ovládací prvky WPF dědí z třídy UserControl a chovají se velmi podobně jak aplikační okna ve WPF. Skládají se ze XAML souboru a souboru s vlastním kódem. V XAML souboru můžete přidávat existující WPF ovládací prvky a přidáním vlastního kódu lze dosáhnout implementace dodatečných funkcí. WPF Vám pak umožní vkládat takovéto soubory funkcí na jiných místech, čímž snadno dosáhnete sbalení funkcionality do znovupoužitelných prvků kdekoliv v rámci Vaší aplikace.

Custom controls

A Custom control is more low-level than a UserControl. When you create a Custom control, you inherit from an existing class, based on how deep you need to go. In many cases, you can inherit the Control class, which other WPF controls inherits from (e.g. the TextBox), but if you need to go even deeper, you can inherit the FrameworkElement or even the UIElement. The deeper you go, the more control you get and the less functionality is inherited.

The look of the Custom control is usually controlled through styles in a theme file, while the look of the User control will follow the look of the rest of the application. That also highlights one of the major differences between a UserControl and a Custom control: The Custom control can be styled/templated, while a UserControl can't.

Summary

Creating re-usable controls in WPF is very easy, especially if you take the UserControl approach. In the next article, we'll look into just how easy it is to create a UserControl and then use it in your own application.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!