我使用此控件Here能夠託管可以綁定到列表並生成相應的行的表,但工作得很好,但是在打印到XPS或PDF文件,它確實出現在FlowDocument上,但它打印爲空白,我試圖改變背景和前景的顏色沒有成功,有什麼建議?WYSIWYG所見即所得但不打印
0
A
回答
0
我剛剛成功實現了該示例(Create Flexible UIs With Flow Documents And Data Binding),並且確實遇到了與您相同的問題。
問題是BindableRun
的DataContext
未被正確設置。如本文所述,您需要修復上下文(使用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>
相關問題
- 1. WYSIWYG所見即所得可以限制文本/高度
- 2. 所見即所得vs所見即所得
- 3. 用PHP打印所見即所得的HTML格式的內容
- 4. 所見即所得的HTML表單打印
- 5. 在線所見即所得編輯器,但輸出用於打印
- 6. 所見即所得和XSS
- 7. 所見即所得編碼
- 8. 樂譜所見即所得
- 9. Insert Bootstrap所見即所得
- 10. jQuery UI所見即所得?
- 11. 所見即所得的JavaScript
- 12. 所見即所得在Chrome
- 13. Magento 1.7所見即所得
- 14. Android - 是不是Eclipse所見即所得?
- 15. Summernote所見即所得編輯器不可見
- 16. 自動摘錄所見即所得
- 17. 所見即所得沒有HTML標籤?
- 18. 所見即所得的編輯按鈕
- 19. TWIKI所見即所得編輯器
- 20. 最佳ASP.net所見即所得
- 21. 所見即所得html編輯查詢
- 22. Drupal所見即所得自動刪除
- 23. Google Wave像所見即所得
- 24. Asp.net MVC所見即所得EditorTemplate
- 25. 所見即所得刪除標記
- 26. JavaScript所見即所得的實現
- 27. 所見即所得/ PageDown混合?
- 28. Rails或Jquery所見即所得
- 29. 所見即所得XML編輯器java
- 30. jQuery + InnovaStudio所見即所得編輯器
我會考慮,如果你不是太流行起來沉沒成本,切換到CSS,因爲它允許打印模板。 –
@Gregory:不確定如何在WPF'FlowDocument'中使用CSS會有所幫助。 – user7116
不知道,除了這個問題真的讓我很困惑,打印機怎麼看不到我的表 - 在控制之內 - 並且可以看到沒有問題的其他控件 – Musaab