我有一个包含绑定到我的ViewModel的东西的FlowDocument,如下所示:
<FlowDocumentReader>
<FlowDocument>
<Paragraph>
<Run Text="{Binding MyTextProperty}"/>
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
现在我想使用某种DataTemplate显示类的List,但不知道如何启动。说我得到了一个类:
public MyClass
{
String Title {get;set;}
String FlowText {get;set;}
}
public List<MyClass> MyList {get;set;}
我想将它绑定到FlowDocument列表,如下所示:
<FlowDocumentReader>
<FlowDocument>
<List Items="{Binding MyList}">
<Bold><Run Text="{Binding Title}"/></Bold>
<LineBreak/>
<Run Text="{Binding FlowText}"/>
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
当然这不起作用 - 但我找不到任何解释如何使用模板绑定FlowDocument中的列表 - 这可能吗?
答案 0 :(得分:6)
请参阅this question。
我认为你有两个选择
<强>更新强>
使用两个附加属性的更动态的解决方案。带有模板的资源被添加到段落中(必须设置x:Shared="False"
属性,否则我们将一遍又一遍地添加相同的元素)。然后将源列表和模板资源的名称设置为附加属性。
在PropertyChanged回调中,检查是否设置了两个属性,然后为List中的每个项创建Span
元素。 span元素DataContext
设置为当前项以使Bindings正常工作
<FlowDocumentReader xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib">
<FlowDocument>
<Paragraph behaviors:ParagraphInlineBehavior.ParagraphInlineSource="{Binding MyList}"
behaviors:ParagraphInlineBehavior.TemplateResourceName="inlineTemplate">
<Paragraph.Resources>
<Collections:ArrayList x:Shared="False" x:Key="inlineTemplate">
<Bold>
<Run Text="{Binding Title}"/>
</Bold>
<LineBreak/>
<Run Text="{Binding FlowText}"/>
<LineBreak/>
</Collections:ArrayList>
</Paragraph.Resources>
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
<强> ParagraphInlineBehavior 强>
public class ParagraphInlineBehavior : DependencyObject
{
public static readonly DependencyProperty TemplateResourceNameProperty =
DependencyProperty.RegisterAttached("TemplateResourceName",
typeof(string),
typeof(ParagraphInlineBehavior),
new UIPropertyMetadata(null, OnParagraphInlineChanged));
public static string GetTemplateResourceName(DependencyObject obj)
{
return (string)obj.GetValue(TemplateResourceNameProperty);
}
public static void SetTemplateResourceName(DependencyObject obj, string value)
{
obj.SetValue(TemplateResourceNameProperty, value);
}
public static readonly DependencyProperty ParagraphInlineSourceProperty =
DependencyProperty.RegisterAttached("ParagraphInlineSource",
typeof(IEnumerable),
typeof(ParagraphInlineBehavior),
new UIPropertyMetadata(null, OnParagraphInlineChanged));
public static IEnumerable GetParagraphInlineSource(DependencyObject obj)
{
return (IEnumerable)obj.GetValue(ParagraphInlineSourceProperty);
}
public static void SetParagraphInlineSource(DependencyObject obj, IEnumerable value)
{
obj.SetValue(ParagraphInlineSourceProperty, value);
}
private static void OnParagraphInlineChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Paragraph paragraph = d as Paragraph;
IEnumerable inlines = ParagraphInlineBehavior.GetParagraphInlineSource(paragraph);
string templateName = ParagraphInlineBehavior.GetTemplateResourceName(paragraph);
if (inlines != null && templateName != null)
{
paragraph.Inlines.Clear();
foreach (var inline in inlines)
{
ArrayList templateList = paragraph.FindResource(templateName) as ArrayList;
Span span = new Span();
span.DataContext = inline;
foreach (var templateInline in templateList)
{
span.Inlines.Add(templateInline as Inline);
}
paragraph.Inlines.Add(span);
}
}
}
}