将FlowDocument中的列表绑定到List <MyClass>?

Sam*_*Sam 8 wpf binding mvvm flowdocument

我有一个包含绑定到我的ViewModel的东西的FlowDocument,如下所示:

<FlowDocumentReader>
  <FlowDocument>
    <Paragraph>
      <Run Text="{Binding MyTextProperty}"/>
    </Paragraph>
  </FlowDocument>
</FlowDocumentReader>
Run Code Online (Sandbox Code Playgroud)

现在我想使用某种DataTemplate显示类的List,但不知道如何启动.说我得到了一个类:

public MyClass
{
  String Title {get;set;}
  String FlowText {get;set;}
}

public List<MyClass> MyList {get;set;}
Run Code Online (Sandbox Code Playgroud)

我想将它绑定到FlowDocument列表,如下所示:

<FlowDocumentReader>
  <FlowDocument>
    <List Items="{Binding MyList}">
      <Bold><Run Text="{Binding Title}"/></Bold>
      <LineBreak/>
      <Run Text="{Binding FlowText}"/>
    </Paragraph>
  </FlowDocument>
</FlowDocumentReader>
Run Code Online (Sandbox Code Playgroud)

当然这不起作用 - 但我找不到任何解释如何使用模板绑定FlowDocument中的列表 - 这可能吗?

Fre*_*lad 6

看到这个问题.

我认为你有两种选择

  • 使用ItemsControl
  • 使用附属物


使用两个附加属性更新更动态的解决方案.带有模板的资源被添加到段落中(必须x:Shared="False"设置属性,否则我们将一遍又一遍地添加相同的元素).然后将源列表和模板资源的名称设置为附加属性.

在PropertyChanged回调中,检查是否设置了两个属性,然后Span为List中的每个项创建一个元素.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>
Run Code Online (Sandbox Code Playgroud)

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)