WPF FlowDocument :: How Can I Define Custom Templa

2019-09-07 09:50发布

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.

2条回答
\"骚年 ilove
2楼-- · 2019-09-07 10:28

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

查看更多
看我几分像从前
3楼-- · 2019-09-07 10:29

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

查看更多
登录 后发表回答