[英]Bold,Inlines, hyperlink collections of textblock?
获取代码:
results.senselist += "\n" + sense_list + Orth + gramGroup + "\n";
<TextBlock Text="{Binding senselist"}></TextBlock>
我希望gramGroup为超链接,其他颜色为Sense_list为粗体或Inlines。
希望所有代码。
您需要根据需要在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
可能是
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; }
}
在屏幕上呈现如下
我错过了问题中的大胆部分。 只需在TextBlock
中设置FontWeight="Bold"
。
无法为同一文本的不同部分设置样式。 您应该使用两个不同的TextBlock,如下所示:
<StackPanel Orientation="Horizontal">
<TextBlock Text="sense_list " FontWeight="Bold"/>
<TextBlock Width="Auto">
<Hyperlink NavigateUri="http://www.google.com">
gramGroup
</Hyperlink>
</TextBlock>
</StackPanel>
您可以使用标签,因为它是一个内容控件,并且您可以编写一个转换器,将所有格式的字符串转换为文本块。 我假设两个字符串之间有一个分隔符以区分它们。 请参考下面的代码。
<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.