0

Hey guys i have this converter class :

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;
    }

}

and this is how i referenced it and used it the first time :

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

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

no in the class,as you guys can see i have a date converter in there? how would i go around getting to that object through the xaml? want to convert date in another control, from same class

new xaml :

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

thanks in advance!

i am using visual studio 2012/windows phone 8/c#/silverlight

jv42
  • 8,521
  • 5
  • 40
  • 64
Arrie
  • 1,327
  • 5
  • 18
  • 39

1 Answers1

1

You have to move your date converter out of the colorconverter class into it's own class

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;
    }
}

Then declare it at the top like you did your color converter and then change the converter to point to the date converter's key

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

Text="{Binding DocDate , Converter={StaticResource dateConverter}}"
TBohnen.jnr
  • 5,117
  • 1
  • 19
  • 26
  • so there is no way for me to use one class for many converters? and thanks for quick reply :) – Arrie Nov 21 '12 at 09:40
  • Nope, unfortunately not because the interface only supports Convert And ConvertBack, why would you want to do that? Cool :-). http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx – TBohnen.jnr Nov 21 '12 at 09:53
  • lol, just thought going to keep it clean having it in one class, but i see now how it really works, while we on the subject of converters? i dont want to ask a whole new question on stack, how can i get the date recieved to show like 23 Nov 2012? it currently shows 2012/11/23,your help here would be GREATLY appreciated! upvoting answer now :) – Arrie Nov 21 '12 at 11:47
  • 1
    NO worries, it's DT.ToString("dd MMM yyyy"); but it's actually better to keep it culture specific – TBohnen.jnr Nov 21 '12 at 12:55