Bewerken

Delen via


How to: Bind to a Method

The following example shows how to bind to a method using ObjectDataProvider.

Example

In this example, TemperatureScale is a class that has a method ConvertTemp, which takes two parameters (one of double and one of the enum type TempType) and converts the given value from one temperature scale to another. In the following example, an ObjectDataProvider is used to instantiate the TemperatureScale object. The ConvertTemp method is called with two specified parameters.

<Window.Resources>
  <ObjectDataProvider ObjectType="{x:Type local:TemperatureScale}"
                      MethodName="ConvertTemp" x:Key="convertTemp">
    <ObjectDataProvider.MethodParameters>
      <system:Double>0</system:Double>
      <local:TempType>Celsius</local:TempType>
    </ObjectDataProvider.MethodParameters>
  </ObjectDataProvider>

  <local:DoubleToString x:Key="doubleToString" />

</Window.Resources>

Now that the method is available as a resource, you can bind to its results. In the following example, the Text property of the TextBox and the SelectedValue of the ComboBox are bound to the two parameters of the method. This allows users to specify the temperature to convert and the temperature scale to convert from. Note that BindsDirectlyToSource is set to true because we are binding to the MethodParameters property of the ObjectDataProvider instance and not properties of the object wrapped by the ObjectDataProvider (the TemperatureScale object).

The Content of the last Label updates when the user modifies the content of the TextBox or the selection of the ComboBox.

<Label Grid.Row="1" HorizontalAlignment="Right">Enter the degree to convert:</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="tb">
  <TextBox.Text>
    <Binding Source="{StaticResource convertTemp}" Path="MethodParameters[0]"
             BindsDirectlyToSource="true" UpdateSourceTrigger="PropertyChanged"
             Converter="{StaticResource doubleToString}">
      <Binding.ValidationRules>
        <local:InvalidCharacterRule/>
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>
<ComboBox Grid.Row="1" Grid.Column="2" 
  SelectedValue="{Binding Source={StaticResource convertTemp},
  Path=MethodParameters[1], BindsDirectlyToSource=true}">
  <local:TempType>Celsius</local:TempType>
  <local:TempType>Fahrenheit</local:TempType>
</ComboBox>
<Label Grid.Row="2" HorizontalAlignment="Right">Result:</Label>
<Label Content="{Binding Source={StaticResource convertTemp}}"
    Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"/>

The converter DoubleToString takes a double and turns it into a string in the Convert direction (from the binding source to binding target, which is the Text property) and converts a string to a double in the ConvertBack direction.

The InvalidCharacterRule is a ValidationRule that checks for invalid characters. The default error template, which is a red border around the TextBox, appears to notify users when the input value is not a double value.

See also