2012-01-14 4 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; 
     } 

    } 
} 

は、私はそれがW/Cを作るわたしのコンバータを使用し、自動的にWPFをamkeでしょう実装できるインタフェースのセットがありますallingクラスに対応。

ので、あなたが引数としてIValueConverterの実装を渡し、自分のクラスでのTypeConverter属性を使用することができ、この

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

からこの

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

答えて

2

に行きます。例えば

[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

私は2ウェイバインドで動作させることはできません。 –

+0

'ConvertBack'にブレークポイントを入れます。それは呼び出されますか?もしそうなら、どうなりますか? –

関連する問題