簡體   English   中英

控件通過ElementName綁定到其他控件,但未顯示初始值

[英]Control binding to other Control through ElementName, but Initial value isn't shown

我有兩個控件,一個WPF DatePicker和一個WPF擴展工具包DateTimeUpDown。 DatePicker具有兩種方式綁定到ViewModel中的DateTime屬性,而DateTimeUpDown具有通過Element綁定到DatePicker的方式。

關於滾動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>

在ViewModel中

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

我發現了問題。 顯然,我的問題是由於綁定是在父類上定義了屬性的特定類上進行的。 當“覆蓋”繼承類中的屬性實現時,它將起作用。 這沒有道理,但可以。

您可能想嘗試調試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>

這將在您的輸出窗口中產生額外的信息,這可能有助於確定丟失值的位置。

此處更多信息: 調試WPF綁定

您應該嘗試在第二個綁定中添加UpdateSourceTrigger=PropertyChanged

在雙向綁定中,它將強制其在PropertyChanged事件上更新源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM