Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 3.08 KB

how-to-bind-to-a-method.md

File metadata and controls

34 lines (23 loc) · 3.08 KB
title description ms.date helpviewer_keywords ms.assetid
How to: Bind to a Method
Follow this example to find out how to bind to the method of an object in the Windows Presentation Foundation (WPF).
03/30/2017
data binding [WPF], binding to methods using ObjectDataProvider
binding [WPF], to methods
methods [WPF], binding to
5f55e71e-2182-42a0-88d1-700cc1427a7a

How to: Bind to a Method

The following example shows how to bind to a method using xref:System.Windows.Data.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 xref:System.Windows.Data.ObjectDataProvider is used to instantiate the TemperatureScale object. The ConvertTemp method is called with two specified parameters.

[!code-xamlBindToMethod#WindowResources]

Now that the method is available as a resource, you can bind to its results. In the following example, the xref:System.Windows.Controls.TextBox.Text%2A property of the xref:System.Windows.Controls.TextBox and the xref:System.Windows.Controls.Primitives.Selector.SelectedValue%2A of the xref:System.Windows.Controls.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 xref:System.Windows.Data.Binding.BindsDirectlyToSource%2A is set to true because we are binding to the xref:System.Windows.Data.ObjectDataProvider.MethodParameters%2A property of the xref:System.Windows.Data.ObjectDataProvider instance and not properties of the object wrapped by the xref:System.Windows.Data.ObjectDataProvider (the TemperatureScale object).

The xref:System.Windows.Controls.ContentControl.Content%2A of the last xref:System.Windows.Controls.Label updates when the user modifies the content of the xref:System.Windows.Controls.TextBox or the selection of the xref:System.Windows.Controls.ComboBox.

[!code-xamlBindToMethod#UI]

The converter DoubleToString takes a double and turns it into a string in the xref:System.Windows.Data.IValueConverter.Convert%2A direction (from the binding source to binding target, which is the xref:System.Windows.Controls.TextBox.Text%2A property) and converts a string to a double in the xref:System.Windows.Data.IValueConverter.ConvertBack%2A direction.

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

See also