2011-06-28 54 views
0

我使用此控件Here能夠託管可以綁定到列表並生成相應的行的表,但工作得很好,但是在打印到XPS或PDF文件,它確實出現在FlowDocument上,但它打印爲空白,我試圖改變背景和前景的顏色沒有成功,有什麼建議?WYSIWYG所見即所得但不打印

+0

我會考慮,如果你不是太流行起來沉沒成本,切換到CSS,因爲它允許打印模板。 –

+0

@Gregory:不確定如何在WPF'FlowDocument'中使用CSS會有所幫助。 – user7116

+0

不知道,除了這個問題真的讓我很困惑,打印機怎麼看不到我的表 - 在控制之內 - 並且可以看到沒有問題的其他控件 – Musaab

回答

0

我剛剛成功實現了該示例(Create Flexible UIs With Flow Documents And Data Binding),並且確實遇到了與您相同的問題。

問題是BindableRunDataContext未被正確設置。如本文所述,您需要修復上下文(使用FixupDataContext幫助器方法),將DataContext設置爲FrameworkContentElement,並清除您先前「修復」的上下文(使用UnFixupDataContext幫助程序方法)。 執行這些語句的順序至關重要。。重新閱讀文章並確保您瞭解它;我不得不重新閱讀它幾次,並研究代碼才能真正理解他在說什麼。

我已經執行了一步,並添加了對數據綁定TableCell中其他元素的支持。這些更改包括使用附加屬性來標識具有其DataContext「固定」的元素,然後將輔助方法展開爲還針對FrameworkElements以及FrameworkContentElements

HTH,

編輯:

public static class DataContextHelper 
{ 

    #region UseAncestorDataContext 

    public static readonly DependencyProperty UseAncestorDataContextProperty = DependencyProperty.RegisterAttached("UseAncestorDataContext", typeof(bool), typeof(DataContextHelper), 
                        new FrameworkPropertyMetadata(false, DataContextHelper.OnUseAncestorDataContextChanged)); 

    public static bool GetUseAncestorDataContext(DependencyObject d) 
    { 
     return (bool)d.GetValue(UseAncestorDataContextProperty); 
    } 

    public static void SetUseAncestorDataContext(DependencyObject d, bool value) 
    { 
     d.SetValue(UseAncestorDataContextProperty, value); 
    } 

    private static void OnUseAncestorDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     if ((bool)e.NewValue) 
      UseAncestorDataContext(d); 
    } 

    #endregion 

    /// <summary> 
    /// If you use a Bindable flow document element more than once, you may encounter a "Collection was modified" 
    /// exception. The error occurs when the binding is updated because of a change to an inherited dependency 
    /// property. The most common scenario is when the inherited DataContext changes. It appears that an inherited 
    /// properly like DataContext is propagated to its descendants. When the enumeration of descendants gets to 
    /// a Bindable, the dependency properties of that element change according to the new DataContext, which change 
    /// the (non-dependency) properties. However, for some reason, changing the flow content invalidates the enumeration 
    /// and raises an exception. 
    /// </summary>   
    public static void UseAncestorDataContext(DependencyObject element) 
    {    
     if (element is FrameworkContentElement) 
     { 
      FrameworkContentElement contentElement = (FrameworkContentElement)element; 
      Binding binding = new Binding(FrameworkContentElement.DataContextProperty.Name); 
      binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FrameworkElement), 1); 
      contentElement.SetBinding(FrameworkContentElement.DataContextProperty, binding); 
     } 
     else if (element is FrameworkElement) 
     { 
      FrameworkElement frameworkElement = (FrameworkElement)element; 
      Binding binding = new Binding(FrameworkElement.DataContextProperty.Name); 
      binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FrameworkElement), 1); 
      frameworkElement.SetBinding(FrameworkContentElement.DataContextProperty, binding); 
     } 
    } 

    public static void ClearDataContextBinding(DependencyObject d) 
    { 
     while (RestoreDataContextRecursive(d));    
    } 

    private static bool RestoreDataContextRecursive(DependencyObject d) 
    {    
     if (d is FrameworkContentElement && GetUseAncestorDataContext(d)) 
     { 
      Binding binding = BindingOperations.GetBinding(d, FrameworkContentElement.DataContextProperty); 
      if (binding != null && binding.Path != null && binding.Path.Path == FrameworkContentElement.DataContextProperty.Name 
       && binding.RelativeSource != null && binding.RelativeSource.Mode == RelativeSourceMode.FindAncestor && binding.RelativeSource.AncestorType == typeof(FrameworkElement) && binding.RelativeSource.AncestorLevel == 1) 
      { 
       BindingOperations.ClearBinding(d, FrameworkContentElement.DataContextProperty); 
       return true; 
      } 
     } 
     else if (d is FrameworkElement && GetUseAncestorDataContext(d)) 
     { 
      Binding binding = BindingOperations.GetBinding(d, FrameworkElement.DataContextProperty); 
      if (binding != null && binding.Path != null && binding.Path.Path == FrameworkElement.DataContextProperty.Name 
       && binding.RelativeSource != null && binding.RelativeSource.Mode == RelativeSourceMode.FindAncestor && binding.RelativeSource.AncestorType == typeof(FrameworkElement) && binding.RelativeSource.AncestorLevel == 1) 
      { 
       BindingOperations.ClearBinding(d, FrameworkElement.DataContextProperty); 
       return true; 
      } 
     } 

     // As soon as we have disconnected a binding, return. Don't continue the enumeration, since the collection may have changed 
     foreach (object child in LogicalTreeHelper.GetChildren(d)) 
     { 
      if (child is DependencyObject && RestoreDataContextRecursive((DependencyObject)child)) 
       return true; 
     } 

     return false; 
    } 

} 

使用

<DataTemplate x:Key="PercentCellTemplate"> 
     <documents:ContentFragment> 
      <TableCell Style="{StaticResource TableCellStyle}" BorderThickness="0,0,1,0"> 
       <Paragraph> 
        <Rectangle Width="14" Height="14" VerticalAlignment="Center" Margin="0,6,0,0" Fill="{Binding Path=Result, Mode=OneWay, Converter={StaticResource MappingConverterResultEnumToIconResource}}" 
           documents:DataContextHelper.UseAncestorDataContext="True"/> 
        <documents:DocumentRun Style="{StaticResource ReportDocument_NormalRunStyle}" Text="{Binding Path=Percent, Mode=OneWay, StringFormat={}{0:0.000}%}" 
              BaselineAlignment="Center" documents:DataContextHelper.UseAncestorDataContext="True" /> 
       </Paragraph> 
      </TableCell> 
     </documents:ContentFragment> 
    </DataTemplate> 
+0

感謝您回覆此問題,我不確定爲什麼我沒有解決它,因爲我解決了這個問題,是的,它確實是datacontext,我認爲他們應該考慮在他們的下一個.NET版本 – Musaab

+0

不客氣。在那裏生成/打印流動文檔的方法明顯缺乏解決方案,因此我花了一個星期的時間爲自己找出答案後,可以爲之付出更多的努力。我剛剛編輯了答案,以包含我的版本的helper方法,這些方法也修復了'FrameworkElements'上的綁定。 – Dennis