Sunday, August 21, 2011

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


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.


   1: /// <summary>
   2:     /// Gets the BindInlinesText dependency property from an TextBlock
   3:     /// </summary>
   4:     /// <param name="source">The object to get the property from</param>
   5:     /// <returns>The property's value</returns>
   6:     public static string GetBindInlinesText(DependencyObject source)
   7:    { return (string)source.GetValue(BindInlinesTextProperty); }
   8:  
   9:  
  10:     /// <summary>
  11:     /// Sets the BindInlinesText dependency property on a TextBlock
  12:     /// </summary>
  13:     /// <param name="source">The object to set the property on</param>
  14:     /// <param name="value">The value to set</param>
  15:     public static void SetBindInlinesText(DependencyObject source, string value)
  16:     { 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 will 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.

SAS Survival Guide for Windows Phone 7

Full version costs $4.99. (Trial Version is also available)

For over twenty years, the SAS Survival Guide has been the definitive guide to surviving any situation, anywhere in the world. Now, for the first time ever, the million-copy bestselling book has been reinvented for the Windows Phone 7.

Written by former SAS soldier and instructor, John "Lofty" Wiseman, this application brings you the elite training techniques of Britain’s toughest fighting force in the most accessible version ever. Now you can take the original set of world-class survival skills with you anywhere in the world – from the peaks of Kilimanjaro to the deserts of Kandahar…or to your closest national park.

Contains:

  • Full text of the bestselling book optimised for the Windows Phone 7
  • Photo galleries of edible, medicinal and poisonous plants
  • Morse Code signalling device
  • 100 + question quiz to test if you've got what it takes to survive
  • Survival Checklist
  • Sun Compass
  • Search tool to scan entire book by subject
  • Extreme Climate Survival: sections on surviving Polar, Desert, Tropical, and Sea
  • Comprehensive First Aid section

Like SAS Survival Guide on facebook:

Screenshots:

Source: SAS Survival Guide for Windows Phone 7. App Developer: Trellisys.net