【问题标题】:Control binding to other Control through ElementName, but Initial value isn't shown控件通过 ElementName 绑定到其他控件,但不显示初始值
【发布时间】:2015-01-07 10:49:06
【问题描述】:

我有两个控件,一个是 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>

在视图模型中

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)

更新

我发现了问题。显然我的问题是由于绑定到一个专门的类,该类的属性是在父类上定义的。当“覆盖”继承类中的属性实现时,它可以工作。这没有意义,但它有效。

【问题讨论】:

  • 还有什么理由不将 UpDown 绑定到 FromDateTime 属性?
  • 我已经尝试过了,但是当 UpDown 环绕到另一个日期(经过午夜)时,DatePicker 没有得到更新
  • 这听起来像是另一个问题(可以/应该解决)。
  • 将 DateTimeUpDown 的绑定切换为与 StartDatePicker 类似,导致在 StartDatePicker 环绕时 DatePicker 不会更新。那你有什么解决这个问题的建议吗?
  • 不是没有看到实际属性。

标签: c# wpf binding datepicker elementname


【解决方案1】:

您可能想尝试调试 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

【讨论】:

  • 谢谢。我已将跟踪添加到我原来的问题中,它对我没有多大帮助......
  • 该跟踪中有对“EndDatePicker”的引用 - 您要仔细检查您的绑定吗?
  • 啊,抱歉,那是因为我在结束日期时遇到了同样的情况,我在 DateTimeUpDown 绑定到 EndDatePiker 而不是绑定到 StartDatePicker 上运行了诊断。在绑定到 StartDatePicker 的另一个 DateTimeUpDown 控件上执行此操作,结果相同,只是它是 StartDatePicker。
【解决方案2】:

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

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

【讨论】:

  • 试过了,但 DateTimeUpDown 没有用该值更新,它只是 00:00 而不是例如 14:43
猜你喜欢
  • 2013-07-07
  • 2017-11-02
  • 2019-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
相关资源
最近更新 更多