2 min read

Binding Inlines to the TextBlock using Attached Properties on Windows Phone 7

Binding Inlines to the TextBlock using Attached Properties on Windows Phone 7
Photo by Toa Heftiba / Unsplash

Old Post - Just kept for the record. The platform the post centered around was extinct log ago.

An attached property is a concept defined by Extensible Application Markup Language (XAML). An attached property is intended to be used as a type of global property that is settable on any object.

Let's see one simple piece of code on how to create a dependency property...

public static class BindingHelper
{
 
 public static readonly DependencyProperty BindInlinesTextProperty = DependencyProperty.RegisterAttached(
            "BindInlinesText",typeof(string), typeof(BindingHelper),
            new PropertyMetadata(OnBindingChanged));
 
 
        /// <summary>
        /// Gets the BindInlinesText dependency property from an TextBlock
        /// </summary>
        /// <param name="source">The object to get the property from</param>
        /// <returns>The property's value</returns>
        public static string GetBindInlinesText(TextBlock source) { return (string)source.GetValue(BindInlinesTextProperty); }
 
 
        /// <summary>
        /// Sets the BindInlinesText dependency property on a TextBlock
        /// </summary>
        /// <param name="source">The object to set the property on</param>
        /// <param name="value">The value to set</param>
        public static void SetBindInlinesText(TextBlock source, string value) { source.SetValue(BindInlinesTextProperty, value); }
 
       /// Whenever Binding value changes it will update the TextBlock on which the property attached
        private static void OnBindingChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (obj != null && obj is TextBlock)
            {
                BindInlines(obj as TextBlock, e.NewValue.ToString());
            }
        }
     
        private static void BindInlines(TextBlock txtblock, string value)
        {
            if (txtblock == null)
            { return; }
            string[] sep = { "<b>", "</b>" };
            string[] splitword = value.Split(sep, StringSplitOptions.None);
            try
            {
                txtblock.Inlines.Clear();
                Run run1 = new Run();
 
                Run run2 = new Run();
                Run run3 = new Run();
                txtblock.Inlines.Add(run1);
                txtblock.Inlines.Add(run2);
                txtblock.Inlines.Add(run3);
                run1.Text = splitword[0];
                run1.Text = splitword[1];
                run1.Text = splitword[2];
            }
            catch { }
        }
   
    }

You can also specify the type of object on which the property can be attached. In this example i have made the property to be visible only on Textblock elements. You can also create your own Attached Property that can be used on any UIElement by specifying the type as DependencyObject.

    /// <summary>
    /// Gets the BindInlinesText dependency property from an TextBlock
    /// </summary>
    /// <param name="source">The object to get the property from</param>
    /// <returns>The property's value</returns>
        public static string GetBindInlinesText(DependencyObject source)
       { return (string)source.GetValue(BindInlinesTextProperty); }
      /// <summary>
      /// Sets the BindInlinesText dependency property on a TextBlock
      /// </summary>
     /// <param name="source">The object to set the property on</param>
    /// <param name="value">The value to set</param>
       public static void SetBindInlinesText(DependencyObject source, string value)
       { source.SetValue(BindInlinesTextProperty, value); }

And each and every Attached Property that we are creating must have the functions that can be used set and get values as i have used in this example.

In xaml the attached property can be used like this.

<TextBlock Helpers:BindingHelper.BindInlinesText ="{Binding Value}" />

In the above example I have tried to interpret HTML into XAML. In case of binding inlines directly into textblock we have to serve either Inline object or List Collection of Inline objects and do the appropriate code changes on BindInlines method as described above.