0

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.

Scott Baker
  • 10,013
  • 17
  • 56
  • 102

2 Answers2

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

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • please see my edits; you answered the question I asked, but I really phrased my question poorly. – Scott Baker Feb 23 '15 at 18:56
  • You do know if you leave off the key a style will effect the TargetType. You question is still not clear to me. – paparazzo Feb 23 '15 at 19:00
  • So if you were to bind a bunch of POCO objects to a listbox, you could define a custom template to use to render each one, using whatever StackPanels, Grids, Textboxes etc. that you want. Can I do that to the Paragraph elements in a FlowDocument? – Scott Baker Feb 23 '15 at 19:05
  • @ScottSEA I don't think so. It is not like you are binding to a collection with and get to use items templates. But just because I don't know how does not mean it cannot be done. – paparazzo Feb 23 '15 at 19:07
0

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

Scott Baker
  • 10,013
  • 17
  • 56
  • 102