2010-11-10 6 views
0

Pourquoi le PropertyChangedEvent n'est-il pas déclenché lorsque j'ai une valeur vide pour un DatePicker dans Silverlight?Valeur de liaison vide

Voici mon exemple 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> 

code Derrière:

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

Mon Code modèle (Comme vous pouvez le voir, j'ai essayé Nullable<DateTime> et 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)); 
     } 
    } 
} 

Répondre

0

Le problème auquel je faisais face était le fait que ConvertBack lançait une exception c'est pourquoi PropertyChanged n'a jamais été tiré.

J'ai également changé le contrôle Date pour lier à SelectedDate plutôt que Text comme Mark Heath suggéré.

Mise à jour Xaml

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

Voici mon convertisseur ...

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

Voici une implémentation légèrement différente - http://madprops.org/blog/a-nullable-datetime-ivalueconverter-and-validationrule/ –

0

ne devrait pas vous reliez à la propriété SelectedDate du DatePicker au lieu de Text?

+0

Oui, je l'ai changé. Merci – Gabe

Questions connexes