【发布时间】:2017-11-02 23:33:24
【问题描述】:
我有一个 C# Wpf 项目,我已在其中成功加载了 Xps。文件到文档查看器。我希望能够在我的 C# 代码中包含一个变量,当您滚动文档时该变量会注意到页面更改。 到目前为止,我已经发现,xaml 代码有一个函数,如果您滚动到下一页,它会自动更改页码:
<DocumentViewer x:Name="viewDocument" HorizontalAlignment="Left" VerticalAlignment="Top" Height="Auto" Grid.Row="0" Grid.Column="0" >
<FixedDocument></FixedDocument>
</DocumentViewer>
<TextBlock Text="{Binding ElementName=viewDocument,Path=MasterPageNumber}" Grid.Row="1"/>
我的最终目标是停止用户在每个页面上花费的时间,这就是为什么我需要能够将当前页码与我的代码中的一个变量连接起来,而这在上面的示例中是无法做到的。我试图实现 INotifyPropertyChanged,但我对 C# 还很陌生,我找不到错误。它将变量设置为第一页,但之后它不会更新。
这是我的视图模型:
using System; using System.ComponentModel;
namespace Tfidf_PdfOnly {
public class MainViewModel : INotifyPropertyChanged
{
private int _myLabel;
public int MyLabel
{
get
{
return this._myLabel;
}
set
{
this._myLabel = value;
NotifyPropertyChanged("MyLabel");
}
}
public MainViewModel()
{
_myLabel = 55;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
这是在我的 Document_Viewer.xaml.cs 文件中
XpsDocument document1 = new XpsDocument(path, System.IO.FileAccess.Read);
//load the file into the viewer
viewDocument.Document = document1.GetFixedDocumentSequence();
MainViewModel vm = new MainViewModel();
this.DataContext = vm;
vm.MyLabel = viewDocument.MasterPageNumber;
为了查看它是否有效,我将它绑定到 UI 上的标签:
<DocumentViewer x:Name="viewDocument" HorizontalAlignment="Left" VerticalAlignment="Top" Height="Auto" Grid.Row="0" Grid.Column="0" >
<FixedDocument></FixedDocument>
</DocumentViewer>
<TextBlock Text="{Binding MyLabel, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" Grid.Row="1" HorizontalAlignment="Right"/>
我希望我的问题很清楚,任何帮助都值得赞赏!
【问题讨论】:
标签: c# wpf xaml documentviewer