Label and LinkLabel controls in WPF
Label And TextBlock:
For Label control in you can use
Label or
Textblock in WPF.
TextBlock is lighter, so use it if you don’t need the Accelerator feature of the Label control.
The labels have an automatic margin so all controls and spacing is done automatically by the parent control.
Namespace : System.Windows.Controls.Label
Namespace : System.Windows.Controls.TextBlock
Designer Code:
<StackPanel>
<Label Name="lbl1">one from label</Label>
<TextBlock Name="txtblo1"> one from textblock</TextBlock>
</StackPanel>
Code Behind:
To retive the text from the Label and TextBlock:
// Window1.xaml.cs
private void Window_Loaded(object sender,
RoutedEventArgs e)
{
MessageBox.Show(lbl1.Content.ToString());
MessageBox.Show(txtblo1.Text.ToString());
}
Label with formatting:
<Label Name="Lbl1"
Content="SomeText"
Width="150" Height="30"
Canvas.Left="10" Canvas.Top="10"
FontSize="14" FontFamily="Arial"
FontWeight="Bold"
Background="Blue"
Foreground="Red"
VerticalAlignment="Center"
HorizontalAlignment="Center"
/>
Image as Background of a Label:
<Label.Background>
<ImageBrush ImageSource="\Images\win2wpf.JPG" />
</Label.Background>
LinkLabel (or) Hyperlink:
Namespace : System.Windows.Documents.Hyperlink
Designer Code:
<TextBlock> Check out this site
<Hyperlink NavigateUri="http://www.Win2wpf.com">
Win2wpf
</Hyperlink>
for convesion
</TextBlock>
Code Behind:
To Set the navigateUri from code:
// Window1.xaml.cs
Run run1 = new Run("Win2wpf");
// Create a Hyperlink and setting NavigateUri
Hyperlink hlink = new Hyperlink(run1);
hlink.NavigateUri = new Uri("http://www.Win2wpf.com");