This is a migrated thread and some comments may be shown as answers.

[Solved] Keeping cell in edit mode

1 Answer 153 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Amit Patel
Top achievements
Rank 1
Amit Patel asked on 19 Jul 2011, 08:43 PM

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;
  }
 }

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitrina
Telerik team
answered on 25 Jul 2011, 03:15 PM
Hello Amit Patel,

  When you show the popup then it is detected as an element outside of the GridView. Therefore the focus is lost and by default the data is committed (as the default value on lost focus is to commit). You may change this behavior by setting the ActionOnLostFocus property of the RadGridView to "None".

If you would like to have this behavior only when showing the Calendar popup then in CreateCellElement you may get the RadGridView. On creating of the Popup set the action to "None", then on closing the Popup set the action to the default "CommitEdit" 
if (popup == null)
                    popup = new Popup();
 
                if (clubsGrid != null)
                {
                    clubsGrid.ActionOnLostFocus = ActionOnLostFocus.None;
                }
...
                btnClose.Click += delegate(object btnCloseSender, RoutedEventArgs btnCloseArg)
                {
                    popup.IsOpen = false;
                    if (clubsGrid != null)
                    {
                        clubsGrid.ActionOnLostFocus = ActionOnLostFocus.CommitEdit;
                    }
                };

In our DateTimeColumn we are using a single RadDatePicker for our DateTime column. 

Best wishes,
Didie
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
Amit Patel
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or