I have a datepicker in a Datagrid row. What I want is when a user clicks in the row and the datepicker that the date goes to today's date.
This code below does work if you go straight to the datepicker and then go and pick a date, however the problem is when you click inside the row first then the datepicker goes to 01/01/0001.
I have this XAML but it does not work.
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{x:Static System:DateTime.Now}"></DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
Any help would be grateful
Related
I have bound a ComboBox to a list of DateTime values to select a start time for an event. I am using an ItemTemplate to specify the formatting of the DateTime in the list. I also want the user to be able to manually specify a start time not in the list, like 8:27 AM or 9:30 PM.
Wiring that up is not the issue; rather, I want the user to be presented with the same formatted DateTime as the list.
<ComboBox x:Name="StartTimeButton"
ItemsSource="{Binding DataContext.StartTimes,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding DefaultStartTime}"
IsEditable="True">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label>
<TextBlock Text="{Binding StringFormat='{}{0:hh:mm tt}'}" />
</Label>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
This results in:
I have been unable to determine how to format the top TextBlock used by the ComboBox (when editing is enabled) to match the formatting of the dropdown area.
The ultra shorthand that gets you halfway there (you can drop the DataTemplate):
<ComboBox ItemsSource="{Binding Times}"
IsEditable="True" ItemStringFormat="hh:mm tt"
TextSearch.TextPath="Hour"/>
The problem is, that DateTime has no property that outputs the time in that format. Personally, i would write a wrapper that has an internal DateTime and a property that facilitates formatting to string and parsing from it.
There does not appear to be a standard way of creating a new item form the entered text, so you might need to parse the ComboBox.Text manually in case a value is entered that is not in the list.
I created two DatePickers (dp1 and dp2) in WPF. When I select a date on the dp1, I would like dp2 to update automatically: The date from dp1 should come to dp2. Any ideas on how I should do it?
I prefer a XAML only solution over to codebehind answer.
<StackPanel>
<DatePicker Name="dp1"></DatePicker>
<DatePicker SelectedDate="{Binding ElementName=dp1, Path=SelectedDate}"></DatePicker>
</StackPanel>
Inside my window, I have a simple datagrid control with a dateTimePicker column, and a textbox column. I group the rows inside the datagrid by a month-year string derived from the dateTimePicker. Here is the xaml for the columns and the grouping.....
<DataGridTemplateColumn Header="Date" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<xctk:DateTimePicker IsEnabled="True" Format="Custom" FormatString="M/d/yyyy h:mm" Value="{Binding theDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Text" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox TextWrapping="Wrap" AcceptsReturn="True" MaxLines="2" VerticalScrollBarVisibility="Auto" MaxLength="150" Text="{Binding theText, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" FontWeight="Bold" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
I have a very simple class with the text string, and datetimepicker datetime, and a string for month-year that I create dynamically. Here is the C# code for the class...
public DateTime dueDate { get; set; }
public String task { get; set; }
private String _monthYear;
public String monthYear
{
get { return dueDate.Month.ToString() + " - " + dueDate.Year.ToString(); }
}
Here is my initialization of the grouping
myCollectionView = CollectionViewSource.GetDefaultView(myObservableCollectionList);
myCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("monthYear"));
MyDataGrid.ItemsSource = myCollectionView;
// In this case, myObservableCollectionList is an observable collection that holds the datagrid
// rows. myCollectionView is an ICollectionView object
Everything is working properly except one very annoying piece of unexpected behavior. I don't know if this is a bug with the DateTimePicker control from the extended WPF toolkit.
Whenever I change the month or year on the DateTimePicker control and click on a new row or a different column in the same row, the row where I changed the date will not be grouped but will remain inside the old month-year date group until I sort it. This behavior would be fine if it was consistent HOWEVER...
If i were to change the month or year on the DateTimePicker control, and then tab through or click through the same DateTimePicker control (Not changing anything), and then click on a new row, the old row will be grouped into a new month - year category.
I'm not sure but I feel like this is a bug with the DateTimePicker control where it's calling some sort of event when you tab through the control even if you don't change date. However right now I'm pretty confused, and was wondering if anyone has any insight as to why this is happening.
*NOTE** I have tested this with and without the INotifyPropertyChanged, I do have the interface implemented, and the behavior occurs with and without it implemented I thought I would just leave it out for simplicity sake.
Any help is appreciated thank you!
I have fixed this issue, it was caused by the uses of both a _monthYear as well as a monthYear, so I guess whenever i would change the date, there would be multiple calls to change _monthYear. For whatever reason, I'm assuming something to do with the group description...
taskScheduleCollection.GroupDescriptions.Add(new PropertyGroupDescription("monthYear"));
maybe mixed in with the ValueChanged or a similar event inside the DateTimePicker would cause the grouping to re apply and new row groups would be created(I'm not 100% sure about this).
I fixed this issue by just removing the _monthYear property as it was unnecessary and just keeping a single monthYear property that was dependent on the date.
public String monthYear
{
get { return _date.Month.ToString() + " - " + _date.Year.ToString(); }
}
The grouping now only reapplys whenever i sort my list collection, which is the consistent behavior that I want.
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.
I want then when user select the date from datepicker on textbox it will show the date like 01-April-2012
Please tell me how it is done??
Thanks in advance
You have to format your textbox. Assuming the text box is already bounded to the DatePicker, it should be something like
<TextBlock Text="{Binding ElementName=DatePicker1, Path=Value, StringFormat={}{0:dd-MMMM-yyyy}}" />