מדריך Silverlight – פקדים בסיסיים: טקסט
ישנם מספר פקדים להצגה ועריכת טקסט, לכל אחד תכונות שונות. בפרק זה אנו סוקרים את החשובים ביניהם.
הפקד TextBlock
הפקד TextBlock נועד להצגה של טקסט למשתמש ללא אפשרות עריכה. התכונה החשובה ביותר של פקד היא היא התכונה Text שמאפשרת לקבוע את הטקסט להצגה.
דוגמא לשימוש בפקד TextBlock:
<UserControl x:Class="FirstSilverlightApplication3.MainPage"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
>:d="http://schemas.microsoft.com/expression/blend/2008"
>:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<TextBlock Text="Hello from text block" />
</Grid>
</UserControl>
והתוצאה:
הפקד TextBox
הפקד TextBox מאפשר לקבל טקסט מהמשתמש. גם בפקד זה התכונה החשובה ביותר נקראת Text. ההבדל המרכזי בין פקד זה לקודם הוא יכולת העריכה של הטקסט ע"י המשתמש.
תכונה מועילה נוספת של פקד TextBox היא MaxLength שמגבילה את כמות הטקסט שהמשתמש יכול להכניס. בנוסף לפקד יש אירוע TextChanged אשר נורה בכל פעם שהטקסט משתנה.
<UserControl x:Class="FirstSilverlightApplication3.MainPage"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
>:d="http://schemas.microsoft.com/expression/blend/2008"
>:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<StackPanel>
<TextBlock Text="Enter your name:" />
<TextBox Text="My name is.." />
</StackPanel>
</UserControl>
הפקד PasswordBox
הפקד PasswordBox הוא פקד דומה לפקד TextBox, אלא שהוא מותאם במיוחד לקבלת סיסמאות מהמשתמש. פקד זה יציג כוכביות (או כל סימן אחר) כאשר המשתמש יכניס אליו קלט. בנוסף מאחורי הקלעים הפקד שומר את המידע בצורה קצת יותר מאובטחת ע"י שימוש במחלקה SecureString
.להלן דוגמת שימוש בפקד PasswordBox:
<UserControl x:Class="FirstSilverlightApplication3.MainPage"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
>:d="http://schemas.microsoft.com/expression/blend/2008"
>:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<StackPanel>
<TextBlock Text="Enter your password:" />
<PasswordBox />
</StackPanel>
</UserControl>
הפקד RichTextBox
פקד RichTextBox הוא פקד נוסף המאפשר עריכת טקסט, אלא שבניגוד לפקד TextBox, פקד זה מאפשר שימוש בטקסט "עשיר", כולל שימוש בצבעים, Bold ועוד.
להלן דוגמא של שימוש בפקד להצגת טקסט עשיר. שימו לב לשימוש באובייקטים מסוג Run לצורך הגדרת מקטע טקסט בעל מאפיינים ייחודיים:
<UserControl x:Class="FirstSilverlightApplication3.MainPage"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
>:d="http://schemas.microsoft.com/expression/blend/2008"
>:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<StackPanel>
<TextBlock Text="Enter your rich text:" />
<RichTextBox HorizontalAlignment="Left"
Margin="10,12,0,0"
Name="contentBox"
VerticalAlignment="Top"
IsReadOnly="True">
<Paragraph FontFamily="Georgia"
FontSize="16"
TextAlignment="Justify">This photograph shows how
<Run Text=" to use "
FontStyle="Italic"
FontWeight="ExtraBold" /> a
<Run Text=" rich text box "
Foreground="Red"
FontWeight="ExtraBold" /> .
</Paragraph>
</RichTextBox>
</StackPanel>
</UserControl>
תגובות בפייסבוק