views:

31

answers:

1

I am trying to format a Tweet using Data Binding. What I need to do is split the Text value of the tweet based on what type of content it is.

text = "This is a Tweet with a hyperlink http://www.mysite.com"

I need to add some color formatting to the http://... portion of the text value.

Here's the kicker, I'd like to do this using only XAML Data Binding.

 <TextBlock x:Name="Tweet1" FontWeight="Bold" Height="207.236" 
    LineHeight="55" TextAlignment="Left" TextWrapping="Wrap" 
    Width="1614.646" Text="{Binding XPath=/statuses/status[2]/text}" 
    FontSize="56" FontFamily="Segoe Book" 
    Foreground="{DynamicResource TextColor-Gray}" />

// needs to end up looking like

<TextBlock x:Name="Tweet1" FontWeight="Bold" ... FontSize="56" FontFamily="Segoe Book">
  <Run Foreground="{DynamicResource TextColor-Gray}" >This is a Tweet with a hyperlink</Run>
<Run Foreground="{DynamicResource TextColor-Pink}" >http://www.mysite.com&lt;/Run&gt;
</TextBlock>

Here is a Regex I could use to split the text value, but I'm trying to use strictly DataBinding.

Regex regUrl = new Regex(@"/http:\/\/\S+/g");

Suggestions?

+2  A: 

You can't bind to Text and substitute with Runs because Text is of type String. Instead, you'd need to bind Inlines and provide a converter that parses the text (using your regex, for example) and produces the appropriate Inlines:

<TextBlock Inlines="{Binding XPath=/statuses/status[2]/text, Converter={StaticResource InlineConverter}}"/>

HTH,
Kent

Kent Boogaart
+1 Can't be done the way the OP wants.
Ragepotato
That what I figured. Thanks for confirming. I'll post my converter when I'm done building it.
discorax
You can't bind to Inlines. "Inlines property is Read-Only and can not be set by markup" Any other suggestions?
discorax
Your solution won't work, but it lead me to this answer which I am trying to work through now. http://stackoverflow.com/questions/1959856/data-binding-the-textblock-inlines
discorax