WPF FlowDocument의 데이터 반복 항목
-
06-07-2019 - |
문제
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에 제목 및 할당 속성으로 클래스를 제공하는 대신 해당 클래스 목록을 제공하고 FlowDocument에 각 개체를 표시합니다. 누군가가 FlowDocument에서 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: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 제어 기능이있는 작은 샘플입니다.
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>
도움이 되었기를 바랍니다 :)
제휴하지 않습니다 StackOverflow