-1
<TextBlock Text="{Binding text,Converter={StaticResource visibleconvert}}"
           Foreground="Black">
</TextBlock>
public object Convert(object value, Type targetType, object parameter, string language)
{
    TextBlock txt = new TextBlock();
    string str = (string)value;
    string[] strList = str.Split('=');

    Run run2 = new Run();
    run2.Text = strList[1];
    SolidColorBrush mySolidColorBrush = new SolidColorBrush(Colors.Blue);
    run2.Foreground = mySolidColorBrush;
    txt.Inlines.Add(run2);
    return txt;
}

As results is "Windows.UI.Xaml.Controls.TextBlock". I had debugger then return txt is null while "txt.Inlines.Add(run2);" is having data. Everyone help me to solve it.

slavoo
  • 5,798
  • 64
  • 37
  • 39

1 Answers1

0

You can achieve using a MultiValueConverter by sending the TextBlock itself to the converter. refer below code

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

 <TextBlock>
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource Conv}" UpdateSourceTrigger="PropertyChanged">
                <Binding RelativeSource="{RelativeSource Self}" Mode="OneTime"/>
                <Binding Path="senselist" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>

class TextBlockConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        TextBlock txt =  values[0] as TextBlock; 
        string str = (string)values[1];
        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[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 senselist = "sense_list" + "|" + "gramGroup";

My post Bold,Inlines, hyperlink collections of textblock?

Community
  • 1
  • 1
Ayyappan Subramanian
  • 5,348
  • 1
  • 22
  • 44