自动创建转换器或其他通用解决方案

时间:2015-02-25 09:31:51

标签: wpf xaml

当我需要在View中显示枚举时,我会为这种类型的枚举编写一个转换器

例如:

Public Enum ReportTypes
    Overview
    Crosstable
    AllCPDetails
End Enum

我的转换器是:

Public Class ReportTypeToStringConverter
    Implements IValueConverter

    Public Property Overview As String
    Public Property Crosstable As String
    Public Property AllCPDetails As String

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim enumValue As ReportTypes = DirectCast(value, ReportTypes)
        Select Case enumValue
            Case ReportTypes.Overview
                Return Overview
            Case ReportTypes.Crosstable
                Return Crosstable
            Case ReportTypes.AllCPDetails
                Return AllCPDetails
            Case Else
                Return DependencyProperty.UnsetValue
        End Select
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

然后,在我的XAML中,我可以写下这个:

<local:ReportTypeToStringConverter x:Key="ReportTypeToStringConverter"
                                   Overview="Overview"
                                   Crosstable="Cross Table"
                                   AllCPDetails="All CP Details"/>

在此示例中,存在“Enum&lt; - &gt; String”的关系,但我可能需要编写另一种关系:“Enum&lt; - &gt; Color”,“Enum&lt; - &gt; Visibility ” ......

代码非常干净,一切正常,但是当我有很多枚举或许多关系时...我如何自动创建转换器?片段? Visual Studio中的项模板?

另一种通用解决方案?

1 个答案:

答案 0 :(得分:1)

可能最通用的转换器可以使用ResourceDictionary从值ToString()结果映射到任意对象。源值甚至不需要是枚举。

public class GenericConverter : IValueConverter
{
    private readonly ResourceDictionary resources = new ResourceDictionary();

    public ResourceDictionary Resources
    {
        get { return resources; }
    }

    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        return resources[value.ToString()];
    }

    public object ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

你会...像这样用它来转换Visibility值:

<Window.Resources>
    <local:GenericConverter x:Key="VisibilityToSomeStringConverter">
        <local:GenericConverter.Resources>
            <sys:String x:Key="Visible">VISIBLE</sys:String>
            <sys:String x:Key="Hidden">HIDDEN</sys:String>
            <sys:String x:Key="Collapsed">COLLAPSED</sys:String>
        </local:GenericConverter.Resources>
    </local:GenericConverter>
    <local:GenericConverter x:Key="VisibilityToSomeIntConverter">
        <local:GenericConverter.Resources>
            <sys:Int32 x:Key="Visible">10</sys:Int32>
            <sys:Int32 x:Key="Hidden">20</sys:Int32>
            <sys:Int32 x:Key="Collapsed">30</sys:Int32>
        </local:GenericConverter.Resources>
    </local:GenericConverter>
</Window.Resources>