獲取代碼:粗體,內聯,文本塊的超鏈接集合?
results.senselist += "\n" + sense_list + Orth + gramGroup + "\n";
<TextBlock Text="{Binding senselist"}></TextBlock>
我想gramGroup是超鏈接和其他顏色和sense_list大膽或內聯。
希望所有代碼。
獲取代碼:粗體,內聯,文本塊的超鏈接集合?
results.senselist += "\n" + sense_list + Orth + gramGroup + "\n";
<TextBlock Text="{Binding senselist"}></TextBlock>
我想gramGroup是超鏈接和其他顏色和sense_list大膽或內聯。
希望所有代碼。
不可能對相同文本的不同部分進行樣式設置。 你應該有兩個不同的TextBlock是這樣的:
<StackPanel Orientation="Horizontal">
<TextBlock Text="sense_list " FontWeight="Bold"/>
<TextBlock Width="Auto">
<Hyperlink NavigateUri="http://www.google.com">
gramGroup
</Hyperlink>
</TextBlock>
</StackPanel>
[這的確是可能的:)](http://stackoverflow.com/a/29700907/2530848) –
很酷,不知道: D無論如何,結果是相同的,我們仍然需要多個屬性來正確綁定它們:/ – Donbabbeo
您需要定義的內聯在你的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"
的問題。
您可以使用標籤,因爲它是一個內容控件,您可以編寫一個轉換器,將字符串轉換爲具有所有格式的文本塊。我認爲兩個字符串之間有一個分隔符來區分它們。請參閱以下代碼。
<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";
命名空間「using:TestApp」中不存在名稱「TextBlockConverter」它是什麼? –
您嘗試添加名稱空間中存在問題。查看本文內容http://stackoverflow.com/questions/3297660/adding-a-custom-namespace-to-xaml –
我在對象值變爲null。雖然,senselist正在獲取數據。 –
你究竟想要什麼?如果你想用不同的格式綁定它們,你肯定需要單獨的屬性 –