控件通过 ElementName 绑定到其他控件,但不显示初始值

本文关键字:控件 显示 其他 ElementName 绑定 | 更新日期: 2023-09-27 18:36:29

我有两个控件,一个WPF DatePicker和WPF extenstion toolkit DateTimeUpDown。DatePicker 具有与 ViewModel 中的 DateTime 属性的双向绑定,DateTimeUpDown 具有通过 Element 与 DatePicker 的绑定。

绑定在滚动 DateTimeUpDown 时工作正常,这会更改 DatePicker 控件。但是,当在 ViewModel 中设置属性的初始值时,不会设置"日期时间上下"值。

这或多或少是它的外观:在 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 时,将正确设置日期选取器,但不会设置 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)

更新

我发现了问题。显然,我的问题是由于绑定到一个专门的类,其中属性是在父类上定义的。当"重写"继承类中的属性实现时,它会起作用。这没有意义,但它有效。

控件通过 ElementName 绑定到其他控件,但不显示初始值

您可能想尝试在 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 事件的源。