C# 文本块的粗体、内联、超链接集合?
获取代码:C# 文本块的粗体、内联、超链接集合?,c#,wpf,C#,Wpf,获取代码: results.senselist += "\n" + sense_list + Orth + gramGroup + "\n"; <TextBlock Text="{Binding senselist"}></TextBlock> results.senselist+=“\n”+sense\u list+Orth+gramGroup+“\n”; 我希望gramGroup是超链接,其他颜色和感觉列表是粗体或内联线 希望都是代码。不可能为同一文本的不同部分设
results.senselist += "\n" + sense_list + Orth + gramGroup + "\n";
<TextBlock Text="{Binding senselist"}></TextBlock>
results.senselist+=“\n”+sense\u list+Orth+gramGroup+“\n”;
我希望gramGroup是超链接,其他颜色和感觉列表是粗体或内联线
希望都是代码。不可能为同一文本的不同部分设置样式。 您应该使用两种不同的文本块,如下所示:
<StackPanel Orientation="Horizontal">
<TextBlock Text="sense_list " FontWeight="Bold"/>
<TextBlock Width="Auto">
<Hyperlink NavigateUri="http://www.google.com">
gramGroup
</Hyperlink>
</TextBlock>
</StackPanel>
gramGroup
您需要根据需要在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>
它在屏幕中呈现如下
我漏掉了问题中大胆的部分。只需在
TextBlock
中设置fontwweight=“Bold”
,您就可以使用标签,因为它是一个内容控件,您可以编写一个转换器,将字符串转换为具有所有格式的TextBlock。我假设两个字符串之间有一个分隔符来区分它们。请参阅下面的代码
<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";
类TextBlockConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
TextBlock txt=新的TextBlock();
字符串str=(字符串)值;
字符串[]strList=str.Split(“|”);
运行run1=新运行(strList[0]);
run1.FontWeight=FontWeights.Bold;
运行run2=新运行(strList[1]);
Hyperlink hyp=新的超链接(run2);
添加(run1);
txt.Inlines.Add(hyp);
返回txt;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
senselist=“sense_list”+“|”+“gramGroup”;
您到底想要什么?如果你想用不同的格式绑定它们,你肯定需要单独的属性工具,不知道:D无论如何结果是一样的,我们仍然需要多个属性来正确绑定它们:/name“TextBlockConverter”在命名空间“using:TestApp”中不存在这是什么???你试图添加的名称空间有问题..检查这篇文章我在对象值处得到null。不过,senselist正在获取数据。Run(strList[0])有错误。无法在运行中使用strList[0]。
<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";