Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf textbox binding datetime

Tags:

wpf

textbox

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 '}"/>
like image 744
Serg Avatar asked Dec 12 '14 23:12

Serg


2 Answers

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:

enter image description here

like image 173
Osman Esen Avatar answered Nov 16 '22 13:11

Osman Esen


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

like image 40
SamTh3D3v Avatar answered Nov 16 '22 14:11

SamTh3D3v