Databinding TextBlock Runs in Silverlight / WP7

I finally found a solution that works for me.

As I mention in the comment, Paul Stovell’s approach would not work.

Instead I used a similar approach to add an attached property to the TextBlock, bound to the TextBlock’s DataContext, and attached properties on the runs, indicating which ViewModel properties they should be bound to:

<TextBlock TextWrapping="Wrap"
            Views:BindableRuns.Target="{Binding}">
    <TextBlock.Inlines>
        <Run FontWeight="Bold" Views:BindableRuns.Target="Property1"/>
        <Run Views:BindableRuns.Target="Property2"/>
    </TextBlock.Inlines>
</TextBlock>

Then in my attached TextBox Target (datacontext) property’s changed event, I update the Runs, and subscribe to be notified of changes to the TextBox Target properties. When a TextBox Target property changes, I updated any associated Run’s text accordingly.

public static class BindableRuns
{
    private static readonly Dictionary<INotifyPropertyChanged, PropertyChangedHandler> 
        Handlers = new Dictionary<INotifyPropertyChanged, PropertyChangedHandler>();

    private static void TargetPropertyPropertyChanged(
                                    DependencyObject dependencyObject,
                                    DependencyPropertyChangedEventArgs e)
    {
        if(!(dependencyObject is TextBlock)) return;

        var textBlock = (TextBlock)dependencyObject;
        AddHandler(e.NewValue as INotifyPropertyChanged, textBlock);
        RemoveHandler(e.OldValue as INotifyPropertyChanged);
        InitializeRuns(textBlock, e.NewValue);
    }

    private static void AddHandler(INotifyPropertyChanged dataContext,
                                   TextBlock textBlock)
    {
        if (dataContext == null) return;

        var propertyChangedHandler = new PropertyChangedHandler(textBlock);
        dataContext.PropertyChanged += propertyChangedHandler.PropertyChanged;
        Handlers[dataContext] = propertyChangedHandler;
    }

    private static void RemoveHandler(INotifyPropertyChanged dataContext)
    {
        if (dataContext == null || !Handlers.ContainsKey(dataContext)) return;

        dataContext.PropertyChanged -= Handlers[dataContext].PropertyChanged;
        Handlers.Remove(dataContext);
    }

    private static void InitializeRuns(TextBlock textBlock, object dataContext)
    {
        if (dataContext == null) return;

        var runs = from run in textBlock.Inlines.OfType<Run>()
                   let propertyName = (string)run.GetValue(TargetProperty)
                   where propertyName != null
                   select new { Run = run, PropertyName = propertyName };


        foreach (var run in runs)
        {
            var property = dataContext.GetType().GetProperty(run.PropertyName);
            run.Run.Text = (string)property.GetValue(dataContext, null);
        }
    }

    private class PropertyChangedHandler
    {
        private readonly TextBlock _textBlock;
        public PropertyChangedHandler(TextBlock textBlock)
        {
            _textBlock = textBlock;
        }

        public void PropertyChanged(object sender,
                                    PropertyChangedEventArgs propertyChangedArgs)
        {
            var propertyName = propertyChangedArgs.PropertyName;
            var run = _textBlock.Inlines.OfType<Run>()
                .Where(r => (string) r.GetValue(TargetProperty) == propertyName)
                .SingleOrDefault();
            if(run == null) return;

            var property = sender.GetType().GetProperty(propertyName);
            run.Text = (string)property.GetValue(sender, null);
        }

    }


    public static object GetTarget(DependencyObject obj)
    {
        return obj.GetValue(TargetProperty);
    }

    public static void SetTarget(DependencyObject obj,
        object value)
    {
        obj.SetValue(TargetProperty, value);
    }

    public static readonly DependencyProperty TargetProperty =
        DependencyProperty.RegisterAttached("Target",
            typeof(object),
            typeof(BindableRuns),
            new PropertyMetadata(null,
                TargetPropertyPropertyChanged));

}

Leave a Comment