2017-11-02 14 views
0

Xpsを正常に読み込んだC#Wpfプロジェクトがあります。ファイルをドキュメントビューアに追加します。私は、ドキュメントをスクロールするときにページの変化に気づく変数をC#コードに持たせたいと思っています。 これまでのところ私は、XAMLコードのための機能がありますが、次のページにスクロールした場合、それは自動的にページ番号を変更することを、考え出した:WPFドキュメントビューアの通知ページの変更

<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"/> 

私の最終目標は、ユーザが費やしたSTO pthe時間にあります私は上記の例ではできない私のコード内の変数と現在のページ番号を接続できるようにする必要がある理由です。私は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が、それは私がUI上のラベルにそれをバインド動作するかどうかを確認するには

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; 
ファイルにあります。

<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"/> 

私の質問がはっきりしていることを願っています。

答えて

0

DocumentViewerにはMasterPageNumberという名前のプロパティがあります(ドキュメントのページインデックスとしています)。次のサンプルでは、​​PrismとBlend SDK(ビヘイビア)を使用しています。コンバータはすばやく汚れています。タイミングに関しては、StopWatchインスタンスを使用して、ページの変更間隔を追跡することができます。

MVVMアプローチ

のViewModel

public class ShellViewModel : BindableBase 
{ 
    private int _currentPage; 

    public string Title => "Sample"; 

    public string DocumentPath => @"c:\temp\temp.xps"; 

    public int CurrentPage 
    { 
     get => _currentPage; 
     set => SetProperty(ref _currentPage, value); 
    } 

    public ICommand PageChangedCommand => new DelegateCommand<int?>(i => CurrentPage = i.GetValueOrDefault()); 
} 

ビュー

<Window x:Class="Poc.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:viewModels="clr-namespace:Poc.ViewModels" 
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
    xmlns:behaviors="clr-namespace:Poc.Views.Interactivity.Behaviors" 
    xmlns:converters="clr-namespace:Poc.Views.Converters" 
    xmlns:controls1="clr-namespace:Poc.Views.Controls" 
    mc:Ignorable="d" 
    Title="{Binding Title}" Height="350" Width="525"> 
<Window.Resources> 
    <converters:PathToDocumentConverter x:Key="PathToDocumentConverter"></converters:PathToDocumentConverter> 
</Window.Resources> 
<Window.DataContext> 
    <viewModels:ShellViewModel /> 
</Window.DataContext> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"></RowDefinition> 
     <RowDefinition Height="Auto"></RowDefinition> 
    </Grid.RowDefinitions> 
    <DocumentViewer Document="{Binding DocumentPath,Converter={StaticResource PathToDocumentConverter}}"> 
     <i:Interaction.Behaviors> 
      <behaviors:DocumentViewerBehavior PageViewChangedCommand="{Binding PageChangedCommand}"></behaviors:DocumentViewerBehavior> 
     </i:Interaction.Behaviors> 
    </DocumentViewer> 
    <TextBlock Grid.Row="1" Text="{Binding CurrentPage}"></TextBlock> 
</Grid> 

行動

public class DocumentViewerBehavior : Behavior<DocumentViewer> 
{ 
    public static readonly DependencyProperty PageViewChangedCommandProperty = DependencyProperty.Register(nameof(PageViewChangedCommand), typeof(ICommand), typeof(DocumentViewerBehavior)); 

    public ICommand PageViewChangedCommand 
    { 
     get => (ICommand)GetValue(PageViewChangedCommandProperty); 
     set => SetValue(PageViewChangedCommandProperty, value); 
    } 

    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     AssociatedObject.PageViewsChanged += OnPageViewsChanged; 
    } 

    private void OnPageViewsChanged(object sender, EventArgs e) => PageViewChangedCommand?.Execute(AssociatedObject.MasterPageNumber); 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 

     AssociatedObject.PageViewsChanged -= OnPageViewsChanged; 
    } 
} 

コンバータ

public class PathToDocumentConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var fileInfo = new FileInfo((string)value); 

     if (fileInfo.Exists) 
     { 
      if (String.Compare(fileInfo.Extension, ".XPS", StringComparison.OrdinalIgnoreCase) == 0) 
      { 
       return new XpsDocument(fileInfo.FullName, FileAccess.Read).GetFixedDocumentSequence(); 
      } 
     } 

     return value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
関連する問題