共用方式為


Binding.Converter 屬性

定義

取得或設定系結引擎所呼叫的轉換子物件,以在來源與目標之間傳遞資料時修改資料,反之亦然。

public:
 property IValueConverter ^ Converter { IValueConverter ^ get(); void set(IValueConverter ^ value); };
IValueConverter Converter();

void Converter(IValueConverter value);
public IValueConverter Converter { get; set; }
var iValueConverter = binding.converter;
binding.converter = iValueConverter;
Public Property Converter As IValueConverter
<Binding Converter="converterReference"/>

屬性值

修改資料的 IValueConverter 物件。

範例

若要在系結中使用您的轉換器,請先建立轉換器類別的實例。 下列範例會將此範例顯示為 XAML 檔案中的資源。

<UserControl.Resources>
  <local:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>
<TextBlock Grid.Column="0" Margin="5,0"
  Text="{Binding Month, Converter={StaticResource Converter1}}"/>
// Custom class implements the IValueConverter interface.
public class DateToStringConverter : IValueConverter
{

    #region IValueConverter Members

    // Define the Convert method to change a DateTime object to 
    // a month string.
    public object Convert(object value, Type targetType, 
        object parameter, string language)
    {
        // The value parameter is the data from the source object.
        DateTime thisdate = (DateTime)value;
        int monthnum = thisdate.Month;
        string month;
        switch (monthnum)
        {
            case 1:
                month = "January";
                break;
            case 2:
                month = "February";
                break;
            default:
                month = "Month not found";
                break;
        }

        // Return the month value to pass to the target.
        return month;
    }

    // ConvertBack is not implemented for a OneWay binding.
    public object ConvertBack(object value, Type targetType, 
        object parameter, string language)
    {
        throw new NotImplementedException();
    }

    #endregion
}

備註

實作 IValueConverter 介面並實作 Convert 方法,以建立轉換器。 該方法應該傳回與系結目標相依性屬性相同的型別,或至少可以隱含強制或轉換成目標型別的型別。

適用於

另請參閱