TOC

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

Basic controls:

The Label control

Eng sodda formalarda,Label controli,biz oldingi maqolalarda foydalangan TextBlockga juda o'xshaydi. Siz Labeldan foydalanganigizda Text property o'rniga Content property si borligini tezda anglab yetasiz.Buning sababi shundaki, Label shunchaki matn emas balki uning o'rniga har qanday control ni to'g'ridan-to'g'ri o'zining tarkibiga kiritishi mumkin.Uning tarkibi string toifasiga oid bulishi ham mumkin,shuning uchun siz hozir bunga doir eng sodda birinchi misolni ko'rishingiz mumkin.

<Window x:Class="WpfTutorialSamples.Basic_controls.LabelControlSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LabelControlSample" Height="100" Width="200">
    <Grid>
		<Label Content="This is a Label control." />
	</Grid>
</Window>

E'tibor qilishingiz mumkin bo'lgan yana bir narsa - bu Label-ning default holatida, Matnni tepa va chap burchaklardan bir necha piksel uzoqda ko'rsatishga ruxsat bergan paddingga ega ekanligidir.Bu jarayonni siz TextBlock controli da qila olmaysiz.Buning uchun alohida qo'lda kod orqali kiritishingizga to'g'ri keladi.

Contenti string toifasiga ega bulgan bunga o'xshash oddiy misollarda,Label aslini olganda o'z tarkibida TextBlockni yaratib oladi va sizga matnni ko'rsatib beradi.

Label va TextBlock kontrollari

Xo'sh, nima uchun Label kerak? TextBlock va Label orasida bir nechta muhim farqlar bor.TextBlock faqat matnni ko'rsatib berish vazifasiga ega bo'lsa,Label shuningdek quidagilarga ham ruxsat berish imkoniyatiga ega:

  • chegara(border) ni aniqlashga( yoki belgilashga)
  • Boshqa controllarga ham ruxsat beradi Masalan: Image(rasm) controliga
  • ContentTemplate property(xususiyati)si orqali template(shablonli) content(tarkib) idan foydalanish mumkin
  • Tegishli controllarga e'tiborni qaratish uchun kirish tugmalaridan foydalaning

Eng oxirgi nuqta,Labeldan foydalanishning eng asosiy sabablaridan biri bu,Labelni TextBlock o'rnida foydalish hisoblanadi.Agar siz doimi holatda matnni ko'rsatishni hohlasangiz,siz uchun TextBlockdan foydalanganligingiz afzalroq.Chunki TextBlock Labelga qaraganda ancha yengilroq va undan yaxshiroq ishlaydi

Label and Access keys (mnemonics)

Windows va boshqa operatsion tizimlarda ham [Alt] tugmachasini bosib ushlab turgandan so'ng, kerakli boshqaruv elementiga mos keladigan belgini bosish orqali dialog oynasidagi boshqaruv elementlariga kirish mumkinligi odatiy holdir. [Alt] tugmasini bosib ushab turganda bosish uchun kerak bo'ladigan belgi [Alt] tugmachasini bosib ushlab turganda bosilishi ta'kidlanadi.TextBlock controli (Esaltib o'tamiz! U faqat matn uchun)ushbu funktsiyani qo'llab-quvvatlamaydi, ammo Label qo'llab-quvvatlaydi, shuning uchun bunday holatlar uchun Label nazorati odatda juda yaxshi tanlovdir. Keling, uning misolini amalda ko'rib chiqamiz: highlight

<Window x:Class="WpfTutorialSamples.Basic_controls.LabelControlSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LabelControlSample" Height="180" Width="250">
	<StackPanel Margin="10">
		<Label Content="_Name:" Target="{Binding ElementName=txtName}" />
		<TextBox Name="txtName" />
		<Label Content="_Mail:" Target="{Binding ElementName=txtMail}" />
		<TextBox Name="txtMail" />
	</StackPanel>
</Window>

Skrinshot Alt klavishi bosilganda ko'rinadigan bizning namunaviy dialogimizni ko'rsatadi. [Alt] tugmachasini bosib ushlab turing va keyin N va M tugmachalarini bosib harakat qilib ko'ring, siz kursorning ikkala textbox o'rtasida qanday qilib harakatlanishini ko'rasiz.

Shunday qilib, bu erda bir nechta yangi tushunchalar mavjud. Avvalo, biz belgining oldiga pastki chiziq (_) qo'yish orqali kirish kalitini aniqlaymiz. Bu birinchi belgining oldidan qo'yilishi shart emas, bu sizning Label contentidagi har qanday belgidan oldin bo'lishi mumkin. Umumiy amaliyotimiz bunda boshqa boshqaruv uchun kirish kaliti sifatida ishlatilmagan birinchi belgidan foydalanish edi.

Label va belgilangan controlni ulash uchun biz Target propertysidan foydalanamiz. Buning uchun biz ElementName propertysidan foydalangan holda standart WPF majburiyligini ishlatamiz, bularning barchasini keyinchalik ushbu qo'llanmada tasvirlab beramiz. Binding control elementining nomiga asoslanadi, shuning uchun agar siz ushbu nomni o'zgartirsangiz, binding nomini ham o'zgartirishni unutmasligingiz kerak bo'ladi.

Using controls as Label content

Yuqorida aytib o'tilganidek, Label controli boshqa afzalliklarni saqlab qolish bilan birga boshqa controllarnini joylashtirishga imkon beradi.Keling,Label ichida rasm va matn parchasi bo'lgan, shuningdek har bir label uchun kirish kalitiga ega bo'lgan misolni sinab ko'raylik:

<Window x:Class="WpfTutorialSamples.Basic_controls.LabelControlAdvancedSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LabelControlAdvancedSample" Height="180" Width="250">
	<StackPanel Margin="10">
		<Label Target="{Binding ElementName=txtName}">
			<StackPanel Orientation="Horizontal">
				<Image Source="http://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_green.png" />
				<AccessText Text="_Name:" />
			</StackPanel>
		</Label>
		<TextBox Name="txtName" />
		<Label Target="{Binding ElementName=txtMail}">
			<StackPanel Orientation="Horizontal">
				<Image Source="http://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_blue.png" />
				<AccessText Text="_Mail:" />
			</StackPanel>
		</Label>
		<TextBox Name="txtMail" />
	</StackPanel>
</Window>

Bu avvalgi misolning kengaytirilgan versiyasidir - oddiy matn satrining o'rniga bizning Label endi rasm va matn (AccessText boshqaruvi ichidagi matn nazarda tutilmoqda, bu bizga yorliq uchun kirish kalitini ishlatishga imkon beradi) qismini joylashtiradi.

The Image control, described later in this tutorial, uses a remote image - this is ONLY for demonstrational purposes and is NOT a good idea for most real life applications.

Summary

In most situations, the Label control does exactly what the name implies: It acts as a text label for another control. This is the primary purpose of it. For most other cases, you should probably use a TextBlock control or one of the other text containers that WPF offers.


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!