无法获取XPS渲染的FlowDocument以更新数据绑定的元素/字段

时间:2018-08-17 20:47:14

标签: wpf data-binding xpsdocument

在下面的代码段中,与视图FlowDocument关联的CustomFormPreviewView具有TextBlock元素CustomFormTextField,以两种方式之一绑定:

// Binding Option 1 - FlowDocument fields populated on XPS
view.ShowDialog();

// Binding Option 2 - FlowDocument fields NOT populated on XPS
view.UpdateLayout();

只有第一个选项(显示实际对话框)会导致后续XPS生成包含填充的文本字段/元素,即使两个调用都导致对CustomFormsTemplateTable.UpdateBindingsInTableCell的调用(下面的堆栈跟踪)。

FlowDocument

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:vm="clr-namespace:ViewModels;assembly=ViewModels" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xml:space="preserve" PageWidth="816" PageHeight="1056" PagePadding="48,48,48,48" AllowDrop="True">
  ...
    <Paragraph>
    <Run FontSize="14" xml:space="preserve" />
    <vm:CustomFormsTextField SerializableFieldName="Entity.AssignedTo" SerializableDisplayName="Assigned to" SerializableStringFormat="" IsInDesignMode="{Binding Path=IsInDesignMode}" FontFamily="Arial" FontStyle="Normal" FontWeight="Normal" FontSize="14" Foreground="#FF000000" TextWrapping="Wrap">
      <vm:CustomFormsTextField.TextDecorations>
        <TextDecorationCollection />
      </vm:CustomFormsTextField.TextDecorations>
    </vm:CustomFormsTextField>
  </Paragraph>
  ...
</FlowDocument>

CustomFormsTextField

public class CustomFormsTextField : TextBlock
{
    ...
}

CustomFormViewModel

public XpsDocument XpsDocument
{
    get
    {       
        return CustomFormHelper.ConvertFlowDocumentToXpsDocument(this.FlowDocument);
    }
}

XPS生成数据绑定FlowDocument

var view = new CustomFormPreviewView();
view.ViewModel = new CustomFormViewModel(Entity.form_id, (int)Record);

FixedDocumentSequence sequence;
sequence = view.ViewModel.XpsDocument.GetFixedDocumentSequence();

// Binding Option 1 - FlowDocument fields populated on XPS
view.ShowDialog();

// Binding Option 2 - FlowDocument fields NOT populated on XPS
view.UpdateLayout();

using (var xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(path_XPS, System.IO.FileAccess.Write, System.IO.Packaging.CompressionOption.SuperFast))
{
    var writer = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(xpsDoc);

    if (sequence != null)
    {
        sequence.References.Select(r => r.GetDocument(true)).ToList().ForEach(doc => doc.Pages.ToList().ForEach(page => page.UpdateLayout()));
    }

    writer.Write(sequence);
}

在下面的堆栈跟踪中,两个选项的“ WPF绑定”部分相同,但是“窗口渲染”部分不同。 窗口渲染过程如何更新FlowDocument,以便填充绑定的数据元素(CustomFormTextField)?

堆栈跟踪-绑定选项1

// WPF Binding ////////////////////////////////////////////////////////////////////////////////////////////////////////
ViewModels.dll!ViewModels.CustomFormsTemplateTable.UpdateBindingsInTableCell(System.Windows.Documents.TableCell cell, int row) Line 225 C#  Symbols loaded.
ViewModels.dll!ViewModels.CustomFormsTemplateTable.DrawRows(System.Collections.IEnumerable rows) Line 172   C#  Symbols loaded.
ViewModels.dll!ViewModels.CustomFormsTemplateTable.Draw(System.Collections.IEnumerable rows) Line 153   C#  Symbols loaded.
ViewModels.dll!ViewModels.CustomFormsTemplateTable.OnSerializableItemsSourceChanged(System.Windows.DependencyObject o, System.Windows.DependencyPropertyChangedEventArgs e) Line 126    C#  Symbols loaded.
WindowsBase.dll!System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)  Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.FrameworkContentElement.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Documents.TextElement.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)   Unknown No symbols loaded.
WindowsBase.dll!System.Windows.DependencyObject.NotifyPropertyChange(System.Windows.DependencyPropertyChangedEventArgs args)    Unknown No symbols loaded.
WindowsBase.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType) Unknown No symbols loaded.
WindowsBase.dll!System.Windows.DependencyObject.InvalidateProperty(System.Windows.DependencyProperty dp, bool preserveCurrentValue) Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.Invalidate(bool isASubPropertyChange)   Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpression.TransferValue(object newValue, bool isASubPropertyChange)   Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpression.Activate(object item)   Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpression.AttachToContext(System.Windows.Data.BindingExpression.AttachAttempt attempt)    Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(bool lastChance) Unknown No symbols loaded.
PresentationFramework.dll!MS.Internal.Data.DataBindEngine.Task.Run(bool lastChance) Unknown No symbols loaded.
PresentationFramework.dll!MS.Internal.Data.DataBindEngine.Run(object arg)   Unknown No symbols loaded.
// Window Rendering ///////////////////////////////////////////////////////////////////////////////////////////////////
PresentationCore.dll!System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()    Unknown No symbols loaded.
PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayout() Unknown No symbols loaded.
PresentationCore.dll!System.Windows.Interop.HwndSource.SetLayoutSize()  Unknown No symbols loaded.
PresentationCore.dll!System.Windows.Interop.HwndSource.RootVisualInternal.set(System.Windows.Media.Visual value)    Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Window.SetRootVisual() Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Window.SetRootVisualAndUpdateSTC() Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Window.SetupInitialState(double requestedTop, double requestedLeft, double requestedWidth, double requestedHeight) Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Window.CreateSourceWindow(bool duringShow) Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Window.ShowHelper(object booleanBox)   Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Window.ShowDialog()    Unknown No symbols loaded.

堆栈跟踪-绑定选项2

// WPF Binding ////////////////////////////////////////////////////////////////////////////////////////////////////////
ViewModels.dll!ViewModels.CustomFormsTemplateTable.UpdateBindingsInTableCell(System.Windows.Documents.TableCell cell, int row) Line 236 C#  Symbols loaded.
ViewModels.dll!ViewModels.CustomFormsTemplateTable.DrawRows(System.Collections.IEnumerable rows) Line 172   C#  Symbols loaded.
ViewModels.dll!ViewModels.CustomFormsTemplateTable.Draw(System.Collections.IEnumerable rows) Line 153   C#  Symbols loaded.
ViewModels.dll!ViewModels.CustomFormsTemplateTable.OnSerializableItemsSourceChanged(System.Windows.DependencyObject o, System.Windows.DependencyPropertyChangedEventArgs e) Line 126    C#  Symbols loaded.
WindowsBase.dll!System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)  Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.FrameworkContentElement.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Documents.TextElement.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)   Unknown No symbols loaded.
WindowsBase.dll!System.Windows.DependencyObject.NotifyPropertyChange(System.Windows.DependencyPropertyChangedEventArgs args)    Unknown No symbols loaded.
WindowsBase.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType) Unknown No symbols loaded.
WindowsBase.dll!System.Windows.DependencyObject.InvalidateProperty(System.Windows.DependencyProperty dp, bool preserveCurrentValue) Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.Invalidate(bool isASubPropertyChange)   Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpression.TransferValue(object newValue, bool isASubPropertyChange)   Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpression.Activate(object item)   Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpression.AttachToContext(System.Windows.Data.BindingExpression.AttachAttempt attempt)    Unknown No symbols loaded.
PresentationFramework.dll!System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(bool lastChance) Unknown No symbols loaded.
PresentationFramework.dll!MS.Internal.Data.DataBindEngine.Task.Run(bool lastChance) Unknown No symbols loaded.
PresentationFramework.dll!MS.Internal.Data.DataBindEngine.Run(object arg)   Unknown No symbols loaded.
// Window Rendering ///////////////////////////////////////////////////////////////////////////////////////////////////
PresentationCore.dll!System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()    Unknown No symbols loaded.
PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayout() Unknown No symbols loaded.

注意::在GetFixedDocumentSequence似乎导致填充了元素/字段之后,再次调用UpdateLayout(仅调用UpdateLayout,{{1 }}也不起作用):

GetFixedDocumentSequence

我仍然想知道如何强制元素(sequence = view.ViewModel.XpsDocument.GetFixedDocumentSequence(); view.UpdateLayout(); sequence = view.ViewModel.XpsDocument.GetFixedDocumentSequence(); )更新其绑定数据(对此有直接的公开方法吗?)

0 个答案:

没有答案