简体   繁体   English

粗体,内联,文本块的超链接集合?

[英]Bold,Inlines, hyperlink collections of textblock?

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. 我希望gramGroup为超链接,其他颜色为Sense_list为粗体或Inlines。

Hopefully, All in code. 希望所有代码。

You need to define the Inlines in your xaml as you need and bind the propeties. 您需要根据需要在xaml中定义内联,并绑定属性。

<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 DataContext可能是

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 在屏幕上呈现如下 在此处输入图片说明

I missed out the bold part in the question. 我错过了问题中的大胆部分。 It is just a matter of setting FontWeight="Bold" in your TextBlock . 只需在TextBlock中设置FontWeight="Bold"

It's not possible to style different section of the same text. 无法为同一文本的不同部分设置样式。 You should go with two different TextBlock like this: 您应该使用两个不同的TextBlock,如下所示:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="sense_list " FontWeight="Bold"/>
    <TextBlock Width="Auto">
       <Hyperlink NavigateUri="http://www.google.com">
           gramGroup
       </Hyperlink>
    </TextBlock>
</StackPanel>

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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM