ReadOnly DatePicker in silverlight - silverlight

I am making an application in Silverlight 3.0. In that application i am using DatePicker control as
<TextBlock FontSize="13" Height="23" Name="txtFromDate" Text="FromDate" Width="40" Canvas.Left="444" Canvas.Top="6" />
<controls:DatePicker Name="fromdatePicker" Height="23" Width="110" Canvas.Left="550" Canvas.Top="4" />
Here i don't want to enter date manually(Means the textblock txtFromDate should be read only.)And the value in text block will be whatever date is selected from calender. I am not getting how to do it? Please help me.Thanks in advance.

Assuming you're using a nice MVVM approach:
Bind the SeletectedDate of your date picker to a DateTime property of your ViewModel (two-way binding):
<sdk:DatePicker SelectedDate="{Binding MyDate, Mode=TwoWay}"/>
Bind the Text property of your TextBlock to the same property:
<TextBlock Text="{Binding MyDate}" />

Related

Can't show the DataTemplate on a selected item in combobox?

Newbie question. In the following combobox, the drop-down list correctly displays the company name and phone number as per the DataTemplate. However, selecting an item from the drop-down (by mouse) resutls only in showing the object name:
Stargate_V.DataService.View_Small_Company
The selected company name and phone number is not shown. What am I doing wrong?
<ComboBox
ItemsSource="{Binding PrimaryInsurance.Companies}"
ItemTemplate="{StaticResource CompanyTemplate}"
IsEditable="True"
IsTextSearchEnabled="True"
Height="20" HorizontalAlignment="Left" Margin="375,235,0,0" VerticalAlignment="Top" Width="198" />
The CompanyTemplate is:
<DataTemplate x:Key="CompanyTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Companyname}" Width="240"/>
<TextBlock Text="|" Width="10"/>
<TextBlock Text="{Binding Phone}" Width="80" />
</StackPanel>
</DataTemplate>
TIA
The issue that you are having is the combination of displaying a complex type (i.e. Class) with a DataTemplate and your ComboBox has IsEditable set to true. When setting IsEditable to true the ComboBox doesn't know which property to use for searching so it just calls ToString on the object and displays the results. To fix this issue you need to set the TextSearch.TextPath to the property name on the object that you would like to use for searching. Once you do that it will display that property in the selection box instead of the result of ToString. Below is what your XAML should look like if you wanted to search on the Companyname property on your object.
<ComboBox
ItemsSource="{Binding PrimaryInsurance.Companies}"
ItemTemplate="{StaticResource CompanyTemplate}"
IsEditable="True"
IsTextSearchEnabled="True"
TextSearch.TextPath="Companyname"
Height="20" HorizontalAlignment="Left" Margin="375,235,0,0" VerticalAlignment="Top" Width="198" />

WPF Extended Toolkit DateTimePicker Dates validation

I have a scenario I can't figure out how to implement.
I want to validate that from date is less than the to date in my MVVM based WPF application. I am using the DateTimePicker control .
<StackPanel Orientation="Horizontal" Margin="5,2,5,2">
<TextBlock Text="From Date: " />
<xctk:DateTimePicker x:Name="FromDatePicker"
AutoCloseCalendar="True"
Width="200"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Value="{Binding FromDate, Mode=TwoWay}"
Format="Custom"
FormatString="dd-MMM-yy HH:mm"
TimeFormat="Custom"
TimeFormatString="HH:mm"
ToolTip="From Date">
</xctk:DateTimePicker>
<TextBlock Text="To Date: " />
<xctk:DateTimePicker x:Name="ToDatePicker"
AutoCloseCalendar="True"
Width="200"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Value="{Binding ToDate, Mode=TwoWay}"
Format="Custom"
FormatString="dd-MMM-yy HH:mm"
TimeFormat="Custom"
TimeFormatString="HH:mm" TimeWatermark="Please select time"
ToolTip="To Date"/>
</StackPanel>
Can someone give me a hint on how to validate that the to date is greater than the from date?
There are a number of ways to perform your validation. You can do it via the bound property setters that change when the value in the DateTimePicker controls value's change. Or if you have a button that is wired up to a Command binding in your model you can do the validation there as well...

Validation is not working in Textbox

I have a TExtBox and have created a ValidationRule class from here and this is my xaml :
<TextBox Name="ctsTxt" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"
local:SimpleValidator.ValidationType="{x:Type system:Double}"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource txtBoxStyle}"
Grid.Column="1" Grid.Row="2" Margin="2"
/>
As per this and the validator class, the textbox sh accept only double input, but it accepts everything.
What's wrong in xamls that my validation is not happening at all.
Any help is appreciated.
Did you try setting your Text Binding like this?
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"

Does StringFormat feature of WPF Xaml work on Label.Content?

I have bind my Amount Label's Content Property to a decimal property via DataContext. I am trying to apply stringformat but see no effect. Does StringFormat feature work on Label controls ?? Please tell me on which controls does this feature work. BTW following is the code for the Label Control for whom i want to apply the currency formatting
<Label Grid.Column="2" Content="{Binding Path=Amount, StringFormat={}{0:C}}" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120" />
StringFormat works on properties of type string (when the object you are binding to is being converted to a string the string format is applied). The Content property is of type Object.
You can place a TextBlock inside your label to achieve the desired effect:
<Label Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120">
<TextBlock Text="{Binding Path=Amount, StringFormat={}{0:C}}"/>
</Label>
Try ContentStringFormat
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/866f7934-8b10-4872-b306-122674fad5fa/
<Label Content=ā€{Binding Amount}ā€ ContentStringFormat=ā€Cā€ />

Formatting the date in the DatePicker control within a Silverlight DataForm

This is driving me nuts!
I have a RIA Services driven dataform in silverlight which contains a datepicker control. I want to display the date in ddMMMyyyy format. Here is a sample of the xaml:
<dataFormToolkit:DataForm x:Name="dataForm" AutoGenerateFields="False">
<dataFormToolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataFormToolkit:DataField Label="First Name">
<TextBox Text="{Binding PE_FIRST_NAME, Mode=TwoWay}"/>
</dataFormToolkit:DataField>
<dataFormToolkit:DataField Label="Surname">
<TextBox Text="{Binding PE_SURNAME_NAME,Mode=TwoWay}"/>
</dataFormToolkit:DataField>
<dataFormToolkit:DataField Label="Department">
<ComboBox x:Name="cboDepartment" DisplayMemberPath="CC_NAME" SelectedValuePath="CC_ID" SelectedValue="{Binding Path=CC_ID, Mode=TwoWay}"/>
</dataFormToolkit:DataField>
<dataFormToolkit:DataField Label="Start Date">
<controls:DatePicker Text="{Binding PE_START_DATE, Mode=TwoWay}"/>
</dataFormToolkit:DataField>
</StackPanel>
</DataTemplate>
</dataFormToolkit:DataForm.EditTemplate>
</dataFormToolkit:DataForm>
</StackPanel>
I have followed the advice given in
How to change date format in Silverlight DatePicker control?
and added the following lines to my App startup:
Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "ddMMMyyyy";
but it doesn't make any difference, the date always comes out in its raw format, e.g. 1/3/2006 12:00AM
Is there some reason why the current culture is not working in a dataform?
Update: If I implement the same form the hard way by not using a dataform then the date is in the correct format!
controls:DatePicker Text="{Binding PE_START_DATE, Mode=TwoWay, StringFormat='d'}"
You only have to add to the binding a StringFormat='MM/dd/yyyy' attribute.
<TextBox IsReadOnly="True" TextAlignment="Center" Text="{Binding AlbumDate, StringFormat='MM/dd/yyyy'}" />

Resources