2015-02-23 49 views
0

我对WPF相当陌生,因此请耐心等待。WPF FlowDocument ::我如何为儿童段落,运行等定义自定义模板

我试图在RichTextBox(RTB)中自定义FlowDocument的各种元素的表示。 WPF控件是无形的,所以据我所知,我应该能够为Document中的每个孩子定义外观。

我会在RTB中为此定义模板吗?为每个项目(Paragraph,Section,Run等)分别为模板资源? 假设为了争辩起见,我希望在每个 Section周围都有一个红色边框,每个 Paragraph前带有 Margin="5",并且每个 Run都在Consolas中的单词“Paragraph:”。 (见编辑之一)

编辑一个

我在这里了解更多从询问比对一个错误问题。看起来我从来没有问过我第一次真正想要做什么。谁知道,也许我会学习。

我想要定义一个DataTemplateItemTemplate(我认为)类似于ListBox。最终目标是RTB中的“源视图”HTML编辑器。我想一个段落XAML元素来呈现这样的RTB:

<p> 
    Lorem ipsum stackus overflowum 
</p> 

...和运行将使这样的:

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

...等等的不同XAML元素。

回答

1

一些你可以做的样式
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> 

现在Word段落:每个段落之前,我不认为你可以做到这一点的风格

+0

请参阅我的编辑;你回答了我问的问题,但我真的很不好地表述了我的问题。 – 2015-02-23 18:56:11

+0

您知道,如果您离开关键点,样式会影响TargetType。你的问题仍然不清楚。 – Paparazzi 2015-02-23 19:00:40

+0

因此,如果您要将一堆POCO对象绑定到一个列表框,您可以使用自定义模板来渲染每个对象,使用任何您想要的StackPanels,Grids,Textboxes等。我可以这样做到FlowDocument中的段落元素吗? – 2015-02-23 19:05:16

0

简短的回答是,你不能应用模板这些元素,因为它们不继承来自ControlTemplate。