1

我有一个 FlowDocument,其中包含像这样绑定到我的 ViewModel 的内容(我从互联网上获得的):

public class Foo
{
    public string Text { get; set; }
}

public MainWindow()
{
    InitializeComponent();

    List<Foo> foos = new List<Foo>();
    for (int i = 0; i < 20; i++) {
        foos.Add(new Foo() { Text = i.ToString() });
    }

    this.DataContext = foos;
}

我正在尝试在 FlowDocument 中绑定我的列表 foos。

<FlowDocumentReader>
    <FlowDocument> 
        <Paragraph behaviors:ParagraphInlineBehavior.ParagraphInlineSource="{Binding}" 
                   behaviors:ParagraphInlineBehavior.TemplateResourceName="inlineTemplate"> 
            <Paragraph.Resources> 
                <DataTemplate x:Key="inlineTempalte">
                    <TextBlock Text="{Binding Text}" />
                </DataTemplate>
            </Paragraph.Resources> 
        </Paragraph> 
    </FlowDocument> 
</FlowDocumentReader> 

还有这个课程,我从互联网上获得的ParagraphInlineBehavior,但我还没有完成我的目标。(在流文档中显示数据模板)。

OnParagraphInlineChanged方法有问题。

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); 
            } 
        } 
    } 
}
4

0 回答 0