2015-01-07 30 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。 –

相關問題