wpf 文本框绑定日期时间 [英] wpf textbox binding datetime

查看:65
本文介绍了wpf 文本框绑定日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日期时间设置为文本框,但出现异常:System.Windows.Data.Binding.Path' 引发了异常.

Im trying to set date time to a textbox but that gives an exception: System.Windows.Data.Binding.Path' threw an exception.

如果我使用 TextBlock 一切正常.

If I use TextBlock everything is fine.

任何帮助将不胜感激

谢谢

xmlns:sys="clr-namespace:System;assembly=mscorlib"

 <TextBlock   Name="block" Text="{Binding Source={x:Static sys:DateTime.Now},  StringFormat='yyyy-MM-dd HH:mm:ss '}"/>

 <TextBox   Name="block1" Text="{Binding Source={x:Static sys:DateTime.Now},  StringFormat='yyyy-MM-dd HH:mm:ss '}"/>

推荐答案

此绑定使用 TextBox 工作,应该是您正在寻找的:

This binding works using a TextBox and should be what you're looking for:

<TextBox HorizontalAlignment="Left" Height="23" Margin="0,260,0,0" Text="{Binding Source={x:Static sys:DateTime.Now}, Mode=OneWay,  StringFormat='yyyy-MM-dd HH:mm:ss '}" VerticalAlignment="Top" Width="120"/>

结果:

这篇关于wpf 文本框绑定日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆