WPF文本框绑定日期时间

7

我试图将日期时间设置到文本框中,但是出现了异常:System.Windows.Data.Binding.Path' threw an exception。

如果我使用TextBlock,一切都正常。

希望能得到任何帮助。

谢谢。

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 '}"/>
2个回答

10

此绑定使用文本框,并且应该是您要查找的内容:

<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"/>

结果:

enter image description here


8

当你将 TextBox 的绑定模式设置为 OneWay 时,这个功能可以正常工作:

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

这是因为TextBlock控件的默认绑定模式为单向绑定(OneWay),而TextBox控件的默认绑定模式为双向绑定(TwoWay)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接