我正在使用 WPF toolkit TimePicker
<xctk:TimePicker Name="timePicker"
StartTime="{Binding MpStartTime,UpdateSourceTrigger=PropertyChanged}"
EndTime="{Binding MpEndTime,UpdateSourceTrigger=PropertyChanged}"
TextBoxBase.TextChanged="TimePicker_TextChanged"
Value="{Binding ResTime,UpdateSourceTrigger=PropertyChanged,Mode="TwoWay"}"
Format="Custom"
FormatString="hh:mm tt"/>
问题是当我使用小键盘手动输入时间时
PropertyChanged
没有触发,并且 View 模型中的时间没有更新。实际问题是直到失去焦点后值才会更新,所以当我添加以下行时:
private void TimePicker_TextChanged(object sender, TextChangedEventArgs e)
{
timePicker.Focus();
}
该值似乎发生了变化,但现在我无法在时间选择器中输入任何内容,因为每当文本更改时焦点都会丢失。有没有办法解决这个问题?
最佳答案
仅供引用,看来您正在使用 this question 中的解决方法.
你在说什么TimePicker_TextChanged
:
sender
is theTimePicker
control
好的,所以您将焦点设置为错误的控件。或者,实际上,它适用于其他人,因为它会导致
LostFocus
TextBox
的事件(将焦点设置为其他任何东西也可以),但不是当您想继续使用键盘输入值时。在 WPF 中自定义控件通常是复合的,所以我相信有一个
TextBox
某处的子元素。您可以尝试以下代码:
void TimePicker_TextChanged(object sender, TextChangedEventArgs e)
{
var textBox = FocusManager.GetFocusedElement(this) as TextBox;
if (textBox != null)
{
textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
textBox.Focus();
}
}
想法是找到 child
TextBox
,将焦点传递到其他地方,然后将其放回原处。这应该会触发 LostFocus
TextBox
的事件如果 TimePicker
将起作用使用与 UpdateSourceTrigger=LostFocus
的绑定(bind)或者如果它处理 LostFocus
(我不知道 xctk:TimePicker
到底是如何工作的)。另一种方法是更改
TextBox.Text
的绑定(bind)。拥有UpdateSourceTrigger=PropertyChanged
.如果您可以更改控制模板,那是最好的方法。
关于c# - WPF 工具包 TimePicker 值不会按时间手动输入更改,但会在焦点更改时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49772955/