2015-08-13 76 views
0

我試圖發送一個段落TextBlock的格式化的內容,但格式消失:如何將已格式化的段落插入到TextBlock中?

// Create a formatted paragraph 
Paragraph para = new Paragraph(); 
para.FontSize = 25; 
para.FontWeight = FontWeights.Bold; 
para.Inlines.Add(new Run("Text of paragraph.")); 

// Create a span with the content of the paragraph (FontSize 25 and FontWeight Bold stay alive) 
Span span = new Span(para.ContentStart, para.ContentEnd); 

// Create a TextBlock with the span (FontSize 25 and FontWeight Bold get lost) 
TextBlock textBlock = new TextBlock(); 
textBlock.Inlines.Add(span); 

可以做些什麼來保持格式?提前致謝。

更新
段落的格式在運行時是已知的,所以我不能申請屬性值一個手動之一。

更新2
這個問題的背景是,我想衡量格式化段落的長度,如果它們被延伸到一條線。 這可以通過TextBlock完成。段落位於TableCells中,我想自動調整列寬。

回答

0

我得出了以下解決方案:

// Create a formatted Paragraph 
Paragraph para = new Paragraph(); 
para.FontSize = 25; 
para.FontWeight = FontWeights.Bold; 
para.Inlines.Add(new Run("Text of paragraph.")); 

// Clone all Inlines 
List<Inline> clonedInlines = new List<Inline>(); 
foreach (Inline inline in para.Inlines) 
{ 
    Inline clonedInline = ElementClone<Inline>(inline); 
    clonedInlines.Add(clonedInline); 
} 

// Get all Paragraph properties with a set value 
List<DependencyProperty> depProps = DepPropsGet(para, PropertyFilterOptions.SetValues); 

// Apply the Paragraph values to each Inline 
foreach (DependencyProperty depProp in depProps) 
{ 
    object propValue = para.GetValue(depProp); 

    foreach (Inline clonedInline in clonedInlines) 
    { 
     // Can the Inline have the value? 
     if (depProp.OwnerType.IsAssignableFrom(typeof(Inline))) 
     { 
      // Apply the Paragraph value 
      clonedInline.SetValue(depProp, propValue); 
     } 
    } 
} 

// Create a TextBlock with the same properties as the Paragraph 
TextBlock textBlock = new TextBlock(); 
textBlock.Inlines.AddRange(clonedInlines); 

/// <summary> 
/// Cloner. 
/// </summary> 
public static T ElementClone<T>(T element) 
{ 
    // Element to Stream 
    MemoryStream memStream = new MemoryStream(); 
    XamlWriter.Save(element, memStream); 

    // Cloned element from Stream 
    object clonedElement = null; 
    if (memStream.CanRead) 
    { 
     memStream.Seek(0, SeekOrigin.Begin); 
     clonedElement = XamlReader.Load(memStream); 
     memStream.Close(); 
    } 
    return (T)clonedElement; 
} 

/// <summary> 
/// Property-Getter. 
/// </summary> 
public static List<DependencyProperty> DepPropsGet(DependencyObject depObj, PropertyFilterOptions filter) 
{ 
    List<DependencyProperty> result = new List<DependencyProperty>(); 

    foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(depObj, new Attribute[] { new PropertyFilterAttribute(filter) })) 
    { 
     DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(pd); 

     if (dpd != null) 
     { 
      result.Add(dpd.DependencyProperty); 
     } 
    } 

    return result; 
} 
0

格式化不能應用於TextBlock,它類似於Label,如果需要格式化,則可以使用<RichTextBox/>代替。你可以使它ReadOnly避免編輯。

例子:

<RichTextBox Margin="10" ReadOnly="true"> 
     <FlowDocument> 
      <Paragraph FontSize="36">Hello, world!</Paragraph> 
      <Paragraph FontStyle="Italic" TextAlignment="Left" FontSize="14" Foreground="Gray">Thanks to the RichTextBox control, this FlowDocument is completely editable!</Paragraph> 
     </FlowDocument> 
    </RichTextBox> 
1

相反的是,@聯合國幸運的說,TextBlock中確實有這種格式的能力。

結賬this article

通過將樣式直接應用到跨度,您會在文本框中保留它。

摘錄:

     TextBlock tb = new TextBlock(); 
        tb.TextWrapping = TextWrapping.Wrap; 
        tb.Margin = new Thickness(10); 
        tb.Inlines.Add("An example on "); 
        tb.Inlines.Add(new Run("the TextBlock control ") { FontWeight = FontWeights.Bold }); 
        tb.Inlines.Add("using "); 
        tb.Inlines.Add(new Run("inline ") { FontStyle = FontStyles.Italic }); 
        tb.Inlines.Add(new Run("text formatting ") { Foreground = Brushes.Blue }); 
        tb.Inlines.Add("from "); 
        tb.Inlines.Add(new Run("Code-Behind") { TextDecorations = TextDecorations.Underline }); 
        tb.Inlines.Add("."); 

更新 得到您的更新,但你可以採取的字體和樣式從段落,然後將它們直接套用。至少從你上面的例子中看起來如此。

TextBlock tb = new TextBlock(); 

Paragraph para = new Paragraph(); 
para.FontSize = 25; 
para.FontWeight = FontWeights.Bold; 
para.Inlines.Add(new Run("new paragraph")); 

Span span = new Span(para.ContentStart, para.ContentEnd); 
span.FontWeight = para.FontWeight; 
span.FontSize = para.FontSize; 

tb.Inlines.Add(span); 

這是否適合您?

+0

我無法直接應用樣式,請參閱上面的我的更新。 – Pollitzer

+0

看到我上面的更新 - 你可以從段落中的樣式,仍然直接在代碼中應用它們。 – nepdev

+0

段落的格式可以是任何東西,不一定總是指整個範圍,所以,不幸的是,我不能這樣做。請參閱我的更新2. – Pollitzer

0

相反的TextBlock的您可以使用RichTextBox,實現必要的格式 這裏是一個示例代碼

// Create a formatted paragraph 
     Paragraph para = new Paragraph(); 
     para.FontSize = 25; 
     para.FontWeight = FontWeights.Bold; 
     para.Inlines.Add(new Run("Text of paragraph.")); 

     Myrichtextboxtbx.Document.Blocks.Add(para); 

,然後添加你的RichTextBox爲XAML

+0

請參閱我的更新2上面。 – Pollitzer