TOC

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

العناصر الأساسية:

The Label control

إن المكون Label، في أبسط أشكاله، مشابه جداً للمكون TextBlock الذي استخدمناه في مقال آخر. ستلاحظ فوراً بأن المكون Label لديه خاصية Content بدلاً من خاصية Text في المكون TextBlock. السبب هو أن Label يستطيع احتواء أي نوع من المكونات ضمنه بشكل مباشر، عوضاً عن إمكانية وضع النصوص text ضمنه. هذا المحتوى content قد يكون سلسلة محرفية string أيضاً كما سترى في هذا المثال الأول والبسيط جداً.

<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>

قد تلاحظ شيئاً آخر، وهي حقيقة وجود خاصية padding بسيطة للمكون Label، مما يسمح للنص بالابتعاد عن الزاوية اليسارية العليا Top Left بعض البكسلات. وهذا ليس موجوداً في المكون TextBlock حيث ينبغي تحديده يدوياً.

في حالة بسيطة مثل هذه، حيث أن المحتوى content هي ببساطة سلسلة نصية string، سيقوم المكون Label فعلياً بإنشاء TextBlock ضمنياً وعرض السلسلة النصية string فيها.

المكون Label مقابل المكون TextBlock

بالتالي لماذا نستخدم المكون Label إذاً؟ حسناً، هناك القليل من الاختلافات المهمة بين المكونين Label و TextBlock. تسمح TextBlock بعرض سلاسل نصية string فقط. بينما تسمح Label بالإضافة لذلك ما يلي:

  • تعيين الحدود border
  • عرض مكونات أخرى، مثلاً صورة....
  • استخدام محتوى لنموذج/قالب من خلال الخاصية ContentTemplate.
  • Use access keys to give focus to related controls

النقطة الأخيرة المهمة وهي فعلياً واحدة من الأسباب الرئيسية لاستخدام Label بدلاً عن TextBlock. كلما أردت عرض نصوص بسيطة فقط، ينبغي أن تستخدم TextBlock، فهي أخف وتحقق أداء أفضل من Label في معظم الحالات.

Label and Access keys (mnemonics)

In Windows and other operating systems as well, it's common practice that you can access controls in a dialog by holding down the [Alt] key and then pressing a character which corresponds to the control that you wish to access. The character to press will be highlighted when you hold down the [Alt] key. TextBlock controls doesn't support this functionality, but the Label does, so for control labels, the Label control is usually an excellent choice. Let's look at an example of it in action:

<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>

The screenshot shows our sample dialog as it looks when the Alt key is pressed. Try running it, holding down the [Alt] key and then pressing N and M. You will see how focus is moved between the two textboxes.

So, there's several new concepts here. First of all, we define the access key by placing an underscore (_) before the character. It doesn't have to be the first character, it can be before any of the characters in your label content. The common practice is to use the first character that's not already used as an access key for another control.

We use the Target property to connect the Label and the designated control. We use a standard WPF binding for this, using the ElementName property, all of which we will describe later on in this tutorial. The binding is based on the name of the control, so if you change this name, you will also have to remember to change the binding.

Using controls as Label content

As already mentioned, the Label control allows you to host other controls, while still keeping the other benefits. Let's try an example where we have both an image and a piece of text inside the Label, while also having an access key for each of the labels:

<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>

This is just an extended version of the previous example - instead of a simple text string, our Label will now host both and image and a piece of text (inside the AccessText control, which allows us to still use an access key for the label). Both controls are inside a horizontal StackPanel, since the Label, just like any other ContentControl derivate, can only host one direct child control.

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!