中重复数据绑定项

中重复数据绑定项

本文介绍了在 WPF FlowDocument 中重复数据绑定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 中有一个流文档,如下所示:

I have a flowdocument in WPF that looks like this:

<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 属性的类,我想给它一个该类的列表,并让流文档显示它们中的每个对象.有人能告诉我如何在流文档中形成 XAML 来做到这一点吗?

instead of giving the DataContext a class with the Title and AssignedTo property, I would like to give it a List of that class and have the flowdocument show each of them objects. Can someone tell me how to form the XAML in the flowdocument to do this?

推荐答案

谁知道呢,也许像下面的代码示例一样简单,Ajma:

Who knows, maybe as simple as the following code sample will work for you, 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>

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

If it's not, you can always create your own attached property and do whatever you can imagine inside property changed notification. Here is a small sample with Span control:

CS:

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>

希望这有帮助:)

这篇关于在 WPF FlowDocument 中重复数据绑定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:20