我有一个Silverlight应用程序,它使用Web服务来创建XPS文档。文档模板在WCF类库中创建为XAML控件。
public void GenerateXPS()
{
Type typeofControl = Type.GetType(DOCUMENT_GENERATOR_NAMESPACE + "." + ControlTypeName, true);
FrameworkElement control = (FrameworkElement)(Activator.CreateInstance(typeofControl));
control.DataContext = DataContext;
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
//Create first page of document
fixedPage.Children.Add(control);
((IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
XpsDocument xpsd = new XpsDocument(OutputFilePath + "\\" + OutputFileName, FileAccess.ReadWrite);
System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(fixedDoc);
xpsd.Close();
SaveToDocumentRepository();
}
为了将实际数据绑定到我的文档模板,我设置了控件的DataContext属性。问题是,当我查看我的XPS时,图像(我将我的图像控件的源绑定到表示我的图像的URL的字符串属性)不会显示为好像它们没有被加载。我怎么解决这个问题?谢谢!
答案 0 :(得分:1)
绑定基础结构可能需要推动,因为您的操作超出了WPF的预期用途。
尝试在设置datacontext后添加以下代码:
control.DataContext = DataContext;
// we need to give the binding infrastructure a push as we
// are operating outside of the intended use of WPF
var dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.Invoke(
DispatcherPriority.SystemIdle,
new DispatcherOperationCallback(delegate { return null; }),
null);
我在此blog post中介绍了这个和其他与XPS相关的内容。