Saturday, 12 April 2014

Label and TextBlock Control in WPF

Label and TextBlock Control in WPF
Label Control in WPF
The Label control, in its most simple form, will look very much like the TextBlock but difference Label has hold Content property, the TextBlock has hold Text property. The reason for that is that the Label can host any kind of control directly inside of it Like Image etc, instead of just text. This content can be a string as well
The TextBlock only allows you to render a text string, while the Label also allows you to:
§  Specify a border
§  Render other controls, e.g. an image
§  Use template content through the Content Template property
§  Use access keys to give focus to related controls
 For Example
<Window x:Class="WpfApplicationControls.LabelControlinWPF"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="LabelControlinWPF" Height="300" Width="300">

<StackPanel Margin="10,25,5,23">

<Label Content="Login Page in WPF" Margin="12,12,12,12" TextElement.FontFamily="Verdana" TextElement.FontSize="18" TextElement.FontStyle="Italic" />

<!—For EmailId-->

<Label Target="{Binding ElementName=txEmailId}">
<StackPanel Orientation="Horizontal">
<Image Source="/Image/images.jpg" Width="20" />
<AccessText Text="_EmailId:" />
</StackPanel>
</Label>

<TextBox Name="txEmailId" Margin="99,-25,0,0" />

<!—For Password-->

<Label Target="{Binding ElementName=txtPassword}">
<StackPanel Orientation="Horizontal">
<Image Source="Image/pas.jpg" Height="20" Width="22" />
<AccessText Text="_Password:" />
</StackPanel>
</Label>
<PasswordBox Name="txtPassword" PasswordChar="*" MaxLength="10" Margin="99,-25,0,0" />

<Button Content="Login" Margin="150,10,0,0" Width="100" Click="Button_Click_1"></Button>
</StackPanel>

</Window>

Result:


TextBlock Control in WPF
The TextBlock control is one of the most fundamental controls in WPF,  A common understanding is that a Label is for short, one-line texts (but may include e.g. an image), while the TextBlock works very well for multiline strings as well, but can only contain text (strings). Both the Label and the TextBlock offers their own unique advantages, so what you should use very much depends on the situation.

For Example :

<Window x:Class="WpfApplicationControls.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="400" Width="488.158">
<StackPanel Margin="0,0,2,0">
<!--apply common style on TextBlock-->
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="13"/>
</Style>
</StackPanel.Resources>

<!--TextBlock-->
<TextBlock Text="Hi I am learning Wpf with TextBlock Control"></TextBlock>

<!--TextBlock with Left Margin-->
<TextBlock Margin="20" Text="Hi I am learning Wpf with TextBlock Control"></TextBlock>

<!--TextBlock with LineBreak-->
<TextBlock Margin="10" Foreground="PaleGreen"> Hi I am learning Wpf with <LineBreak/>TextBlock Control</TextBlock>

<!--TextBlock with LineHeight-->
<TextBlock Text="This is a multiline text." LineHeight="25" LineStackingStrategy="BlockLineHeight" />

<!--TextBlock with TextTrimming -->
<TextBlock Margin="10" TextTrimming="CharacterEllipsis" Foreground="Green"> Hi I am learning Wpf with TextBlock Control This is a TextBlock control with text that may not be rendered completely, which will be indicated </TextBlock>

<!--TextBlock with TextWrapping-->
<TextBlock Margin="10" TextWrapping="Wrap" Foreground="Blue"> This is a TextBlock control with automatically wrapped text, using the TextWrapping property.</TextBlock>

<!--TextBlock with Bold, Italic and Underline Property-->
<TextBlock Margin="10" > TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> text.</TextBlock>

<!--TextBlock with HyperLink Property-->
<TextBlock>This is my blog link   <Hyperlink RequestNavigate="Hyperlink_RequestNavigate_1" NavigateUri="http://kushonline.blogspot.in/">link</Hyperlink> in it.</TextBlock>

<!--TextBlock with HyperLink Property with MainWindow.xaml.cs-->

private void Hyperlink_RequestNavigate_1(object sender, RequestNavigateEventArgs e)
{
System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);
}



<!--TextBlock with span Property-->
<TextBlock Margin="10" >
This
<Span FontWeight="Bold">is</Span>
a
<Span Background="Silver" Foreground="Maroon">My Blog Link</Span>
on
<Span TextDecorations="Underline"><Bold>Facebook</Bold></Span>
<Span FontStyle="Italic">url</Span>
is
<Span Foreground="Blue"><Italic>www.facebook.com/DotNetProgram</Italic></Span>.
</TextBlock>
</StackPanel>

</Window>

Result:



Formatting text from C#/Code-Behind
As you can see, formatting text through XAML is very easy, but in some cases, you might prefer or even need to do it from your C#/Code-Behind file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplicationControls
{
/// <summary>
/// Interaction logic for TextBlockControl.xaml
/// </summary>
public partial class TextBlockControl : Window
{
public TextBlockControl()
{
InitializeComponent();
LoadTextBlock();
}

private void LoadTextBlock()
{
TextBlock tb1 = new TextBlock();
tb1.TextWrapping = TextWrapping.Wrap;
tb1.Margin = new Thickness(15);
tb1.Inlines.Add("An Example with ");
tb1.Inlines.Add(new Run("the TextBlock control ") { FontWeight = FontWeights.DemiBold});
tb1.Inlines.Add("using ");
tb1.Inlines.Add(new Run("inline ") { FontStyle = FontStyles.Normal });
tb1.Inlines.Add(new Run("text formatting ") { Foreground = Brushes.Red });
tb1.Inlines.Add("from ");
tb1.Inlines.Add(new Run("Code-Behind") { TextDecorations = TextDecorations.Underline });
tb1.Inlines.Add(".");
this.Content = tb1;

}
}
}

Result:





0 comments:

Post a Comment