将FlowDocument中的模板绑定到List <foo>?</foo>

时间:2012-07-14 01:24:26

标签: c# wpf xaml flowdocument attachedbehaviors

我有一个包含绑定到我的ViewModel的东西的FlowDocument(我从互联网上获得):

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绑定到我的列表中。

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

0 个答案:

没有答案