Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4048986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:53:14+00:00 2026-05-20T13:53:14+00:00

I’m using Silverlight on Windows Phone 7. I want to display the first part

  • 0

I’m using Silverlight on Windows Phone 7.

I want to display the first part of some text in a TextBlock in bold, and the rest in normal font. The complete text must wrap. I want the bolded part to contain text from one property in my ViewModel, and the plain text to contain text from a different property.

The TextBlock is defined in a DataTemplate associated with a LongListSelector.

My initial attempt was:

<TextBlock TextWrapping="Wrap">
  <TextBlock.Inlines>
    <Run Text="{Binding Property1}" FontWeight="Bold"/>
    <Run Text="{Binding Property2}"/>
  </TextBlock.Inlines>
</TextBlock>

This fails at runtime with the spectacularly unhelpful “AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR”. This is a known issue because the Run element is not a FrameworkElement and cannot be bound.

My next attempt was to put placeholders in place, and then update them in code:

<TextBlock Loaded="TextBlockLoaded" TextWrapping="Wrap">
    <TextBlock.Inlines>
        <Run FontWeight="Bold">Placeholder1</Run>
        <Run>Placeholder2</Run>
    </TextBlock.Inlines>
</TextBlock>

In the code-behind (yes I am desparate!):

private void TextBlockLoaded(object sender, RoutedEventArgs e)
{
    var textBlock = (TextBlock)sender;
    var viewModel = (ViewModel)textBlock.DataContext;
    var prop1Run = (Run)textBlock.Inlines[0];
    var prop2Run = (Run)textBlock.Inlines[1];
    prop1Run.Text = viewModel.Property1;
    prop2Run.Text = viewModel.Property2;
}

This seemed to work, but because I am using the LongListSelector, although items get recycled, the Loaded codebehind event handler doesn’t re-initialize the Runs, so very quickly the wrong text is displayed…

I’ve looked at using the LongListSelector’s Linked event (which I already use to free up images that I display in the list), but I can’t see how I can use that to re-initialize the Runs’ text properties.

Any help appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:53:15+00:00Added an answer on May 20, 2026 at 1:53 pm

    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));
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
i want to parse a xhtml file and display in UITableView. what is the
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.