共用方式為


Binding.Converter Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the converter object that is called by the binding engine to modify the data as it is passed between the source and target, or vice versa.

Namespace:  System.Windows.Data
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Converter As IValueConverter
public IValueConverter Converter { get; set; }
<Binding Converter="converterReference"/>

XAML Values

  • converterReference
    A reference to an existing object that implements IValueConverter and functions as a converter. Typically the object is created in a ResourceDictionary and given a key, then referenced by using the StaticResource markup extension. For instance: <Binding Converter="{StaticResource myConverter}" .../>

Property Value

Type: System.Windows.Data.IValueConverter
The IValueConverter object that modifies the data.

Exceptions

Exception Condition
InvalidOperationException

The Binding has already been attached to a target element, and cannot be modified.

Remarks

Create a converter by implementing the IValueConverter interface and implementing the Convert and ConvertBack methods.

XAML Usage Notes

For XAML usage of converters on bindings, see Binding Markup Extension. The use of property element syntax to create an object element that fills a Binding.Converter property element is possible, but uncommon. More common is to create an object element in resources and then reference it by key, using attribute syntax.

Examples

To use your converter in a binding, first create an instance of your converter class. The following example shows this as a resource in a XAML file.

<UserControl.Resources>
  <local:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>

Then set the Converter property for the binding to that instance.

<TextBlock Grid.Column="0" 
 
  Text="{Binding Month, Converter={StaticResource Converter1}}"/>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.