IValueConverter 接口

定义

公开允许在数据通过绑定引擎时对其进行修改的方法。

public interface class IValueConverter
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(3874684656, 1810, 18559, 179, 19, 243, 0, 184, 215, 154, 161)]
struct IValueConverter
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(3874684656, 1810, 18559, 179, 19, 243, 0, 184, 215, 154, 161)]
public interface IValueConverter
Public Interface IValueConverter
派生
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

以下示例演示如何实现 IValueConverter 接口,并在数据绑定到对象集合时使用转换器。

注意

如果使用 C++/WinRT (或 C++/CX) ,请参阅 格式化或转换数据值以供显示 ,以获取创作自己的值转换器的更多代码示例。 本主题还讨论了如何将 ConverterParameter 属性与 C++ 字符串格式设置函数配合使用。

<UserControl x:Class="ConverterParameterEx.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:ConverterParameterEx" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" >
        <Grid.Resources>
           <local:DateFormatter x:Key="FormatConverter" />
        </Grid.Resources>
        
        <ComboBox Height="60" Width="250" x:Name="MusicCombo" 
            ItemsSource="{Binding}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name, Mode=OneWay}" />
                        <TextBlock Text="{Binding Path=Artist, Mode=OneWay}" />
                        <TextBlock Text="{Binding Path=ReleaseDate, Mode=OneWay,
                            Converter={StaticResource FormatConverter}, 
                            ConverterParameter=\{0:d\}}" />
                   </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</UserControl>
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace ConverterParameterEx
{
    public partial class Page : UserControl
    {

        public ObservableCollection<Recording> MyMusic =
            new ObservableCollection<Recording>();
        public Page()
        {
            InitializeComponent();

            // Add items to the collection.
            MyMusic.Add(new Recording("Chris Sells", "Chris Sells Live",
                new DateTime(2008, 2, 5)));
            MyMusic.Add(new Recording("Luka Abrus",
                "The Road to Redmond", new DateTime(2007, 4, 3)));
            MyMusic.Add(new Recording("Jim Hance",
                "The Best of Jim Hance", new DateTime(2007, 2, 6)));

            // Set the data context for the combo box.
            MusicCombo.DataContext = MyMusic;
        }
    }

    // Simple business object.
    public class Recording
    {
        public Recording() { }
        public Recording(string artistName, string cdName, DateTime release)
        {
            Artist = artistName;
            Name = cdName;
            ReleaseDate = release;
        }
        public string Artist { get; set; }
        public string Name { get; set; }
        public DateTime ReleaseDate { get; set; }
    }

    public class DateFormatter : IValueConverter
    {
        // This converts the DateTime object to the string to display.
        public object Convert(object value, Type targetType, 
            object parameter, string language)
        {
            // Retrieve the format string and use it to format the value.
            string formatString = parameter as string;
            if (!string.IsNullOrEmpty(formatString))
            {
                return string.Format(
                    new CultureInfo(language), formatString, value);
            }
            // If the format string is null or empty, simply call ToString()
            // on the value.
            return value.ToString();
        }

        // No need to implement converting back on a one-way binding 
        public object ConvertBack(object value, Type targetType, 
            object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
// pch.h
#include <winrt/Windows.Globalization.h>

// MainPage.idl
namespace ConverterParameterEx
{
    [default_interface]
    runtimeclass MainPage : Windows.UI.Xaml.Controls.Page
    {
        MainPage();
    }
}

<!-- MainPage.xaml -->
<Page
    x:Class="ConverterParameterEx.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ConverterParameterEx"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <local:Page/>
</Page>

// MainPage.h
#pragma once

#include "MainPage.g.h"

namespace winrt::ConverterParameterEx::implementation
{
    struct MainPage : MainPageT<MainPage>
    {
        MainPage();
    };
}

namespace winrt::ConverterParameterEx::factory_implementation
{
    struct MainPage : MainPageT<MainPage, implementation::MainPage>
    {
    };
}

// MainPage.cpp
#include "pch.h"
#include "MainPage.h"
#include "MainPage.g.cpp"

using namespace winrt;
using namespace Windows::UI::Xaml;

namespace winrt::ConverterParameterEx::implementation
{
    MainPage::MainPage()
    {
        InitializeComponent();
    }
}

// Page.idl
namespace ConverterParameterEx
{
    [default_interface]
    runtimeclass Page : Windows.UI.Xaml.Controls.UserControl
    {
        Page();
        Windows.Foundation.Collections.IVector<IInspectable> MyMusic{ get; };
    }
}

<!-- Page.xaml -->
<!-- see listing above -->

// Page.h
#pragma once

#include "winrt/Windows.UI.Xaml.h"
#include "winrt/Windows.UI.Xaml.Markup.h"
#include "winrt/Windows.UI.Xaml.Interop.h"
#include "winrt/Windows.UI.Xaml.Controls.Primitives.h"
#include "Page.g.h"
#include "DateFormatter.h"

namespace winrt::ConverterParameterEx::implementation
{
    struct Page : PageT<Page>
    {
        Page();
        Windows::Foundation::Collections::IVector<Windows::Foundation::IInspectable> MyMusic();

    private:
        Windows::Foundation::Collections::IVector<Windows::Foundation::IInspectable> m_myMusic;
    };
}

namespace winrt::ConverterParameterEx::factory_implementation
{
    struct Page : PageT<Page, implementation::Page>
    {
    };
}

// Page.cpp
#include "pch.h"
#include "Page.h"
#if __has_include("Page.g.cpp")
#include "Page.g.cpp"
#endif
#include "Recording.h"

using namespace winrt;
using namespace Windows::UI::Xaml;

namespace winrt::ConverterParameterEx::implementation
{
    Page::Page()
    {
        InitializeComponent();

        std::vector<Windows::Foundation::IInspectable> recordings;

        Windows::Globalization::Calendar releaseDateTime;
        releaseDateTime.Month(7); releaseDateTime.Day(8); releaseDateTime.Year(1748);
        recordings.push_back(winrt::make<Recording>(L"Johann Sebastian Bach", L"Mass in B minor", releaseDateTime));

        releaseDateTime = Windows::Globalization::Calendar{};
        releaseDateTime.Month(11); releaseDateTime.Day(2); releaseDateTime.Year(1805);
        recordings.push_back(winrt::make<Recording>(L"Ludwig van Beethoven", L"Third Symphony", releaseDateTime));

        releaseDateTime = Windows::Globalization::Calendar{};
        releaseDateTime.Month(3); releaseDateTime.Day(12); releaseDateTime.Year(1737);
        recordings.push_back(winrt::make<Recording>(L"George Frideric Handel", L"Serse", releaseDateTime));

        m_myMusic = winrt::single_threaded_observable_vector<Windows::Foundation::IInspectable>(std::move(recordings));

        MusicCombo().DataContext(m_myMusic);
    }

    Windows::Foundation::Collections::IVector<Windows::Foundation::IInspectable> Page::MyMusic() { return m_myMusic; }
}

// Recording.idl
namespace ConverterParameterEx
{
    [bindable]
    runtimeclass Recording
    {
        Recording(String artist, String name, Windows.Globalization.Calendar releaseDate);
        String Artist{ get; };
        String Name{ get; };
        Windows.Globalization.Calendar ReleaseDate{ get; };
    }
}

// Recording.h
#pragma once
#include "Recording.g.h"

namespace winrt::ConverterParameterEx::implementation
{
    struct Recording : RecordingT<Recording>
    {
        Recording() = default;

        Recording(hstring const& artist, hstring const& name, winrt::Windows::Globalization::Calendar const& releaseDate);
        hstring Artist();
        hstring Name();
        winrt::Windows::Globalization::Calendar ReleaseDate();

    private:
        std::wstring m_artist;
        std::wstring m_name;
        Windows::Globalization::Calendar m_releaseDate;
    };
}
namespace winrt::ConverterParameterEx::factory_implementation
{
    struct Recording : RecordingT<Recording, implementation::Recording>
    {
    };
}

// Recording.cpp
#include "pch.h"
#include "Recording.h"
#include "Recording.g.cpp"

namespace winrt::ConverterParameterEx::implementation
{
    Recording::Recording(hstring const& artist, hstring const& name, Windows::Globalization::Calendar const& releaseDate) :
        m_artist{ artist.c_str() },
        m_name{ name.c_str() },
        m_releaseDate{ releaseDate } {}

    hstring Recording::Artist() { return hstring{ m_artist }; }
    hstring Recording::Name() { return hstring{ m_name }; }
    Windows::Globalization::Calendar Recording::ReleaseDate() { return m_releaseDate; }
}

// DateFormatter.idl
namespace ConverterParameterEx
{
    runtimeclass DateFormatter : [default] Windows.UI.Xaml.Data.IValueConverter
    {
        DateFormatter();
    }
}

// DateFormatter.h
#pragma once
#include "DateFormatter.g.h"

namespace winrt::ConverterParameterEx::implementation
{
    struct DateFormatter : DateFormatterT<DateFormatter>
    {
        DateFormatter() = default;

        winrt::Windows::Foundation::IInspectable Convert(winrt::Windows::Foundation::IInspectable const& value, winrt::Windows::UI::Xaml::Interop::TypeName const& targetType, winrt::Windows::Foundation::IInspectable const& parameter, hstring const& language);
        winrt::Windows::Foundation::IInspectable ConvertBack(winrt::Windows::Foundation::IInspectable const& value, winrt::Windows::UI::Xaml::Interop::TypeName const& targetType, winrt::Windows::Foundation::IInspectable const& parameter, hstring const& language);
    };
}
namespace winrt::ConverterParameterEx::factory_implementation
{
    struct DateFormatter : DateFormatterT<DateFormatter, implementation::DateFormatter>
    {
    };
}

// DateFormatter.cpp
#include "pch.h"
#include "DateFormatter.h"
#include "DateFormatter.g.cpp"

namespace winrt::ConverterParameterEx::implementation
{
    winrt::Windows::Foundation::IInspectable DateFormatter::Convert(winrt::Windows::Foundation::IInspectable const& value, winrt::Windows::UI::Xaml::Interop::TypeName const& targetType, winrt::Windows::Foundation::IInspectable const& parameter, hstring const& language)
    {
        // Retrieve the value as a Calendar.
        Windows::Globalization::Calendar valueAsCalendar{ value.as<Windows::Globalization::Calendar>() };

        std::wstringstream wstringstream;
        wstringstream << valueAsCalendar.MonthAsNumericString().c_str();
        wstringstream << L"/" << valueAsCalendar.DayAsString().c_str();
        wstringstream << L"/" << valueAsCalendar.YearAsString().c_str();
        return winrt::box_value(hstring{ wstringstream.str().c_str() });
    }
    winrt::Windows::Foundation::IInspectable DateFormatter::ConvertBack(winrt::Windows::Foundation::IInspectable const& value, winrt::Windows::UI::Xaml::Interop::TypeName const& targetType, winrt::Windows::Foundation::IInspectable const& parameter, hstring const& language)
    {
        throw hresult_not_implemented();
    }
}
//
// MainPage.xaml.h
// Declaration of the MainPage class.
// 

#pragma once

#include "MainPage.g.h"

namespace IValueConverterExample
{

    // Simple business object.
    [Windows::UI::Xaml::Data::Bindable]
    public ref class Recording sealed 
    {
    public: 
        Recording (Platform::String^ artistName, Platform::String^ cdName, Windows::Foundation::DateTime release)
        {
            Artist = artistName;
            Name = cdName;
            ReleaseDate = release;
        }
        property Platform::String^ Artist;
        property Platform::String^ Name;
        property Windows::Foundation::DateTime ReleaseDate;
    };

    public ref class DateFormatter  sealed : Windows::UI::Xaml::Data::IValueConverter 
    {
        // This converts the DateTime object to the Platform::String^ to display.
    public:
        virtual Platform::Object^ Convert(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType, 
            Platform::Object^ parameter, Platform::String^ language)
        {
            Windows::Foundation::DateTime dt = safe_cast<Windows::Foundation::DateTime>(value); 
            Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ dtf =
                Windows::Globalization::DateTimeFormatting::DateTimeFormatter::ShortDate;
            return dtf->Format(dt); 
        }

        // No need to implement converting back on a one-way binding 
        virtual Platform::Object^ ConvertBack(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType, 
            Platform::Object^ parameter, Platform::String^ language)
        {
            throw ref new Platform::NotImplementedException();
        }
    };

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public ref class MainPage sealed
    {
    public:
        MainPage()
        {	
            m_myMusic = ref new Platform::Collections::Vector<Recording^>();

            // Add items to the collection.

            // You can use a Calendar object to create a Windows::Foundation::DateTime
            auto c = ref new Windows::Globalization::Calendar();
            c->Year = 2008;
            c->Month = 2;
            c->Day = 5;
            m_myMusic->Append(ref new Recording("Chris Sells", "Chris Sells Live",
                c->GetDateTime()));

            c->Year = 2007;
            c->Month = 4;
            c->Day = 3;
            m_myMusic->Append(ref new Recording("Luka Abrus",
                "The Road to Redmond", c->GetDateTime()));
            
            c->Year = 2007;
            c->Month = 2;
            c->Day = 3;
            m_myMusic->Append(ref new Recording("Jim Hance",
                "The Best of Jim Hance", dt));
            InitializeComponent();

            // Set the data context for the combo box.
            MusicCombo->DataContext = m_myMusic;	
        }


    protected:
        virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;

    private:
        Windows::Foundation::Collections::IVector<Recording^>^ m_myMusic;
    };
}
Imports System.Collections.ObjectModel
Imports System.Windows.Data
Imports System.Globalization

Partial Public Class Page
    Inherits UserControl

    Public MyMusic As New ObservableCollection(Of Recording)()
    Public Sub New()
        InitializeComponent()

        ' Add items to the collection.
        MyMusic.Add(New Recording("Sheryl Crow", "Detours", New DateTime(2008, 2, 5)))
        MyMusic.Add(New Recording("Brandi Carlisle", "The Story", New DateTime(2007, 4, 3)))
        MyMusic.Add(New Recording("Patty Griffin", "Children Running Through", New DateTime(2007, 2, 6)))

        ' Set the data context for the combo box.
        MusicCombo.DataContext = MyMusic
    End Sub
End Class

' Simple business object. 
Public Class Recording
    Public Sub New()
    End Sub
    Public Sub New(ByVal artistName As String, ByVal cdName As String, _
       ByVal release As DateTime)
        Artist = artistName
        Name = cdName
        ReleaseDate = release
    End Sub
    Private artistValue As String
    Private nameValue As String
    Private releaseDateValue As DateTime
    Public Property Artist() As String
        Get
            Return artistValue
        End Get
        Set(ByVal value As String)
            artistValue = value
        End Set
    End Property
    Public Property Name() As String
        Get
            Return nameValue
        End Get
        Set(ByVal value As String)
            nameValue = value
        End Set
    End Property
    Public Property ReleaseDate() As DateTime
        Get
            Return releaseDateValue
        End Get
        Set(ByVal value As DateTime)
            releaseDateValue = value
        End Set
    End Property
End Class

Public Class DateFormatter
    Implements IValueConverter

    ' This converts the DateTime object to the string to display. 
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, _
        ByVal parameter As Object, ByVal language As System.String) As Object _
        Implements IValueConverter.Convert

        ' Retrieve the format string and use it to format the value. 
        Dim formatString As String = TryCast(parameter, String)
        If Not String.IsNullOrEmpty(formatString) Then

            Return String.Format(New CultureInfo(language), formatString, value)
        End If

        ' If the format string is null or empty, simply call ToString() 
        ' on the value. 
        Return value.ToString()
    End Function

    ' No need to implement converting back on a one-way binding.
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, _
        ByVal parameter As Object, _
        ByVal language As System.String) As Object _
        Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

注解

可以创建一个类,用于通过从 IValueConverter 继承来转换源和目标之间的数据格式。 例如,你可能希望有一个颜色列表,这些颜色存储为 RGBA 值,但在 UI 中显示颜色名称。 通过实现 ConvertConvertBack,可以更改绑定引擎在目标与源之间传递的数据值的格式。 应始终使用功能实现实现 Convert ,但实现 ConvertBack 以便报告未实现的异常是相当常见的。 仅当将转换器用于双向绑定或使用 XAML 进行序列化时,才需要在转换器中使用 ConvertBack 方法。

在任何转换器无法转换源值的情况下,都应从 IValueConverter 实现中返回 UnsetValue,该实现在数据绑定中提供对依赖属性的转换。 转换器不应在 Convert 中针对这种情况引发异常;这些异常将作为运行时异常显示,你需要在 UnhandledException 或更糟地向用户显示为实际的运行时异常。 转换器实现应遵循常规绑定模式,即任何失败的绑定不执行任何操作且不提供值, UnsetValue 而不是 null 是绑定引擎所理解的这种情况的 sentinel 值。 有关详细信息,请参阅深入了解数据绑定

注意

若要将数据绑定到用 Visual C++ 组件扩展 (C++/CX) 编写的自定义值转换器,必须在其中一个代码隐藏文件中直接或间接包含定义 IValueConverter 实现类的头文件。 有关详细信息,请参阅 使用 C++ 创建第一个

提示

UWP 应用的一些默认项目模板包括帮助程序类 BooleanToVisibilityConverter。 此类是一个 IValueConverter 实现,用于处理常见的自定义控件方案,在该方案中,使用控件逻辑类中的布尔值在 XAML 控件模板中设置 Visibility 值。

迁移说明

在Windows 运行时,IValueConverter 方法的语言参数使用字符串,而不是使用 CultureInfo 对象,就像在接口的 Windows Presentation Foundation (WPF) 和 Microsoft Silverlight 定义中那样。

方法

Convert(Object, TypeName, Object, String)

在将源数据传递到目标以在 UI 中显示之前,对其进行修改。

ConvertBack(Object, TypeName, Object, String)

在将目标数据传递给源对象之前,对其进行修改。 此方法仅在 TwoWay 绑定中调用。

适用于

另请参阅