Wpf datetime binding textbox
I am trying to set a date in a textbox, but it gives an exception: System.Windows.Data.Binding.Path 'threw an exception.
If I use TextBlock everything is fine.
any help would 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 '}"/>
+3
source to share
2 answers
This binding works using a TextBox and should be what you are 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:
+7
source to share
this works great when you set the binding mode 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 because the default binding mode in TextBlock
is equal OneWay
and in TextBox
is equalTwoWay
+5
source to share