0

嘿伙计们,我有这个转换器类:

public class InboxItemValueConverters : IValueConverter 
{
    public object Convert(object value, System.Type targetType,
                            object parameter, CultureInfo culture)
    {
        int urgency = (int)value;
        Brush brush = new SolidColorBrush();

        if (urgency == 0)
        {
            brush = new SolidColorBrush(Colors.Green);            }
        else if (urgency == 1)
        {
            brush = new SolidColorBrush(Colors.Yellow);
        }
        else if (urgency == 2)
        {
            brush = new SolidColorBrush(Colors.Red);
        }


        return brush;
    }

    public object ConvertBack(object value,  System.Type targetType,
                              object parameter,  CultureInfo culture)
    {
        return null;
    }



    public object ConvDateToShort(object value, System.Type targetType,
                            object parameter, CultureInfo culture)
    {
        DateTime DT = (DateTime)value;
        return DT.ToShortDateString();
    }


    public object Convdateback(object value, System.Type targetType,
                  object parameter, CultureInfo culture)
    {
        return null;
    }

}

这就是我第一次引用和使用它的方式:

<src:InboxItemValueConverters x:Key="converttocolor" />

 <Canvas Background="{Binding Urgency, Converter={StaticResource converttocolor}}"

没有在课堂上,你们可以看到我那里有一个日期转换器?我将如何通过 xaml 获取该对象?想在另一个控件中转换日期,来自同一类

新的xml:

Text="{Binding DocDate , Converter={StaticResource converttocolor}}"

提前致谢!

我正在使用 Visual Studio 2012/Windows Phone 8/C#/silverlight

4

1 回答 1

1

您必须将日期转换器从 colorconverter 类移到它自己的类中

public class DateValueConverter : IValueConverter 
{
    public object Convert(object value, System.Type targetType,
                            object parameter, CultureInfo culture)
    {
        DateTime DT = (DateTime)value;
        return DT.ToShortDateString();
    }
    public object ConvertBack(object value, System.Type targetType,
                  object parameter, CultureInfo culture)
    {
        return null;
    }
}

然后像你做颜色转换器一样在顶部声明它,然后将转换器更改为指向日期转换器的键

//This needs to be declared below the colorconverter resource
<src:DateValueConverter  x:Key="dateConverter" />

Text="{Binding DocDate , Converter={StaticResource dateConverter}}"
于 2012-11-21T09:35:53.547 回答