我跳过你的第二个问题,因为它足够复杂,可以独立运行。
我遇到了同样的问题,但这可能是由几个不同的事情引起的。
如果问题是因为绑定还没有被“触发”,那么解决方案有点麻烦,但如果您控制 DataContext 类型,则很容易做到。您只需向您的类型添加一个公共或内部方法,该方法允许您为每个公共属性触发 PropertyChanged 事件。这是一个例子:
public interface IForceBinding : INotifyPropertyChanged
{
void ForceBindings();
}
public class MyDataContext : IForceBinding
{
public event PropertyChanged;
private string _text;
public string Text
{
get{return _text;}
set{_text = value; OnPropertyChanged("Text");}
}
public void ForceBindings()
{
OnPropertyChanged("Text");
}
private void OnPropertyChanged(string propertyName)
{
// you know the drill
}
}
然后,您可以这样使用它:
public void Print(MyDataContext preconfiguredContext){
var page = new MyWpfPage();
page.DataContext = preconfiguredContext;
preconfiguredContext.ForceBindings();
// write to xps
如果这不起作用,您可能会遇到第一页上的绑定永远不会显示的错误。在我重新找到解决方案之前,我必须挖掘一段时间。