title: WPF Command DependencyProperty
body: <p>I am building a WPF wrapper to display PDFs using Acrobat (from <a href="https://wpfpdfviewer.codeplex.com/" rel="nofollow noreferrer">this</a> CodePlex project) </p>
  
  <p>The Codeplex project supplies enough samples on performing basic functions on the PDF Viewer, but lacks the functionalities of the WPF MVVM pattern. My WPF wrapper must expose dependency properties to modify the behaviour of the Acrobat PDF Viewer using Binding in Xaml.</p>
  
  <p>Normal binding to proprties such as Filename, PageNumber, etc. is fine, as I am doing the following:</p>
  
  <ol>
  <li><p>Create the Dependency Property (Can be implemented relatively easy from the codeplex project)</p>
  
  <pre><code>public static readonly DependencyProperty PdfPathProperty =
       DependencyProperty.Register("PdfPath", typeof(string), typeof(PdfViewer), new UIPropertyMetadata(null, LoadPDF));
  
  
  public string PdfPath {
      get {
          return (string)GetValue(PdfPathProperty);
      }
      set {
          SetValue(PdfPathProperty, value);
      }
  }
  
  private static void LoadPDF(DependencyObject d, DependencyPropertyChangedEventArgs e) {
      var control = d as PdfViewer;
      if (control == null) return;
      control.LoadFile();
  }
  
  private void LoadFile() {
      _winFormPdfHost.LoadFile(PdfPath);
  }
  </code></pre></li>
  <li><p>Where you use the UserControl, bind to the Dependency Property:</p>
  
  <pre><code>    &lt;viewer:PdfViewer x:Name="viewer" PdfPath="{Binding FileName}" 
                        ShowToolbar="{Binding ShowToolbar, UpdateSourceTrigger=PropertyChanged}"
                        PageNumber="{Binding PageNumber, UpdateSourceTrigger=PropertyChanged}"&gt;
      &lt;/viewer:PdfViewer&gt;
  </code></pre></li>
  </ol>
  
  <p>This is all working as expected. However, I need to somehow pass commands through to the Wrapper. For instance, I need to trigger an action/command such as 'GoToFirstPage', and this is where I am stuck. </p>
  
  <p>I want to do something like the following:</p>
  
  <pre><code>        &lt;viewer:PdfViewer x:Name="viewer" PdfPath="{Binding FileName}" 
                            ShowToolbar="{Binding ShowToolbar, UpdateSourceTrigger=PropertyChanged}"
                            PageNumber="{Binding PageNumber, UpdateSourceTrigger=PropertyChanged}"
                            FirstPageCommand="{Binding GoToFirstPageCommand}"&gt;
          &lt;/viewer:PdfViewer&gt; 
  </code></pre>
  
  <p>However this approach wont work, because I don't want to implement the functionality of the command in the ViewModel. I just want to execute the function in the Wrapper. </p>
  
  <p>I basically want to build a custom toolbar which will cater for the logic instead of using the toolbar that Acrobat offers, and I will need to be able to pass through, or trigger commands in the wrapper from the outside somehow.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I can accomplish this by ignoring MVVM, and only expose a public method from the Wrapper:</p>
  
  <ol>
  <li><p>In the wrapper - Remove the Dependency Property for the Command, and only expose the function:</p>
  
  <pre><code>public void GoToFirstPage() {
      _winFormPdfHost.GoToFirstPage();
  }
  </code></pre></li>
  <li><p>From the ViewModel, access the method through the UserControl</p>
  
  <pre><code>private ICommand _FirstPageCommand;
  public ICommand FirstPageCommand {
      get {
          if (_FirstPageCommand == null) {
              _FirstPageCommand = CreateCommand(GoToFirstPage);
          }
          return _FirstPageCommand;
      }
  }
  
  public void GoToFirstPage(object obj) {
      var viewer = obj as WPFPdfViewer.PdfViewer;
      viewer.GoToFirstPage();
  }
  </code></pre></li>
  </ol>
  
  <p>with the wrapper as the command parameter:</p>
  
  <pre><code>    &lt;Button VerticalAlignment="Center" Content="First" Padding="10,5,10,5" Margin="5" Command="{Binding FirstPageCommand}" CommandParameter="{Binding ElementName=viewer}"/&gt;
  </code></pre>