2010-11-10 37 views
0

为什么Silverlight中的DatePicker的值为空值时PropertyChangedEvent不会被触发?绑定空值

这里是我的榜样XAML

<UserControl x:Class="ValidationExamples.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:ig="http://schemas.infragistics.com/xaml" 
    xmlns:controls='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls' 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <StackPanel> 
      <controls:DatePicker Height="20" Width="100" x:Name="DatePicker" SelectedDate="{Binding Path=DOB, Mode=TwoWay}"/> 
      <Button Height="20" Width="100" Click="Button_Click" Content="Break"/> 
     </StackPanel> 
    </Grid> 
</UserControl> 

代码背后:

public partial class MainPage : UserControl 
{ 
    private Model MyModel; 

    public MainPage() 
    { 
     InitializeComponent(); 
     this.MyModel = new Model(); 
     this.MyModel.DOB = DateTime.Now; 
     this.DataContext = this.MyModel; 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
      // Empty on purpose 
    } 
} 

我的模型代码(正如你可以看到我试过Nullable<DateTime>DateTime):

public class Model : INotifyPropertyChanged 
{ 
    private Nullable<DateTime> dob; 
    public Nullable<DateTime> DOB 
    { 
     get 
     { 
      return this.dob; 
     } 
     set 
     { 
      this.dob = value; 
      NotifyPropertyChanged("DOB"); 
     } 
    } 

    //private DateTime dob; 
    //public DateTime DOB 
    //{ 
    // get 
    // { 
    //  return this.dob; 
    // } 
    // set 
    // { 
    //  this.dob = value; 
    //  NotifyPropertyChanged("DOB"); 
    // } 
    //} 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 

回答

0

我面临的问题是,ConvertBack抛出异常,这就是为什么PropertyChanged从未解雇。

我还将Date控件更改为绑定到SelectedDate而不是Text,因为Mark Heath建议。

更新的XAML

<controls:DatePicker Height="20" Width="100" x:Name="DatePicker" SelectedDate="{Binding Path=DOB, Mode=TwoWay, Converter={StaticResource DateConverter}}"/> 

这里是我的转换器...

public class DateConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     DateTime? result = null; 
     DateTime result2; 

     if (value != null) 
     { 
      if (!String.IsNullOrEmpty(value.ToString())) 
      { 
       if (DateTime.TryParse(value.ToString(), out result2)) 
       { 
        return result2; 
       } 
      } 
     } 

     return result; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     DateTime? result = null; 
     DateTime result2; 

     if (value != null) 
     { 
      if (!String.IsNullOrEmpty(value.ToString())) 
      { 
       if (DateTime.TryParse(value.ToString(), out result2)) 
       { 
        return result2; 
       } 
      } 
     } 

     return result; 
    } 
} 
+0

这里有一个稍微不同的实现 - http://madprops.org/blog/a-nullable-datetime-ivalueconverter-and-validationrule/ – 2011-04-27 21:22:27

0

不应该你对的SelectedDate财产具有约束力而不是Text

+0

是的,我已经改变了。谢谢 – Gabe 2010-11-10 17:52:05