在Silverlight中将特定对象从公共类中取出

时间:2012-11-21 09:29:24

标签: c# silverlight-5.0 winrt-xaml ivalueconverter

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

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到达那个对象?想要在同一个类

中转换另一个控件中的日期

new xaml:

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

提前感谢!

我正在使用visual studio 2012 / windows phone 8 / c#/ silverlight

1 个答案:

答案 0 :(得分: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}}"