【问题标题】:Getting specific object out of public class in silverlight在silverlight中从公共类中获取特定对象
【发布时间】:2012-11-21 09:29:24
【问题描述】:

大家好,我有这个转换器类:

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

【问题讨论】:

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


    【解决方案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}}"
    

    【讨论】:

    • 所以我没有办法为多个转换器使用一个类?并感谢您的快速回复:)
    • 不,很遗憾不是因为界面只支持Convert和ConvertBack,你为什么要这么做?凉爽的 :-)。 msdn.microsoft.com/en-us/library/…
    • 大声笑,只是想在一节课上保持清洁,但我现在看到它是如何工作的,而我们讨论的是转换器?我不想在堆栈上问一个全新的问题,我怎样才能让收到的日期显示为 2012 年 11 月 23 日?它目前显示 2012/11/23,非常感谢您的帮助!现在支持答案:)
    • 不用担心,它是 DT.ToString("dd MMM yyyy");但实际上最好保持特定文化
    猜你喜欢
    • 1970-01-01
    • 2022-08-15
    • 2017-07-03
    • 1970-01-01
    • 2019-08-14
    • 2016-07-19
    • 2015-09-03
    • 2019-07-29
    • 2017-11-22
    相关资源
    最近更新 更多