gpt4 book ai didi

wpf - 在 WPF FlowDocument 中重复数据绑定(bind)项

转载 作者:行者123 更新时间:2023-12-04 17:13:53 25 4
gpt4 key购买 nike

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

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

最佳答案

谁知道呢,也许像下面的代码示例一样简单,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 控件的小示例:

客服:
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 - 在 WPF FlowDocument 中重复数据绑定(bind)项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1615925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com