(0) exportieren Drucken
Alle erweitern

IMultiValueConverter.Convert-Methode

Converts source values to a value for the binding target. The data binding engine calls this method when it propagates the values from source bindings to the binding target.

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

Object Convert (
	Object[] values,
	Type targetType,
	Object parameter,
	CultureInfo culture
)
Object Convert (
	Object[] values, 
	Type targetType, 
	Object parameter, 
	CultureInfo culture
)
function Convert (
	values : Object[], 
	targetType : Type, 
	parameter : Object, 
	culture : CultureInfo
) : Object
Sie können diese Methoden nicht in XAML verwenden.

Parameter

values

The array of values that the source bindings in the MultiBinding produces. The value UnsetValue indicates that the source binding has no value to provide for conversion.

targetType

The type of the binding target property.

parameter

The converter parameter to use.

culture

The culture to use in the converter.

Rückgabewert

A converted value. If the method returns NULL-Verweis (Nothing in Visual Basic), the valid NULL-Verweis (Nothing in Visual Basic) value is used. A return value of DependencyProperty.UnsetValue indicates that the converter did not produce a value, and that the binding will use the FallbackValue if it is available, or else will use the default value. A return value of Binding.DoNothing indicates that the binding does not transfer the value or use the FallbackValue or the default value.

The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the Convert method, or any uncaught exceptions that are thrown by methods that the Convert method calls, are treated as run-time errors. Handle anticipated problems by returning DependencyProperty.UnsetValue.

MultiBinding allows you to bind a binding target property to a list of source properties and then apply logic to produce a value with the given inputs. This example demonstrates how to use MultiBinding.

In the following example, NameListData refers to a collection of PersonName objects, which are objects that contain two properties, firstName and lastName. The following example produces a TextBlock that shows the first and last names of a person with the last name first.

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:SDKSample"
  x:Class="SDKSample.Window1"
  Width="400"
  Height="280"
  Title="MultiBinding Sample">
	
  <Window.Resources>
    <c:NameList x:Key="NameListData"/>
    <c:NameConverter x:Key="myNameConverter"/>
...

  </Window.Resources>
...

    <TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
      <TextBlock.Text>
        <MultiBinding Converter="{StaticResource myNameConverter}"
                      ConverterParameter="FormatLastFirst">
          <Binding Path="FirstName"/>
          <Binding Path="LastName"/>
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>
...

</Window>

To understand how the last-name-first format is produced, let's take a look at the implementation of the NameConverter:

public class NameConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string name;

        switch ((string)parameter)
        {
            case "FormatLastFirst":
                name = values[1] + ", " + values[0];
                break;
            case "FormatNormal":
            default:
                name = values[0] + " " + values[1];
                break;
        }

        return name;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        string[] splitValues = ((string)value).Split(' ');
        return splitValues;
    }
}

NameConverter implements the IMultiValueConverter interface. NameConverter takes the values from the individual bindings and stores them in the values object array. The order in which the Binding elements appear under the MultiBinding element is the order in which those values are stored in the array. The value of the ConverterParameter attribute is referenced by the parameter argument of the Converter method, which performs a switch on the parameter to determine how to format the name.

For the complete sample, see Implementing Parameterized MultiBinding Sample.

For another example of an IMultiValueConverter implementation, see Data Binding Demo.

Microsoft .NET Framework 3.0 wird unter Windows Vista, Microsoft Windows XP SP2 und Windows Server 2003 SP1 unterstützt.

.NET Framework

Unterstützt in: 3.0
Anzeigen:
© 2014 Microsoft