在绑定的FlowDocument的列表列出< MyClass的&GT ;? [英] Binding a list in a FlowDocument to List<MyClass>?

查看:324
本文介绍了在绑定的FlowDocument的列表列出< MyClass的&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须绑定到我的ViewModel这样含有的FlowDocument的东西:

I have a FlowDocument containing stuff bound to my ViewModel like this:

<FlowDocumentReader>
  <FlowDocument>
    <Paragraph>
      <Run Text="{Binding MyTextProperty}"/>
    </Paragraph>
  </FlowDocument>
</FlowDocumentReader>

现在我想显示使用某种DataTemplate中的类的列表中,但有不知道如何下手。说我有一个像类:

Now I want to display a List of class using some kind of DataTemplate, but got no idea how to start. Say I got a class like:

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

public List<MyClass> MyList {get;set;}

和我想这个绑定到FlowDocument的列表,像这样:

And I'd like to bind this to the FlowDocument List, like this:

<FlowDocumentReader>
  <FlowDocument>
    <List Items="{Binding MyList}">
      <Bold><Run Text="{Binding Title}"/></Bold>
      <LineBreak/>
      <Run Text="{Binding FlowText}"/>
    </Paragraph>
  </FlowDocument>
</FlowDocumentReader>

当然,这并不工作 - 但我找不到如何绑定列表中的FlowDocument使用模板任何解释 - 这是可能的。

Of course this does not work - but I can't find any explanation how to bind Lists in a FlowDocument using Templates - is this possible?

推荐答案

请参见这个问题

我觉得你有两个选择


  • 使用的ItemsControl

  • 使用附加属性

更新结果
使用两个附加属性更加动态的解决方案。与模板的资源将被添加到第(其中必须有 X:共享=FALSE属性设置,否则我们就在不断添加相同的元素和过度)。那么源列表和模板资源的名称设置为附加属性。

Update
A more Dynamic solution using two attached properties. A Resource with the Template is added to the Paragraph (which must have the x:Shared="False" attribute set, otherwise we'll just keep adding the same elements over and over). Then the Source List and the name of the template resource is set as attached properties.

在回调的PropertyChanged,一检查,这两个属性都设置,然后针对列表中的每个项目创建了一个跨度元素。 的DataContext 设置为当前项目的范围元素,使绑定工作

In the PropertyChanged callback, a check is made that both properties are set and then a Span element is created for each item in the List. The span elements DataContext is set to the current item to make the Bindings work

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

这篇关于在绑定的FlowDocument的列表列出&LT; MyClass的&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆