Difference between Label and TextBlock

TextBlock is not a control Even though TextBlock lives in the System.Windows.Controls namespace, it is not a control. It derives directly from FrameworkElement. Label, on the other hand, derives from ContentControl. This means that Label can: Be given a custom control template (via the Template property). Display data other than just a string (via the … Read more

How to bind a TextBlock to a resource containing formatted text?

Here is my modified code for recursively format text. It handles Bold, Italic, Underline and LineBreak but can easily be extended to support more (modify the switch statement). public static class MyBehavior { public static string GetFormattedText(DependencyObject obj) { return (string)obj.GetValue(FormattedTextProperty); } public static void SetFormattedText(DependencyObject obj, string value) { obj.SetValue(FormattedTextProperty, value); } public static … Read more

Data binding the TextBlock.Inlines

You could add a Dependency Property to a TextBlock Subclass public class BindableTextBlock : TextBlock { public ObservableCollection<Inline> InlineList { get { return (ObservableCollection<Inline>)GetValue(InlineListProperty); } set { SetValue(InlineListProperty, value); } } public static readonly DependencyProperty InlineListProperty = DependencyProperty.Register(“InlineList”,typeof(ObservableCollection<Inline>), typeof(BindableTextBlock), new UIPropertyMetadata(null, OnPropertyChanged)); private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { BindableTextBlock textBlock = sender as … Read more

Add hyperlink to textblock WPF

Displaying is rather simple, the navigation is another question. XAML goes like this: <TextBlock Name=”TextBlockWithHyperlink”> Some text <Hyperlink NavigateUri=”http://somesite.example” RequestNavigate=”Hyperlink_RequestNavigate”> some site </Hyperlink> some more text </TextBlock> And the event handler that launches default browser to navigate to your hyperlink would be: private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) { System.Diagnostics.Process.Start(e.Uri.ToString()); } To do it with … Read more

How to bind multiple values to a single WPF TextBlock?

You can use a MultiBinding combined with the StringFormat property. Usage would resemble the following: <TextBlock> <TextBlock.Text> <MultiBinding StringFormat=”{}{0} + {1}”> <Binding Path=”Name” /> <Binding Path=”ID” /> </MultiBinding> </TextBlock.Text> </TextBlock> Giving Name a value of Foo and ID a value of 1, your output in the TextBlock would then be Foo + 1. Note: This … Read more