using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;

namespace BeWo.Controls
{
    public class BindableRun : Run
    {
        public static readonly DependencyProperty BoundTextProperty = DependencyProperty.Register("BoundText", typeof(string), typeof(BindableRun), new PropertyMetadata(new PropertyChangedCallback(onBoundTextChanged)));

        public BindableRun()
        {
            Binding b = new Binding("DataContext");
            b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FrameworkElement), 1);
            this.SetBinding(DataContextProperty, b);
        }

        public string BoundText
        {
            get
            {
                return (string)this.GetValue(BoundTextProperty);
            }

            set
            {
                this.SetValue(BoundTextProperty, value);
            }
        }

        private static void onBoundTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Run)d).Text = (string)e.NewValue;
        }
    }
}