2012-01-14 20 views
3

我有2类,日期时间(框架类)和FriendlyDateTime(somethign实现我通知属性更改。是有一组接口可以实现,以避免在WPF中的转换器结合

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using ToolSuite.Contract.BaseClasses; 

namespace foo.WizardElements 
{ 
    public class FriendlyDateTime : NotifyPropertyChangedBase 
    { 
     public FriendlyDateTime() 
     { 

     } 
     public FriendlyDateTime(DateTime? value) 
     { 
      Date = value; 
     } 

     public DateTime? Date 
     { 
      get 
      { 
       if (base._values.ContainsKey("Date")) 
        return Get<DateTime>("Date"); 
       else 
        return null; 
      } 

      set 
      { 
       if (value == null) 
       { 
        if (base._values.ContainsKey("Date")) 
         base._values.Remove("Date"); 
       } 
       else 
        Set<DateTime>("Date", value.Value); 

       base.NotifyPropertyChanged("Date"); 
       base.NotifyPropertyChanged("Hour"); 
       base.NotifyPropertyChanged("Minute"); 
      } 
     } 
     public int Hour 
     { 
      get { return Date.HasValue ? Date.Value.Hour : 0; } 
      set 
      { 
       if (Hour > 23) 
        Hour = 23; 
       var d = Date.HasValue ? Date.Value : DateTime.Now; 
       Date = new DateTime(d.Year, d.Month, d.Day, value, Minute, 0); 
      } 
     } 
     public int Minute 
     { 
      get { return Date.HasValue ? Date.Value.Minute : 0; } 
      set 
      { 
       if (Minute > 59) 
        Minute = 59; 
       var d = Date.HasValue ? Date.Value : DateTime.Now; 
       Date = new DateTime(d.Year, d.Month, d.Day, Hour, value, 0); 
      } 
     } 

     static public implicit operator DateTime?(FriendlyDateTime value) 
     { 
      return value.Date; 
     } 

     static public implicit operator FriendlyDateTime(DateTime? value) 
     { 
      // Note that because RomanNumeral is declared as a struct, 
      // calling new on the struct merely calls the constructor 
      // rather than allocating an object on the heap: 
      return new FriendlyDateTime(value); 
     } 
    } 
} 

转换器

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Data; 
using System.Globalization; 
using ToolSuite.Contract.BaseClasses; 

namespace foo.WizardElements 
{ 
    [ValueConversion(typeof(FriendlyDateTime), typeof(DateTime?))] 
    public class FriendlyDateTimeValueConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (targetType == typeof(DateTime?)) 
      { 
       return ((FriendlyDateTime)value).Date; 
      } 
      else if (targetType == typeof(FriendlyDateTime)) 
      { 
       return new FriendlyDateTime(value as DateTime?); 
      } 
      return null; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (targetType == typeof(DateTime?)) 
      { 
       return ((FriendlyDateTime)value).Date; 
      } 
      else if (targetType == typeof(FriendlyDateTime)) 
      { 
       return new FriendlyDateTime(value as DateTime?); 
      } 
      return null; 
     } 

    } 
} 

是否有一组接口,我可以实现,将amke WPF自动使用我的转换器W/O制造C让班级意识到。

从这个

  <we:DateTimeRangeElement Date="{Binding Path=Filter.EndTime, Mode=TwoWay, Converter={StaticResource DateTimeConverter}}" /> 

所以要这个

  <we:DateTimeRangeElement Date="{Binding Path=Filter.EndTime, Mode=TwoWay}" /> 

回答

2

可以使用的TypeConverter属性上你的类,传递您的实现的IValueConverter作为论据。

例如:

[TypeConverter(typeof(FriendlyDateTimeValueConverter))] 
public class FriendlyDateTime 
{ 
... 
} 

有一个在http://blogs.windowsclient.net/rob_relyea/archive/2008/04/10/strings-to-things-or-how-xaml-interprets-attribute-values.aspx一个很好的解释。

+0

我不能得到这个工作w/2方式绑定。 – 2012-01-17 18:19:05

+0

在'ConvertBack'中放置一个断点。它会被调用吗?如果是这样,会发生什么? – 2012-01-17 22:31:32

相关问题