输入文本时 DatePicker.SelectedDate 不改变
当我的用户通过 DatePicker 中的 Calander 控件选择日期时,该值会正确绑定到基础对象。但是,如果用户在 DatePicker 中键入日期,然后单击按钮,则文本不会设置为 SelectedDate 属性。
用户必须将光标从 DatePicker 内的 TextBox 中移除才能更新绑定对象。
<toolkit:DatePicker Name="_dpField" Grid.Column="1" MinWidth="100"
ToolTip="{Binding Path=ToolTipText}"
TextInput="_dpField_TextInput"
SelectedDate="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
帮助!我如何确保在按钮事件代码中使用此键入的值?
谢谢!
When my users select a date via the Calander control within the DatePicker, the value gets correctly bound to the underlying object. BUT, if the user types the date within the DatePicker, then clicks a button, the text is not set to the SelectedDate property.
The user has to remove the cursor from the TextBox within the DatePicker for the bound object to be updated.
<toolkit:DatePicker Name="_dpField" Grid.Column="1" MinWidth="100"
ToolTip="{Binding Path=ToolTipText}"
TextInput="_dpField_TextInput"
SelectedDate="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
HELP! how do i make sure that this typed value is used within the buttons event code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找到了一个更简单的解决方案,不需要
DateConverter
。我仅绑定到
Text
属性并使用TargetNullValue=''
。I found a easier solution where I don't need the
DateConverter
.I only bound to the
Text
Property and useTargetNullValue=''
.您可以使用转换器将键入的文本解析为有效的日期时间
示例
Xaml
You can use a converter for parsing your typed text to a valid datetime
Sample
Xaml
这是一个简单的解决方案,即使使用欧洲/德国日期格式“dd.MM.yyyy”也能帮助我。
添加到您的 xaml 根元素
,日期选择器如下所示:
希望它适合您!
Here a simple solution, that helped me even with the european/german date format "dd.MM.yyyy".
Add to your xaml root element
and the datepicker looks like this:
hope it works for you!
我能找到的唯一解决方案是通过设置
Focusable="False"
来禁止在 DatePicker 中输入日期,并且只允许从日历中进行选择。这样我们至少可以确保获得正确的日期。The only solution that I could find is to disable entering of the date in the DatePicker by setting
Focusable="False"
and only allowing selection from the calendar. This way we can at least make sure that we get the correct date.在我的特定情况下,我有一个按钮,在从文本更新日期之前在其 ViewModel 中启动了一个操作。但是,将下面的函数绑定到“单击”按钮(“单击”和“操作”均已绑定)会覆盖操作开始之前所需的日期。
编辑:实际上你甚至不需要触摸视图模型
In my particular case I had a Button that started an action in its ViewModel before the date was updated from text. However binding the function below to the buttons Click (both Click and Action are bound) overwrites the required date before the action starts.
Edit: Actually you don't even need to touch the viewmodel
这可能有点晚了,但我已经坚持了一段时间了。
如果您有另一个 WPF 元素,您可以将焦点更改为按钮按下事件开始时的元素,这将使日期选择器处理在其文本框中输入的任何文本。我只用组合框尝试过此操作,但它似乎有效,并且它允许您仍然对日期进行自定义格式(即 26/04/2016 而不是 04/26/2016)。我假设如果您没有任何可以将焦点更改为的内容,您也可以使用不可见元素。
This is probably a bit late, but I've been stuck on this for a while now.
If you have another WPF element you can change focus to that at the beginning of your button press event, this will make the datepicker process any text entered in it's textbox. I've only tried this with a combobox but it seems to work and it allows you to still have custom formatting on your dates (ie 26/04/2016 rather than 04/26/2016). I assume you would be able to use an invisible element as well if you don't have anything to change focus to.