WPF DatePicker show DateTime.Today OR Binding - wpf

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.

Related

Custom DateTime stringformat in WPF

I can't get my custom DateTime string format to work in my binding. I want the format to be "mmmm, yyyy" (e.g. "June, 2012").
The following does not work. I get a short date format (m/d/yyyy).
<TextBlock Text="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
I've considered using a converter, but I prefer a pure XAML approach.
Edit:
For clarity, I have a Window with a dependency property Date of type DateTime. In my XAML, I've named the window 'Thiswindow'.
Edit 2:
I looked back at my actual code, and I had a Label, not a TextBlock. I changed it to TextBlock and it works fine.
<Label Content="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
Anyone know why it doesn't work with Label?
Thanks.
ContentControls have a ContentStringFormat property which overrides the original formatting.
(When i saw your question i expected this to be the problem actually but was surprised to find a TextBlock at first)
Your month needs to be in uppercase:
{Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:MMMM\, yyyy}}
EDIT:
The Label problem is probably because Label has Content, not Text.
Change the Text="{Binding ...}" to Content="{Binding ...}"

TextBox to TextBox on the fly data binding

There are two TextBox on the WPF page. While user is typing into the first TextBox, the second TextBox must display modified text from the first TextBox. As a result both values must be binded to a view model.
Can it be done with Data Binding?
I was able to get the second TextBox display text from the first one implementing DependencyProperty on my view model. But I don't have the slightest idea how to apply transformation on the fly.
Maybe there is an easy way to achieve this?
Just use databinding with UpdateSourceTrigger set to PropertyChanged:
<TextBox x:Name='txt1' Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name='txt2' Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
In this case it will update ViewModel's property on the fly instead of waiting for FocusLost.

Combobox's SelectedValue (or SelectedItem) OneWay binding not working. Any ideas?

In the below window, the Existing Reports combo is bound to an observeablecollection of reportObjects. I have a reportObject property currentReport bound to the combo's SelectedValue property, OneWay. However, that's not working when bound in XAML.
SelectedValue="{Binding currentReport, Mode=OneWay}"
TwoWay binds fine, but I can't do it that way without writing an undo() method to the reportObject class. I'm binding the currentReport's properties to the various textboxes for editing. I want to bind OneWay so the source doesn't get changed. The currentReport's properties are all TwoWay bound to the corresponding textboxes so when I update the table in SQL [Save], it'll pull from that object, who's data is current.
<TextBox Text="{Binding currentReport.reportName, Mode=TwoWay}"
All of the properties bound from currentReport to the textboxes work fine as well. The only problem is the OneWay binding from the SelectedValue to the currentReport object. Does anyone have any ideas how to get this to work? I saw there was a bug, but the post I saw was 2009.
Sorry about the yellow. Not my idea. =)
EDIT: Added this XAML just in case.
<ComboBox ItemsSource="{Binding reportsCollection}" SelectionChanged="cboReports_SelectionChanged"
DisplayMemberPath="displayName"
SelectedValue="{Binding currentReport, Mode=TwoWay}"
x:Name="cboReports" Width="342" Height="40" VerticalAlignment="Center"/>
Forget about you need to change values - that is a separate problem - need to review your data design. Start with the UI problem question. If you want a user to be able to select an item from a combo box then it must have two way binding. Your first question is SelectedValue="{Binding currentReport, Mode=OneWay}" is failing why?

How to set TextSearch.Text for a combobox whose TextBlock uses converter?

In the below code, Combobox is wired to NameInfo object along with a converter.NameInfoConverter returns a format in which items in combobox are shown in a particular format (for eg: LastName, FirstName (Badge#) )
Now, when I set TextSearch.Text="{Binding NameInfo, Converter={StaticResource NameInfoConverter}, ConverterParameter=true}" on combobox; TextSearch doesn't work. When I set TextSearch.TextPath="Name", search itself works but doesnot get the correct format displayed in the selectionbox of combobox.
Any Ideas?
<StackPanel>
<ComboBox x:Name:"cmbName">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name,
Converter={StaticResource NameInfoConverter}, ConverterParameter=true}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
You've probably hit a limitation in the API. I suggest you take an alternative route and bind directly to a property that is correctly formatted for your textblock.
If this is a serious app, you may want to look into using the MVVM pattern and place your converted/formatted property in the viewmodel. Otherwise, just create a new property on your databound class called NameInfo or something and do the conversion from that.

How to change date format in Silverlight DatePicker control?

This works great:
<my:DatePicker IsTodayHighlighted="True" Width="200">
</my:DatePicker>
But I want to format the date, something like this:
<my:DatePicker IsTodayHighlighted="True" Width="200" Format="yyyy-mm-dd">
</my:DatePicker>
Anyone know the syntax for this?
Unfortunately the DatePicker control currently does not support free DateTime formats.
If this is something you're interested in seeing up support in future version of DatePicker, please create a codeplex feature request that suggests that.
http://silverlight.codeplex.com/WorkItem/Create.aspx
Just to point out that the new Silverlight Toolkit March 2009 TimePicker & TimeUpDown controls do support a full range of globalization options. One of those include free DateTime formats. So it is just a matter of public interest on whether or not we port that ability back to DatePicker.
Have a look at the format for TimePicker #
http://silverlight.codeplex.com/Wiki/View.aspx?title=Silverlight%20Toolkit%20Overview%20Part%201#TimePicker
In the meanwhile, The best workaround is to either change the local culture or the format on the local culture.
public App()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
or change the format on the local culture.
public App()
{
Thread.CurrentThread.CurrentCulture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "D/m/yyyy";
You should define a custom control template and edit the textbox of the datepicker control to format the text.
The Silverlight DatePicker has a SelectedDateFormat Property on it, this may be what you are looking for.
You could just hide the controls textbox (with a smaller width), expose you're own (optionally set the IsEnabled to false) and use an Element binding and Converter. If you're using MVVM, then set the DataContext to your ViewModel. I suppose another option would be to overwrite the DataTemplate to not include the text box and do the same idea.
<StackPanel Orientation="Horizontal" Height="22">
<TextBox x:Name="textBox2" Width="106" Text="{Binding ElementName=datePicker2, Path=SelectedDate, Mode=TwoWay, Converter={StaticResource internationalDateTimeFormatConverter}}" />
<controls:DatePicker x:Name="datePicker2" IsTabStop="False" SelectedDate="{Binding TargetDatePicker, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Width="23" HorizontalAlignment="Left" />
</StackPanel>
UPDATE:
The TwoWay binding from the text box to the the date picker works well, but it doesn't update the ViewModel Property. So I'm going to set the IsEnabled=False and call it good.
I notice this is an answered question.
But I would like to notify about this link to a Silverlight 5 control toolkit I have recently started creating. It contains (among other controls) a DateTimeBox control in which you can handle both date and time within the same control. At this point, it is still under development, but it should be usable for most scenarios.

Resources