1

I have two controls a WPF DatePicker and the WPF extenstion toolkit DateTimeUpDown. The DatePicker has a two way binding to a DateTime Property in a ViewModel and the DateTimeUpDown has a binding to the DatePicker through Element.

The binding works fine regarding scrolling the DateTimeUpDown, this changes the DatePicker control. However, when the initial value of the property in the ViewModel is set the DateTimeUpDown value isn't set.

This is more or less how it looks: In Resources.xaml

<StackPanel Name="StartDate" Visibility="Collapsed">
  <TextBlock Text="Start Date" Margin="0, 0, 0, 2" />
  <DatePicker Name="StartDatePicker"  SelectedDate="{Binding FromDateTime, Mode=TwoWay, ValidatesOnDataErrors=True}" IsTodayHighlighted="False" Uid="ReportingStartDay" />
</StackPanel>
<StackPanel Name="StartTime" Visibility="Collapsed">
  <TextBlock Text="Start Time" Margin="0, 0, 10, 2" />                        
  <xctk:DateTimeUpDown Value="{Binding ElementName=StartDatePicker, Path=SelectedDate, Mode=TwoWay}" Background="White" Format="ShortTime" Height="26"  Margin="0,1,5,0" TextAlignment="Left"></xctk:DateTimeUpDown>
</StackPanel>

In the ViewModel

private DateTime fromDateTime;
public DateTime FromDateTime {
  get { return fromDateTime; }
  set {
    fromDateTime = value;
    OnPropertyChanged("FromDateTime");
  }
}

When the FromDateTime is set the DatePicker is set correctly, however the DateTimeUpDown value isn't set.


I have now tried adding tracing for the binding, which unfortunately doesn't help me much:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=36462666) for Binding (hash=21177529)
System.Windows.Data Warning: 58 :   Path: 'SelectedDate'
System.Windows.Data Warning: 62 : BindingExpression (hash=36462666): Attach to Xceed.Wpf.Toolkit.DateTimeUpDown.Value (hash=6941388)
System.Windows.Data Warning: 67 : BindingExpression (hash=36462666): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=36462666): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 :     Lookup name EndDatePicker:  queried DateTimeUpDown (hash=6941388)
System.Windows.Data Warning: 78 : BindingExpression (hash=36462666): Activate with root item DatePicker (hash=55504765)
System.Windows.Data Warning: 108 : BindingExpression (hash=36462666):   At level 0 - for DatePicker.SelectedDate found accessor DependencyProperty(SelectedDate)
System.Windows.Data Warning: 104 : BindingExpression (hash=36462666): Replace item at level 0 with DatePicker (hash=55504765), using accessor DependencyProperty(SelectedDate)
System.Windows.Data Warning: 101 : BindingExpression (hash=36462666): GetValue at level 0 from DatePicker (hash=55504765) using DependencyProperty(SelectedDate): DateTime (hash=-1518077112)
System.Windows.Data Warning: 80 : BindingExpression (hash=36462666): TransferValue - got raw value DateTime (hash=-1518077112)
System.Windows.Data Warning: 89 : BindingExpression (hash=36462666): TransferValue - using final value DateTime (hash=-1518077112)

UPDATE

I found the problem. Apparently my problem was due to the binding was to a specialized class where the property was defined on the parent class. When "overriding" the property implementation in the inherited class it works. This doesn't make sense, but it works.

dennis_ler
  • 659
  • 1
  • 9
  • 36
  • 1
    Any reason not to bind the UpDown to the FromDateTime property as well? – H H Jan 07 '15 at 10:59
  • I have tried this, but then the DatePicker doesn't get updated when the UpDown wraps around to another date (passing midnight) – dennis_ler Jan 07 '15 at 11:43
  • That sounds like another problem (that could/should be solved). – H H Jan 07 '15 at 12:29
  • Well switching out the binding for the DateTimeUpDown to be similar to the StartDatePicker, results in the DatePicker not getting updated when StartDatePicker wraps around. do you have any suggestion to solving this then ? – dennis_ler Jan 07 '15 at 13:06
  • Not without seeing the actual property. – H H Jan 07 '15 at 21:23
  • If you are referring to the FromDateTime property, then it's like shown above ? – dennis_ler Jan 08 '15 at 08:52

2 Answers2

2

You might want to try debugging the binding on your DateTimeUpDown. Something like:

<Window …
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
/>
 <xctk:DateTimeUpDown Value="{Binding ElementName=StartDatePicker, Path=SelectedDate, Mode=TwoWay, diagnostics:PresentationTraceSources.TraceLevel=High}" ...></xctk:DateTimeUpDown>

This will produce extra info in your output window which may help pinpoint where the value is being lost.

More info here: Debugging WPF Bindings

Scott
  • 533
  • 1
  • 3
  • 10
  • Thank you. I have added the trace to my original question, it doesn't help me much... – dennis_ler Jan 08 '15 at 08:53
  • Theres a reference to 'EndDatePicker' in that trace - do you want to double check your binding? – Scott Jan 08 '15 at 21:45
  • Ahhh, sorry, that's because I have the same situation for then end date and I ran the diagnostics on the DateTimeUpDown binding to the EndDatePiker instead of the one binding to the StartDatePicker. Doing it on the other DateTimeUpDown control that binds to the StartDatePicker, results in the same except that it's the StartDatePicker instead. – dennis_ler Jan 09 '15 at 09:27
0

You should try to add UpdateSourceTrigger=PropertyChanged in the second binding.

In a two-way binding, it will force it to update the source on PropertyChanged events.

sk_
  • 2,105
  • 17
  • 31
  • Tried it but the DateTimeUpDown doesn't get updated with the value, it is just 00:00 instead of for example 14:43 – dennis_ler Jan 07 '15 at 14:11