How to access control inside a DataTemplate from codebehind

3 posts, 1 answers
  1. Dhanya
    Dhanya avatar
    3 posts
    Member since:
    Feb 2020

    Posted 13 Feb Link to this post

    Hi,

    I am using <telerik:GridViewDataColumn in which I have a DataTemplate. In this Datatemplate,I have my popup. I have written the below code in file say for example Sample.xaml. In the eventhandler 'PreviewTextInput' written in file Sample.xaml.cs, I need to set the 'BeadPlexNameInvalidPopup.IsOpen' property to true. Is there any way to access the popup in file Sample.xaml.cs?

    Any small help would be appreciated!

     

        <telerik:GridViewDataColumn x:Name="GridviewColumn" Header="{x:Static res:Resources.BeadPlexName}"
                                        DataMemberBinding="{Binding Name}"
                                        Width="*"
                                        EditTriggers="CellClick">
                                        <telerik:GridViewDataColumn.CellEditTemplate>
                                         <DataTemplate>
                                            <Grid x:Name="Grid1">
                                                <TextBox x:Name="BeadPlexNameTextBox" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" MaxLength="70"
                                                 PreviewTextInput="BeadPlexName_OnPreviewTextInput">
                                                </TextBox>
                                                    <Popup Name="BeadPlexNameInvalidPopup" >
                                                        <ContentControl Template="{StaticResource InvalidCharPopupTemplate}" >
                                                        </ContentControl>
                                                    </Popup>
                                                </Grid>
                                         </DataTemplate>
                                        </telerik:GridViewDataColumn.CellEditTemplate>
                                    </telerik:GridViewDataColumn>

  2. Answer
    Martin Ivanov
    Admin
    Martin Ivanov avatar
    2444 posts

    Posted 13 Feb Link to this post

    Hello Dhanya,

    To achieve your requirement, you can get the parent Grid of the TextBox that fires the PreviewTextInput and then, get the Popup from its children collection. The code should look something like this:

    private void BreadPlexNameTextBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
    {
    	var tb = (TextBox)sender;
    	var parentGrid = ParentOfTypeExtensions.ParentOfType<Grid>(tb);
    	var popup = parentGrid.Children.OfType<Popup>().First();
    }

    Regards,
    Martin Ivanov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Dhanya
    Dhanya avatar
    3 posts
    Member since:
    Feb 2020

    Posted 13 Feb in reply to Martin Ivanov Link to this post

    Hi Martin,

    It really worked!!! Tons of thanks!!! Especially for the quick response..

Back to Top