Serg
Serg

Reputation: 103

wpf textbox binding datetime

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

Answers (2)

Osman Esen
Osman Esen

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:

enter image description here

Upvotes: 10

SamTh3D3v
SamTh3D3v

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

Related Questions