How to access a control under CellEditTemplate's DataTemplate programmatically?

14 posts, 1 answers
  1. Stephen
    Stephen avatar
    43 posts
    Member since:
    Apr 2012

    Posted 01 Aug 2012 Link to this post

    I have a gridview, that has a GridViewDataColumn column.  It has a CellEditTemplate, with a DataTemplate inside of it.  In there I have a RadDateTimePicker.  How do I access that at the time the row's are loaded?  I think I am close, but I must either have the wrong event, or I am trying to get to it the wrong way.  What I am trying to do is set limits on the dates the user can select, based on another control.

    Here is my Xaml:
    <telerik:RadGridView x:Name="rgvMainGrid"
    Width="1088"
    MaxWidth="1088"
    Margin="5"
    HorizontalAlignment="Left"
    AutoGenerateColumns="False"
    CanUserDeleteRows="False"
    CanUserInsertRows="False"
    IsReadOnly="True"
    ScrollViewer.VerticalScrollBarVisibility="Auto" RowLoaded="rgvMainGrid_RowLoaded">
    <telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn Width="160"
    DataMemberBinding="{Binding StartTime}"
    EditTriggers="CellClick"
    Header="Start Time">
    <telerik:GridViewDataColumn.CellEditTemplate>
    <DataTemplate>
    <telerik:RadDateTimePicker x:Name="rdpStartTimePicker"
    SelectedValue="{Binding StartTime,
    Mode=TwoWay}" />
    </DataTemplate>
    </telerik:GridViewDataColumn.CellEditTemplate>
    </telerik:GridViewDataColumn>
    <telerik:GridViewDataColumn Width="160"
    DataMemberBinding="{Binding EndTime}"
    EditTriggers="CellClick"
    Header="End Time">
    <telerik:GridViewDataColumn.CellEditTemplate>
    <DataTemplate>
    <telerik:RadDateTimePicker x:Name="rdpEndTimePicker"
    SelectedValue="{Binding EndTime,
    Mode=TwoWay}" />
    </DataTemplate>
    </telerik:GridViewDataColumn.CellEditTemplate>
    </telerik:GridViewDataColumn>
    <telerik:GridViewDataColumn Width="*"
    DataMemberBinding="{Binding Notes}"
    Header="Notes"
    TextWrapping="Wrap" />
    </telerik:RadGridView.Columns>
    </telerik:RadGridView>


    Here is my code behind:
    private void rgvMainGrid_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
    {
        var _Picker = e.Row.FindName("rdpStartTimePicker") as RadDateTimePicker;
     
        if (_Picker != null)
        {
            if (rdpDate.SelectedDate != null)
            {
                _Picker.SelectableDateStart = rdpDate.SelectedDate;
                _Picker.SelectableDateEnd = rdpDate.SelectedDate.Value.AddDays(1);
            }
        }
    }

    Am I close?  I tried looking through everything under e and e.Row but I couldn't find where rdpStartTimePicker is mentioned.
  2. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 02 Aug 2012 Link to this post

    Hi Stephen,

    I would recommend you to count on Bindings instead - you can expose properties to bind to SelectableDateStart and SelectableDateEnd.
    Nevertheless, I would recommend you to use ChildrenOfType<T>()/ ParentOfType<T>() extension methods rather than FindName. 

    All the best,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Stephen
    Stephen avatar
    43 posts
    Member since:
    Apr 2012

    Posted 02 Aug 2012 Link to this post

    Awesome!  That is a great suggestion.  I never thought about putting those properties in my collection, then binding the property value to what I had in my collection.
  5. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 24 May 2013 Link to this post

    How am I suppose to bind the CultureInfo to in order to have the DateTimeFormat.ShortDatePattern set ?
  6. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 24 May 2013 Link to this post

    What I mean is that we are trying to make a RadDatePicker not editable and having the DateSelectionMode to Month in a Column of the grid.  We have succeeded with the RadDatePicker on the form because in code we can set the culture of it to a new culture having the DateTimeFormat.ShortDatePattern to "MM yyyy" however we're unable to figure out how to bind the RadDateTimePicker in the DataTemplate of the column

  7. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 24 May 2013 Link to this post

    When we click the Calender icon and change the value, nothing change in the Editor because it's IsReadOnly = true however it needs to be readonly.  We don't want the user to edit the value in the editor however making so, any attempt to select a date with the picker result in nothing
  8. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 28 May 2013 Link to this post

    Hello Mark,

    You can find a control in a cell with the help of ChildrenOfType<T>() extension method. In you case it will be ChildrenOfType<RadDateTimePicker>() - once you find the one you want, you can set date pattern. 
    As for clicking on the calendar icon - what is the exact behavior that you want to achieve, do you define date time picker in CellTemplate or CellEditTemplate, do you want just to display the values without being able to open the drop down of the picker ?

    Regards,
    Maya
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  9. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 30 May 2013 Link to this post

    Sir,

    With the following code, when we want to change the date by using the picker icon, it doesn't change the value even thought the mode is TwoWay.  The need is to have the Editor readonly HOWEVER, we should be able to change the date using the picker

    Maybe a IsEditorReadonly would have been great.

    <telerik:GridViewDataColumn Header="Date du compte" UniqueName="AccountDate" DataFormatString="{}{0:MMM-yyyy}">
        <telerik:GridViewDataColumn.CellEditTemplate>
            <DataTemplate>
                <telerik:RadDatePicker DateSelectionMode="Month" SelectedValue="{Binding AccountDate, Mode=TwoWay}" IsReadOnly="True"></telerik:RadDatePicker>
            </DataTemplate>
        </telerik:GridViewDataColumn.CellEditTemplate>
    </telerik:GridViewDataColumn>
  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 May 2013 Link to this post

    Hello Marc,

    I might be missing the point here, but if you want the property to be updated and see it in read-only mode of the column, you need to bind it (set DataMemberBinding).

    Regards,
    Maya
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  11. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 30 May 2013 Link to this post

    So here's the new code based on what you say and I'm still unable to change the value.  after selecting a new date with the picker icon, the value in the editor doesn't change

    <telerik:GridViewDataColumn Header="Date du compte" UniqueName="AccountDate" DataFormatString="{}{0:MMM-yyyy}" DataMemberBinding="{Binding AccountDate, Mode=TwoWay}">
        <telerik:GridViewDataColumn.CellEditTemplate>
            <DataTemplate>
                <telerik:RadDatePicker DateSelectionMode="Month" SelectedValue="{Binding AccountDate, Mode=TwoWay}" IsReadOnly="True"></telerik:RadDatePicker>
            </DataTemplate>
        </telerik:GridViewDataColumn.CellEditTemplate>
    </telerik:GridViewDataColumn>
  12. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 May 2013 Link to this post

    Hi Marc,

    Indeed, I missed something in your requirements - do you want to be able to select date from the drop down, but not to edit the TextBox ?  

    Regards,
    Maya
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  13. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 30 May 2013 Link to this post

    That's right
  14. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 May 2013 Link to this post

    Hi Marc,

    The way to go is to handle Loaded event, find the TextBox and set IsReadOnly only to it:

    <telerik:GridViewDataColumn Header="Date du compte" UniqueName="AccountDate" DataFormatString="{}{0:MMM-yyyy}"
                                                DataMemberBinding="{Binding Established, Mode=TwoWay}">
                        <telerik:GridViewDataColumn.CellEditTemplate>
                            <DataTemplate>
                                <telerik:RadDatePicker DateSelectionMode="Month" SelectedValue="{Binding Established, Mode=TwoWay}" Loaded="RadDatePicker_Loaded">
                                    </telerik:RadDatePicker>
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellEditTemplate>
                        </telerik:GridViewDataColumn>
     
    private void RadDatePicker_Loaded(object sender, RoutedEventArgs e)
            {
                var picker = sender as RadDateTimePicker;
                var textBox = picker.ChildrenOfType<TextBox>().FirstOrDefault();
                if (textBox != null)
                {
                    textBox.IsReadOnly = true;
                }
            }


    Regards,
    Maya
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  15. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 30 May 2013 Link to this post

    Thank you it works
Back to Top
DevCraft banner