Binding Datetime to Textbox in Silverlight - silverlight

I have couple of tables in database with Datatype DateTime and default value getdate() in sql server. while I am binding this to textbox it is displaying 1/1/0001 as default. I am using Silverlight Ria services.
<TextBox Text="{Binding Mode=TwoWay, Path=LST_UPDT_TS, StringFormat=\{0:D\}}"/>
Thank you for helping and for ur time..

The textbox is displaying the default value for DateTime, which means two things:
The Binding worked (else the TextBox would be empty).
The property DateTime LST_UPDT_TS... has not been set
You should check that the property is being set properly, and that you are binding to the correct instance.

Related

How to get short date format in my Textbox WPF

I have my table DataType is set to Date in SQL Server but is showing the DateTime in my WPF textbox and I'm not sure how I can format the textbox or SQL Server to only show Date.
I tried using DatePicker instead of a TextBox but I need it to be ReadOnly and the DatePicker is grayed out when it is set to NOT ENABLED and there is no option for ReadOnly.
How can I only display the short date in my TextBox?
I think this code should solve your problem:
<TextBox Text="{Binding PropertyPath, StringFormat=d}" />

How to access DataGridTextColumn String format from code behind c#?

I'm using WPF to display Products on my DataGrid, for database I am using MySql database,
decimals in mysql are stored as 100.30, 100.10, 90.40, as you can see mysql is using dot(".") as separator between decimals, and to show it on my screen as comma separated, I used next thing:
<DataGridTextColumn Binding="{Binding Price, StringFormat='{}{0:C}',ConverterCulture=EN}" Header="PRICE" FontSize="15" FontFamily="Verdana" Width="10*" />
I set culture info to my DataGridTextColum where I am displaying my price, but in case someone someday would like to change that culture info how could I acces this DataGridTextColumn and change culture info for that column "Price" .. ?
Thanks guys,
Cheers
If you give the column an x:Name (like "col1") in your XAML markup you should be able to access it and its properties programmatically like this:
Binding b = col1.Binding as Binding;
string format = b.StringFormat;
var c = b.ConverterCulture;
Note that you won't be able to change any property of the binding after it has been used though so if you need to change the culture or the StringFormat you must either do this in the constructor before the binding has been resolved or by simply editing the XAML markup. It can't be done programmatically after the binding has been resolved.

WPF DatePicker show DateTime.Today OR Binding

What is the best way to have a WPF DatePicker show a predefined DateTime (e.g. DateTime.Today) while still maintaining binding?
SelectedDate="{x:Static sys:DateTime.Today}"
and
Text="{Binding Path=MyPublicProperty.ADateTimeMemberProperty, Mode=TwoWay}"
don't play well together. When a UserControl is loaded, I'd like it to display today's date, until a row is selected. Is there a way to make them get along?
Text="{Binding Path=MyPublicProperty.ADateTimeMemberProperty,
Mode=TwoWay, FallbackValue={x:Static sys:DateTime.Today}}"
Use FallbackValue to set a value when it cannot be retrieved from the binding.

Nullable database property but texbox still shows red border when content deleted

Hi I am binding a WPF textbox to an Entity Framework property as follows:
<TextBox Grid.Column="1" Grid.Row="0" Margin="5,2"
Text="{Binding Path=MyEntityObject.SizeLower, Mode=TwoWay}" />
It binds fine to the property and when I change it, it saves to the DB as expected. But if I delete the content of the Textbox I get the red error border around it. I dont have any validator in place so I am guessing the texbox is complaining about the value not being nullable. But in fact this property in the DB is nullable, so I cannot understand why it would error.
The system generated EF property definition is as follows:
<EdmScalarPropertyAttribute(EntityKeyProperty:=false, IsNullable:=true)>
<DataMemberAttribute()>
Public Property SizeLower() As Nullable(Of Global.System.Int64)
Get
Return _SizeLower
End Get
Set
OnSizeLowerChanging(value)
ReportPropertyChanging("SizeLower")
_SizeLower = StructuralObject.SetValidValue(value)
ReportPropertyChanged("SizeLower")
OnSizeLowerChanged()
End Set
End Property
Private _SizeLower As Nullable(Of Global.System.Int64)
Is there something I am missing? I thought the binding system was able to determine if a property was nullable and allow nulls if so?
How can I see what the error is? Hovering doesnt seem to do the trick.
Thanks for any advice.
===================================
ADDITIONAL INFO
If I select all and delete, then change focus, the validation box appears. Here's a screencapture before and after. Also I have confirmed that I can manually put NULLs in the database for the bound properties so thats not the problem.
DENIED. Tried to put picture here but I dont have 10 points...!
Here is an offsite link instead: CLICK HERE
You should add the TargetNullValue property to your binding:
<TextBox Grid.Column="1" Grid.Row="0" Margin="5,2"
Text="{Binding Path=MyEntityObject.SizeLower,
Mode=TwoWay,
TargetNullValue=''}" />
This tells the binding to treat null values in MyEntityObject.SizeLower as string.empty for display, and string.empty as null when setting.

WPF databinding to a list of datetime objects

I have a combo box that I want to bind to a list of datetime objects, but I want to show the datetime objects in short time format. I'm pretty sure I need to use some form of data template for this, but I can't figure out how to bind to the datetime object's ToShortTime method within the data template.
Can someone point me in the right direction?
Assuming you're using .NET 3.0 or 3.5 with SP1, you can simply use the StringFormat to specify the format, for example:
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='{}{0:t}'}" />
Will show the current date time with short time format ('t' standard date time format modifier, exactly the same as calling DateTime.Now.ToString("t")).
Edit: If you're already in a data template having a DateTime as the DataContext, just use:
<TextBlock Text="{Binding StringFormat='{}{0:t}'}" />
You could set the Converter property on the binding. Implement IValueConverter to make the change to a string. The MSDN docs for IValueConverter actually use this as an example.
There is a property named:
Combobox.ItemStringFormat
here you can provide the Stringformat as usual. No need to Datatemplate the items for this purpose.
<ComboBox ItemsSource="{Binding MyDates}"
ItemStringFormat="yyyy-MM-dd" />

Resources