Share via


How to: Data-Bind DocumentViewer's Zoom Property Using ZoomPercentageConverter

This Extensible Application Markup Language (XAML) example shows how to bind the data of the DocumentViewer.Zoom property to a text control using the ZoomPercentageConverter type-converter.

Example

<Window x:Class="SDKSample.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>
    <Grid.Resources>
      <ZoomPercentageConverter x:Key="MyConverterKey" />
    </Grid.Resources>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <DocumentViewer Name="dvZoomSource" Grid.Row="0" />
    <TextBox Grid.Row="1">
      <TextBox.Text>
        <Binding
            ElementName="dvZoomSource" 
            Path="Zoom" 
            Converter="{StaticResource MyConverterKey}" 
            Mode="OneWay" />
      </TextBox.Text>
    </TextBox>
  </Grid>
</Window>

Task Remarks

  • In this example, any changes to the value of Zoom are immediately reflected in the data-bound TextBox. The ZoomPercentageConverter type-converter is automatically called whenever an update propagates through the data binding. This particular type-converter translates the double value of Zoom into a string value appended with a percent sign (%).

  • This example uses a property element syntax binding declaration to bind the value of Zoom to the Text property of a TextBox.

  • The ElementName clause in the binding declaration refers to the Name of the source element, which in this case is "dvZoomSource".

  • The Path clause in the binding declaration names the source property, which in this case is Zoom.

  • The Converter clause specifies a type-converter to use for this data binding. In this case, clause refers to a locally-defined static resource mapping by matching the resource key (x:Key) to the value referenced in the Converter clause. In the example above, the resource key is "MyConverterKey". The key itself is an arbitrary string value that must be unique within the current scope. Locally-defined resources must by referenced using the StaticResource syntax show in the example above.

  • The Mode clause in the binding declaration specifies that this is a one-way data binding; updates to the source value of Zoom are reflected in the target TextBox, but changes to the contents of the TextBox do not change the value of Zoom.

See Also

Reference

IValueConverter

Concepts

Data Binding Overview
Binding Declarations Overview