2015-01-07 28 views
1

我有两个控件WPF DatePicker和WPF扩展工具包DateTimeUpDown。 DatePicker具有双向绑定到ViewModel中的DateTime属性,DateTimeUpDown通过元素绑定到DatePicker。控制绑定到其他控件通过ElementName,但初始值不显示

该绑定工作正常滚动DateTimeUpDown,这将更改DatePicker控件。但是,如果设置ViewModel中的属性的初始值,则不会设置DateTimeUpDown值。

这或多或少是它的外观: 在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> 

在视图模型

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

当FromDateTime设置的DatePicker的设置正确,但是DateTimeUpDown值没有设置。


我现在已经尝试添加跟踪结合,不幸的是没有帮助我很多:

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

我发现这个问题。显然,我的问题是由于绑定是在父类上定义属性的专门类。当“覆盖”继承类中的属性实现时,它会起作用。这没有任何意义,但它有效。

+1

任何理由不沿着上下结合FromDateTime财产呢? –

+0

我试过这个,但是当UpDown环绕到另一个日期(通过午夜)时,DatePicker没有得到更新 –

+0

这听起来像是另一个问题(可以/应该解决)。 –

回答

0

您应该尝试在第二个绑定中添加UpdateSourceTrigger=PropertyChanged

在双向绑定中,它将强制它更新PropertyChanged事件上的源。

+0

尝试了它,但DateTimeUpDown没有得到更新的值,它只是00:00而不是例如14:43 –

2

您可能想尝试调试DateTimeUpDown上的绑定。例如:

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

这将在您的输出窗口中产生额外的信息,这可能有助于找出丢失值的位置。

此处了解详情:Debugging WPF Bindings

+0

谢谢。我已经添加了跟踪到我原来的问题,这并没有多大帮助... –

+0

Theres在该跟踪中对'EndDatePicker'的引用 - 你想仔细检查你的绑定吗? – Scott

+0

Ahhh,很抱歉,那是因为我的情况与结束日期相同,我将DateTimeUpDown绑定到EndDatePiker而不是绑定到StartDatePicker的诊断程序运行。在绑定到StartDatePicker的另一个DateTimeUpDown控件上执行此操作,结果相同,只不过它是StartDatePicker。 –

相关问题