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

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

13 Answers 381 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 01 Aug 2012, 08:19 PM
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.

13 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 02 Aug 2012, 06:49 AM
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.

0
Stephen
Top achievements
Rank 1
answered on 02 Aug 2012, 12:23 PM
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.
0
Marc Roussel
Top achievements
Rank 2
answered on 24 May 2013, 06:48 PM
How am I suppose to bind the CultureInfo to in order to have the DateTimeFormat.ShortDatePattern set ?
0
Marc Roussel
Top achievements
Rank 2
answered on 24 May 2013, 06:59 PM
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

0
Marc Roussel
Top achievements
Rank 2
answered on 24 May 2013, 07:07 PM
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
0
Maya
Telerik team
answered on 28 May 2013, 11:42 AM
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.

0
Marc Roussel
Top achievements
Rank 2
answered on 30 May 2013, 10:02 AM
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>
0
Maya
Telerik team
answered on 30 May 2013, 11:23 AM
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.

0
Marc Roussel
Top achievements
Rank 2
answered on 30 May 2013, 11:28 AM
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>
0
Maya
Telerik team
answered on 30 May 2013, 11:36 AM
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.

0
Marc Roussel
Top achievements
Rank 2
answered on 30 May 2013, 11:37 AM
That's right
0
Maya
Telerik team
answered on 30 May 2013, 11:47 AM
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.

0
Marc Roussel
Top achievements
Rank 2
answered on 30 May 2013, 12:09 PM
Thank you it works
Tags
GridView
Asked by
Stephen
Top achievements
Rank 1
Answers by
Maya
Telerik team
Stephen
Top achievements
Rank 1
Marc Roussel
Top achievements
Rank 2
Share this question
or