private DateSelector GenerateDateSelector(bool isEditing, DataGridCell cell) { DateSelector dateSelector = (cell != null) ? (cell.Content as DateSelector) : null; if (dateSelector == null) { dateSelector = new DateSelector(); } dateSelector.ConsumerFontSize = 13; dateSelector.InputTextWidth = 15; dateSelector.HorizontalAlignment = HorizontalAlignment.Center; ApplyBinding(dateSelector, DateSelector.SelectedDateProperty); ApplyBinding(dateSelector, DateSelector.CommandProperty); ApplyBinding(dateSelector, DateSelector.CommandParameterProperty); if (isEditing) { // 变换焦点才可以获取。 this.Dispatcher.BeginInvoke(new Action(() => { dateSelector.Focusable = true; dateSelector.Focus(); }), System.Windows.Threading.DispatcherPriority.Input); } return(dateSelector); }
/// <summary> /// Called when a cell has just switched to edit mode. /// </summary> /// <param name="editingElement">A reference to element returned by GenerateEditingElement.</param> /// <param name="editingEventArgs">The event args of the input event that caused the cell to go into edit mode. May be null.</param> /// <returns>The unedited value of the cell.</returns> protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) { DateSelector dateSelector = editingElement as DateSelector; if (dateSelector != null) { // 变换焦点才可以获取。 dateSelector.Focusable = false; return(dateSelector.SelectedDate); } return((DateTime?)null); }
public static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateSelector dp = d as DateSelector; string propName = e.Property.Name; if (propName == "ConsumerFontSize") { int fontSize = (int)e.NewValue; dp.tbHourSpecial.FontSize = fontSize; dp.tbMinuteSpecial.FontSize = fontSize; dp.tbSplitSpecial.FontSize = fontSize; } else if (propName == "SelectedDate" && null == dp.SelectedDate) { dp.SelectedDate = null; dp.tbHourSpecial.Text = string.Empty; dp.tbMinuteSpecial.Text = string.Empty; } else if (propName == "SelectedDate" && null != dp.SelectedDate && dp.SelectedDate != DateTime.MinValue && dp.SelectedDate != DateTime.MaxValue && string.IsNullOrEmpty(dp.tbHourSpecial.Text) && string.IsNullOrEmpty(dp.tbMinuteSpecial.Text)) { DateTime dtValue = DateTime.Parse(dp.SelectedDate.ToString()); dp.tbHourSpecial.Text = dtValue.Hour.ToString().PadLeft(2, '0'); if (dp.tbHourSpecial.IsEnabled) { bool tt = dp.tbHourSpecial.Focus(); tt = dp.tbHourSpecial.Focusable; } dp.tbMinuteSpecial.Text = dtValue.Minute.ToString().PadLeft(2, '0'); dp.calendar.SelectedDate = dtValue; dp.gridMain.ToolTip = dtValue.ToString("yyyy-MM-dd HH:mm"); } //else if (propName == "IsDefaultValue") //{ // bool result = (bool)e.NewValue; // if (!result && null == dp.SelectedDate) // { // dp.SelectedDate = null; // dp.tbHourSpecial.Text = string.Empty; // dp.tbHourSpecial.Focus(); // dp.tbMinuteSpecial.Text = string.Empty; // } //} }