Skip to main content

Microsoft Silverlight

Answered Question how can I get textblock fontweight , fontcolorRSS Feed

(0)

nz
nz

Member

Member

9 points

14 Posts

how can I get textblock fontweight , fontcolor

Guys,

Does anyone know how to get fontweight , fontcolor from specific words of a textblock in c#, as those code below, how can I get those run elements

<TextBlock FontSize="20" x:Name="boardField" Margin="34,40,30,31" TextWrapping="Wrap" Foreground="White" FontWeight="ExtraBlack" FontFamily="Kristen ITC">
      <Run FontWeight="Normal" Text=" this is my "/>
      <Run FontWeight="Bold"  Text="blackboard"/>
  </TextBlock>

many thx

Sledge70
Sledge70

Contributor

Contributor

5988 points

1,050 Posts

Answered Question

Re: how can I get textblock fontweight , fontcolor

 You can either name the run elements and access them in code behind i.e.

 

<TextBlock FontSize="20" x:Name="boardField" Margin="34,40,30,31" TextWrapping="Wrap" Foreground="White" FontWeight="ExtraBlack" FontFamily="Kristen ITC">
  <Run x:Name="runNormal" FontWeight="Normal" Text=" this is my "/>
  <Run x:Name="runBold" FontWeight="Bold"  Text="blackboard"/>
</TextBlock>
CodeBehind:
FontWeight fontWeight = runNormal.FontWeight;

Or you could use the VisualTreeHelper and interrogate the content of the TextBlock.

Edit: How could I forget about Inline?

______________________________________________________
Please mark replies as answers if they answered your question...

Flexman on Silverlight

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Answered Question

Re: how can I get textblock fontweight , fontcolor

Access Run element by calling TextBlock.Inlines collection:

 foreach (Inline l in boardField.Inlines)
            {
              Run r = l as Run;
              if(r != null)
                MessageBox.Show(r.FontWeight.ToString() + ": " + r.Text);
            }

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

nz
nz

Member

Member

9 points

14 Posts

Re: how can I get textblock fontweight , fontcolor

thanks guys

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities