WPF FlowDocument :: How Can I Define Custom Templates for the Child Paragraph, Run, etc

我与影子孤独终老i 提交于 2019-12-13 06:52:37

问题


I'm fairly new to WPF, so please bear with me.

I am trying to customize the presentation of the various elements of a FlowDocument in a RichTextBox (RTB). WPF controls are lookless, so as I understand it I should be able to define the look for every child in the Document.

Would I define the template for this in the RTB? Separate templates for each item (Paragraph, Section, Run, etc) as resources? Let's say, for sake of argument, that I want a red border around every Section, the word "Paragraph:" before every Paragraph with a Margin="5", and every Run to be in Consolas. (see edit one)

EDIT ONE:

I learn more here from asking the wrong question than the right one. It seems I never ask what I'm really trying to do the first time out the gate. Who knows, maybe I'll learn.

I am trying to define a DataTemplate or an ItemTemplate (I think) similar to a ListBox. The ultimate goal is a "source view" HTML editor in an RTB. I want a Paragraph XAML element to render like this in the RTB:

<p>
  Lorem ipsum stackus overflowum 
</p>

... and a Run would render like this:

<span style="font-weight:bold">clarity is important when asking questions</span>

... and so on with the different XAML elements.


回答1:


Some of that you can do with styles
E.G.

<Paragraph Style="{StaticResource Par2}">
    This is not a substitute for training.
</Paragraph>

<Style x:Key="Par1short"  TargetType="Paragraph">
    <Setter Property="Margin" Value="15,4,0,0" />
</Style>
<Style x:Key="Par2"  TargetType="Paragraph">
    <Setter Property="Margin" Value="30,7,0,0" />
</Style>
<Style x:Key="Par2short"  TargetType="Paragraph">
    <Setter Property="Margin" Value="30,4,0,0" />
</Style>
<Style x:Key="Par3"  TargetType="Paragraph">
    <Setter Property="Margin" Value="45,7,0,0" />
</Style>
<Style x:Key="Par3short"  TargetType="Paragraph">
    <Setter Property="Margin" Value="45,4,0,0" />
</Style>
<Style x:Key="Par4"  TargetType="Paragraph">
    <Setter Property="Margin" Value="60,7,0,0" />
</Style>
<Style x:Key="Par4short"  TargetType="Paragraph">
    <Setter Property="Margin" Value="60,4,0,0" />
</Style>
<Style x:Key="Par5"  TargetType="Paragraph">
    <Setter Property="Margin" Value="75,7,0,0" />
</Style>
<Style x:Key="Par5short"  TargetType="Paragraph">
    <Setter Property="Margin" Value="75,4,0,0" />
</Style>

Now the word Paragraph: before every paragraph I don't think you can do that with styles




回答2:


The short answer is that you can't apply templates to these elements because they don't inherit from ControlTemplate.



来源:https://stackoverflow.com/questions/28680625/wpf-flowdocument-how-can-i-define-custom-templates-for-the-child-paragraph-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!