Hello,
Some time back I had posted a question on forum if you guys have any plan to support "*" or string for DatePicker, but I did not see any udpate to it. Anyway, I need this feature, and trying to create my own customDate picker column, and it seems to be doing what I need to do, except for one place. When I change a date on my calendar control, cell get updated with selected date and comes out of edit mode. But I need to keep this cell in edit mode until user closes the date picker control. How are you guys pulling this off with your date picker column?
Thanks,
Amit
See my code Below:
public class CustomDateColumn : GridViewBoundColumnBase
{
private Popup popup = null;
public bool OptionalClear { get; set; }
public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
{
var textBlock = new TextBlock();
textBlock.SetBinding(TextBlock.TextProperty, new Binding(DataMemberBinding.Path.Path) { Mode = BindingMode.TwoWay});
return textBlock;
}
public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
{
var grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition() { MinWidth = 100 });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
var textBox = new TextBox();
textBox.BorderThickness = new Thickness(0);
textBox.SetBinding(TextBox.TextProperty, new Binding(DataMemberBinding.Path.Path) { Mode = BindingMode.TwoWay });
Grid.SetColumn(textBox, 0);
grid.Children.Add(textBox);
Image calendarImage = new Image();
calendarImage.Source = new BitmapImage(new Uri("Calendar.png", UriKind.RelativeOrAbsolute));
Grid.SetColumn(calendarImage, 1);
calendarImage.MouseLeftButtonUp += delegate(object sender, MouseButtonEventArgs e)
{
if (popup == null)
popup = new Popup();
Border border = new Border()
{
BorderThickness = new Thickness(1),
BorderBrush = new SolidColorBrush(Colors.Black)
};
var sp = new StackPanel();
border.Child = sp;
sp.DataContext = dataItem; //set dataContext
var calendar = new RadCalendar();
calendar.BorderThickness = new Thickness(0);
calendar.SelectionChanged += (dateChange, arg) =>
{
cell.IsInEditMode = false;
cell.IsInEditMode = true;
};
calendar.SetBinding(RadCalendar.SelectedDateProperty, new Binding(DataMemberBinding.Path.Path)
{
Mode = BindingMode.TwoWay,
Converter = new DateTimeConverter(),
ConverterParameter = cell
}
);
sp.Children.Add(calendar);
Grid linkButonGrid = new Grid() {HorizontalAlignment = HorizontalAlignment.Stretch};
linkButonGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
linkButonGrid.ColumnDefinitions.Add(new ColumnDefinition());
sp.Children.Add(linkButonGrid);
HyperlinkButton btnClose = new HyperlinkButton() { Content = "Close" };
btnClose.Click += delegate(object btnCloseSender, RoutedEventArgs btnCloseArg)
{
popup.IsOpen = false;
};
linkButonGrid.Children.Add(btnClose);
Grid.SetColumn(btnClose, 0);
HyperlinkButton btnClear = new HyperlinkButton() { Content = "Clear"};
btnClear.HorizontalAlignment = HorizontalAlignment.Right;
btnClear.Click += delegate(object btnCloseSender, RoutedEventArgs btnCloseArg)
{
//clear the value
};
linkButonGrid.Children.Add(btnClear);
Grid.SetColumn(btnClear, 1);
var position = e.GetPosition(null);
popup.Child = border;
popup.HorizontalOffset = position.X;
popup.VerticalOffset = position.Y;
popup.IsOpen = true;
};
grid.Children.Add(calendarImage);
return grid;
}
}
public class DateTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string date = value as string;
if (!string.IsNullOrEmpty(date) && date.Trim() == "*")
{
return "*";
}
System.Diagnostics.Debug.WriteLine(value);
return value;
}
}