0

Getting a code :

results.senselist += "\n" + sense_list + Orth + gramGroup + "\n";
<TextBlock Text="{Binding senselist"}></TextBlock>

I want gramGroup is hyperlink and other color and sense_list is bold or Inlines.

Hopefully, All in code.

3 Answers3

2

You need to define the Inlines in your xaml as you need and bind the propeties.

<TextBlock>
    <TextBlock.Inlines>
        <Run Text="{Binding PlainText1}"></Run>
        <Hyperlink>
            <TextBlock Text="{Binding LinkText}"></TextBlock>
        </Hyperlink>
        <Run Text="{Binding PlainText2}"></Run>
        <Run Text="{Binding ColorText}" Foreground="Red"></Run>
    </TextBlock.Inlines>
</TextBlock>

DataContext could be

public class MyDataContext
{
    public MyDataContext()
    {
        PlainText1 = "This is";
        LinkText = "some link";
        PlainText2 = "with text";
        ColorText = "and red color :)";
    }

    public string LinkText { get; set; }
    public string ColorText { get; set; }
    public string PlainText1{ get; set; }
    public string PlainText2 { get; set; }
}

Which renders in screen as follows enter image description here

I missed out the bold part in the question. It is just a matter of setting FontWeight="Bold" in your TextBlock.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
0

It's not possible to style different section of the same text. You should go with two different TextBlock like this:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="sense_list " FontWeight="Bold"/>
    <TextBlock Width="Auto">
       <Hyperlink NavigateUri="http://www.google.com">
           gramGroup
       </Hyperlink>
    </TextBlock>
</StackPanel>
Donbabbeo
  • 33
  • 5
  • Cool, didn't know that :D Anyway the outcome is the same, we still need multiple properties to binding them correctly :/ – Donbabbeo Apr 17 '15 at 14:03
0

You can use a label because it is a content control and you can write a converter which converts the string into textblock with all formatting. I assumed there is a seperator between two string to differentiate them. Refer below code.

<Window.Resources>
    <local:TextBlockConverter x:Key="Conv"/>
</Window.Resources>

<Label Content="{Binding senselist,Converter={StaticResource Conv}}"></Label>
class TextBlockConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        TextBlock txt = new TextBlock();
        string str = (string)value;
        string[] strList = str.Split('|');
        Run run1 = new Run(strList[0]);
        run1.FontWeight = FontWeights.Bold;
        Run run2 = new Run(strList[1]);
        Hyperlink hyp = new Hyperlink(run2);
        txt.Inlines.Add(run1);
        txt.Inlines.Add(hyp);
        return txt;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 senselist = "sense_list" + "|" + "gramGroup";
Ayyappan Subramanian
  • 5,348
  • 1
  • 22
  • 44