Reputation: 103
Im trying to set date time to a textbox but that gives an exception: System.Windows.Data.Binding.Path' threw an exception.
If I use TextBlock everything is fine.
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 '}"/>
Upvotes: 7
Views: 19831
Reputation: 1696
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"/>
result:
Upvotes: 10
Reputation: 9944
this works fine when you set the Binding Mode in the TextBox
to 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 TextBlock
is OneWay
and in TextBox
is TwoWay
Upvotes: 8