代码之家  ›  专栏  ›  技术社区  ›  ajma

重复WPF FlowDocument中的数据绑定项

  •  3
  • ajma  · 技术社区  · 16 年前

    <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Paragraph FontFamily="Georgia">
            <StackPanel>
                <TextBlock Text="{Binding Path=Title}"/>
                <TextBlock Text="{Binding Path=AssignedTo}"/>
            </StackPanel>
        </Paragraph>
    </FlowDocument>
    

    我不想给DataContext一个带有Title和AssignedTo属性的类,而是想给它一个该类的列表,并让flowdocument显示每个对象。有人能告诉我如何在flowdocument中形成XAML来实现这一点吗?

    1 回复  |  直到 16 年前
        1
  •  10
  •   Anvaka    16 年前

    谁知道呢,也许像下面的代码示例这样简单的代码对您也适用,Ajma:

    <Window x:Class="WpfTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WpfTest="clr-namespace:WpfTest"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"
        Title="Bound Inlines Sample" Height="300" Width="300">
      <Window.Resources>
        <Collections:ArrayList x:Key="array">
          <System:String>Hello</System:String>
          <System:String>World</System:String>
          <System:String>!</System:String>
        </Collections:ArrayList>
      </Window.Resources>
      <Grid>
        <FlowDocumentReader>
          <FlowDocument>
            <Paragraph FontFamily="Georgia">
                <ItemsControl ItemsSource="{StaticResource array}"/>            
            </Paragraph>
          </FlowDocument>
        </FlowDocumentReader>
      </Grid>
    </Window>
    

    如果不是,您可以始终创建自己的附加属性,并在属性更改通知中执行您可以想象的任何操作。下面是一个带有跨度控制的小示例:

    public class SpanOperations : DependencyObject
    {
      public static IEnumerable GetInlineSource(DependencyObject obj)
      {
        return (IEnumerable)obj.GetValue(InlineSourceProperty);
      }
    
      public static void SetInlineSource(DependencyObject obj, IEnumerable value)
      {
        obj.SetValue(InlineSourceProperty, value);
      }
    
      public static readonly DependencyProperty InlineSourceProperty =
          DependencyProperty.RegisterAttached("InlineSource", typeof(IEnumerable), typeof(SpanOperations), new UIPropertyMetadata(null, OnInlineSourceChanged));
    
      private static void OnInlineSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
        var span  = d as Span;
        if (span == null)
        {
          // It's a demo only. Can work with only spans... 
          return;
        }
        span.Inlines.Clear();
    
        var inlines = e.NewValue as IEnumerable;
        if (inlines != null)
        {
          foreach (var inline in inlines)
          {
            // We assume only inlines will come in collection:
            span.Inlines.Add(inline as Inline);
          }
    
        }
      }
    }
    

    XAML

    <Window x:Class="WpfTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WpfTest="clr-namespace:WpfTest"
        xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"
        Title="Bound Inlines Sample" Height="300" Width="300">
      <Window.Resources>
        <Collections:ArrayList x:Key="array">
          <Run>Hello</Run>
          <LineBreak/>
          <Run>Hello</Run>
          <LineBreak/>
          <Bold>
            <Run>Hello</Run>
          </Bold>
        </Collections:ArrayList>
      </Window.Resources>
      <Grid>
        <FlowDocumentReader>
          <FlowDocument>
            <Paragraph FontFamily="Georgia">
              <Span WpfTest:SpanOperations.InlineSource="{Binding Source={StaticResource array}}"/>
            </Paragraph>
          </FlowDocument>
        </FlowDocumentReader>
      </Grid>
    </Window>
    

    希望这有帮助:)