我在StackPanel
中添加了一些TextBlock
元素到Border
元素。 我通過添加Inlines
來添加並格式化TextBlock
的文本。WPF C# - 從TextBlock獲取內聯格式粗體文本
點擊後,我想獲得TextBlock
的格式化文本。這裏是我的代碼。
public void addText()
{
TextBlock myText = new TextBlock();
myText.Inlines.Add(new Bold(new Run("Hello ")));
myText.Inlines.Add("World!");
Border myBorder = new Border();
myBorder.Child = myText;
myBorder.MouseDown += new MouseButtonEventHandler(Border_Clicked);
myStackPanel.Children.Add(myBorder);
}
private void Border_Clicked(object sender, MouseButtonEventArgs e)
{
//Border senderBox = (Border)sender;
//TextBlock senderText = (TextBlock)senderBox.Child;
//Bold inline = (Bold) senderText.Inlines.ElementAt(0);
// How to Output "Hello "?
}
Border_Clicked
應輸出「你好」。正如你所看到的,我能夠使用加粗的文本,但我怎樣才能輸出它?
我覺得行內纔有效,直接在XAML。您可以在轉換器中創建InLines,但很麻煩。您可以在FlowDocument查看器中執行FlowDocument。 – Paparazzi
要明確瞭解您的要求,請告訴我您希望在Border_Clicked事件中獲得粗體文本(Hello),對嗎? – Davy
難道你不能簡單地根據[這個答案](http://stackoverflow.com/a/5263094/1997232)設置屬性'FontWeight'?像'myText.Inlines.Add(新運行(「粗體文本」){FontWeight = FontWeight.Bold});' – Sinatr