Exportar (0) Imprimir
Expandir todo

IMultiValueConverter.ConvertBack (Método)

Converts a binding target value to the source binding values.

Espacio de nombres: System.Windows.Data
Ensamblado: PresentationFramework (en presentationframework.dll)

Object[] ConvertBack (
	Object value,
	Type[] targetTypes,
	Object parameter,
	CultureInfo culture
)
Object[] ConvertBack (
	Object value, 
	Type[] targetTypes, 
	Object parameter, 
	CultureInfo culture
)
function ConvertBack (
	value : Object, 
	targetTypes : Type[], 
	parameter : Object, 
	culture : CultureInfo
) : Object[]
No se pueden utilizar métodos en XAML.

Parámetros

value

The value that the binding target produces.

targetTypes

The array of types to convert to. The array length indicates the number and types of values that are suggested for the method to return.

parameter

The converter parameter to use.

culture

The culture to use in the converter.

Valor devuelto

An array of values that have been converted from the target value back to the source values.

If there are more return values than there are bindings in the MultiBinding, the excess return values are ignored. If there are more source bindings than there are return values, the remaining source bindings do not have any values set on them. Types of return values are not verified against targetTypes; the values are set on the source bindings directly.

Return DoNothing at position i to indicate that no value is to be set on the source binding at index i.

Return DependencyProperty.UnsetValue at position i to indicate that the converter is unable to provide a value for the source binding at index i, and that no value is to be set on it.

Return referencia null (Nothing en Visual Basic) to indicate that the converter cannot perform the conversion or that it does not support conversion in this direction.

The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the ConvertBack method or any uncaught exceptions that are thrown by methods that the ConvertBack method calls are treated as run-time errors. Handle anticipated problems by returning referencia null (Nothing en Visual Basic).

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 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0

Adiciones de comunidad

AGREGAR
Mostrar:
© 2014 Microsoft