2009-10-23 121 views
3

我在WPF中的FlowDocument,看起來像這樣:重複數據綁定項WPF的FlowDocument

<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的標題和AssignedTo屬性的類,我想給它的列表該類,並有flowdocument顯示每個對象。有人可以告訴我如何在flowdocument中形成XAML來執行此操作嗎?

+0

好像[這個問題的副本](http://stackoverflow.com/questions/1254633/is-there-an-itemscontrol-equivalent-for-text-content/1258900#1258900) – 2009-10-23 21:51:49

回答

8

誰知道,也許是簡單如下面的代碼示例會爲你工作,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> 

如果不是的話,你總是可以創建自己的附加屬性,做任何你可以在裏面財產想象改變通知。這裏是一個小樣本與跨度控制:

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> 

希望這有助於:)

+0

附加屬性規則,好回答安德烈! – 2009-10-25 02:11:13