2016-05-09 47 views
3

我在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應輸出「你好」。正如你所看到的,我能夠使用加粗的文本,但我怎樣才能輸出它?

+0

我覺得行內纔有效,直接在XAML。您可以在轉換器中創建InLines,但很麻煩。您可以在FlowDocument查看器中執行FlowDocument。 – Paparazzi

+0

要明確瞭解您的要求,請告訴我您希望在Border_Clicked事件中獲得粗體文本(Hello),對嗎? – Davy

+0

難道你不能簡單地根據[這個答案](http://stackoverflow.com/a/5263094/1997232)設置屬性'FontWeight'?像'myText.Inlines.Add(新運行(「粗體文本」){FontWeight = FontWeight.Bold});' – Sinatr

回答

2

@ Helen,有一種方法可以使用TextRange從TextPointer中獲取文本。嘗試此代碼

void myBorder_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    var senderBox = (Border)sender; 
    var senderText = (TextBlock)senderBox.Child; 
    var inline = (Bold)senderText.Inlines.ElementAt(0); 

    var textRange = new TextRange(inline.ContentStart, inline.ContentEnd); 
    Console.WriteLine(textRange.Text); 
} 
0

無法控制MessageBox中的字體特徵。我認爲你應該考慮創建一個「自定義MessageBox」。事情是這樣的:

<Window x:Class="WpfApplication1.CustomMessageBox" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     SizeToContent="WidthAndHeight" MaxWidth="400"> 

    <Grid x:Name="GridContent" Margin="10"> 

    </Grid> 
</Window> 

所以它的構造,你可以把你的粗體:

private Bold _text; 
public CustomMessageBox(Bold formattedText) 
{ 
    _text = formattedText; 
    GridContent.Child = _text; 
} 

使用:

private void Border_Clicked(object sender, MouseButtonEventArgs e) 
{ 
    Border senderBox = (Border)sender; 
    TextBlock senderText = (TextBlock)senderBox.Child; 
    Bold inline = (Bold) senderText.Inlines.ElementAt(0); 
    var customMsgBox = new CustomMessageBox(inline); 
    customMsgBox.ShowModal(); 
} 

現在,如果你不知道它會始終是一個大膽對象,我建議您將格式化的文本保存爲XML格式,然後加載。看看這個:showing formatted text

+0

謝謝,但我不想在一個消息框中輸出文本,而是輸入到一個「TextBox」中。我無法將「Bold」分配給「TextBox.Text」。 –

+0

也許你應該使用RichTextBox。將Bold元素保存爲xaml,並將其加載到RichTextBox中。看到這個:https://msdn.microsoft.com/en-us/library/aa970917(v=vs.100).aspx – quicoli

0

問題是從Bold元素中獲取文本的問題?

private void Border_Clicked(object sender, MouseButtonEventArgs e) 
{ 
    var border = (Border)sender; 
    var textBlock = (TextBlock)border.Child; 
    var bold = (Bold)textBlock.Inlines.ElementAt(0); 

    // How to Output "Hello "? 

    // try 
    var output = ((Run)bold).Text; 
    // or rather (because Bold is a wrapper) 
    var output = ((Run)bold.Inlines[0]).Text; 
} 

如果你能像這樣

myText.Inlines.Add(new Run("Bold text") { FontWeight = FontWeight.Bold }); 

在線添加那麼它的

var run = (Run)textBlock.Inlines[0]; 
var output = run.Text; 
+0

Did1不知道你可以投入內聯運行。 – person27