Frage

Ich habe eine Flowdocument in WPF, die wie folgt aussieht:

<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>

anstelle Datacontext eine Klasse mit dem Titel und AssignedTo Eigenschaft des Gebens, würde Ich mag, um ihm eine Liste dieser Klasse und haben die Flowdocument zeigen jede von ihnen Objekte. Kann mir jemand sagen, wie die XAML in der Flowdocument zu bilden, dies zu tun?

War es hilfreich?

Lösung

Wer weiß, vielleicht so einfach wie das folgende Codebeispiel wird für Sie arbeiten, 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>

Wenn es nicht ist, können Sie immer Ihre eigene angebrachte Eigenschaft erstellen und tun, was Sie sich vorstellen können innerhalb Eigenschaft Benachrichtigung geändert. Hier ist eine kleine Probe mit Span-Steuerung:

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>

Hope, das hilft:)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top