wpf 文本框绑定日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27453733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf textbox binding datetime
提问by Serg
Im trying to set date time to a textbox but that gives an exception: System.Windows.Data.Binding.Path' threw an exception.
我试图将日期时间设置为文本框,但这给出了一个异常:System.Windows.Data.Binding.Path' 抛出了一个异常。
If I use TextBlock everything is fine.
如果我使用 TextBlock 一切都很好。
any help will be appreciated
任何帮助将不胜感激
thank you
谢谢你
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 '}"/>
回答by Osman Esen
This binding works using a TextBox and should be what you're looking for:
此绑定使用 TextBox 工作,应该是您要查找的内容:
<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"/>
result:
结果:


回答by ELH
this works fine when you set the Binding Mode in the TextBoxto OneWay:
当您在TextBoxto 中设置绑定模式时,这可以正常工作OneWay:
<TextBox Grid.Row="1" Name="block1" Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='yyyy-MM-dd HH:mm:ss ',Mode=OneWay}"/>
this is due to the fact that the default Binding mode in TextBlockis OneWayand in TextBoxis TwoWay
这是因为默认绑定模式 in TextBlockisOneWay和 in TextBoxisTwoWay

