Find Textbox in EditDataTemplate

7 posts, 0 answers
  1. ITA
    ITA avatar
    383 posts
    Member since:
    Aug 2009

    Posted 12 Mar 2013 Link to this post

    Hi,

    how do i find a textbox on "EditEnded"? I have a RadDataform with an EditTemplate. Now i use a textbox like this:
    <DataTemplate x:Key="MyEditTemplate">
       <Grid>
             <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition  Height="40"/>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
             </Grid.RowDefinitions>
             <telerik:DataFormDataField Label="{Binding Source={StaticResource Lang}, XPath=SettingsHost/@Header}" DataMemberBinding="{Binding Host, Mode=TwoWay}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />
                                            <telerik:DataFormDataField Label="{Binding Source={StaticResource Lang}, XPath=SettingsMandant/@Header}" DataMemberBinding="{Binding Mandant, Mode=TwoWay}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" />
              <telerik:DataFormDataField Label="{Binding Source={StaticResource Lang}, XPath=SettingsPort/@Header}"  DataMemberBinding="{Binding Port, Mode=TwoWay}" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" />                                      
              <telerik:DataFormDataField Label="{Binding Source={StaticResource Lang}, XPath=SettingsPasswort/@Header}"  DataMemberBinding="{Binding Passwort, Mode=TwoWay}" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" />                                                                                                           
            <TextBox x:Name=MyTextBox Foreground="red" />                                       
         </Grid>
    </DataTemplate>

    CodeBehind:

    if (e.EditAction == Telerik.Windows.Controls.Data.DataForm.EditAction.Commit)
    {
          Model.MandantObj NewMandant = VerbindungForm1.CurrentItem as Model.MandantObj;
         // Find Textbox?
         VerbindungForm1.MyTextbox ??? does not work...
    }

    Thanks
    Best Regards
    Rene

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 12 Mar 2013 Link to this post

    Hello Rene,

    You cannot find an element placed in DataTemplate through its name. What you can try instead is to work with ChildrenOfType<T>() extension method and find the element you want.   

    Greetings,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. ITA
    ITA avatar
    383 posts
    Member since:
    Aug 2009

    Posted 12 Mar 2013 Link to this post

    Hi Maya,

    thanks, but i still have a problem. I can find my Window, i can find the RadDataForm in the window, but
    not the Textbox. How is the best way to find my control?
    // Ok works fine
    Window w = Window.GetWindow(this);
    RadDataForm Radform = null;
    // Ok works fine
    Radform = Klassen.helper.FindChild<RadDataForm>(w, "VerbindungForm1");

    FindChild:
    public static T FindChild<T>(DependencyObject parent, string childName)
           where T : DependencyObject
           {
               // Confirm parent and childName are valid.
               if (parent == null) return null;
     
               T foundChild = null;
     
               int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
               for (int i = 0; i < childrenCount; i++)
               {
                   var child = VisualTreeHelper.GetChild(parent, i);
                   // If the child is not of the request child type child
                   T childType = child as T;
                   if (childType == null)
                   {
                       // recursively drill down the tree
                       foundChild = FindChild<T>(child, childName);
     
                       // If the child is found, break so we do not overwrite the found child.
                       if (foundChild != null) break;
                   }
                   else if (!string.IsNullOrEmpty(childName))
                   {
                       var frameworkElement = child as FrameworkElement;
                       // If the child's name is set for search
                       if (frameworkElement != null && frameworkElement.Name == childName)
                       {
                           // if the child's name is of the request name
                           foundChild = (T)child;
                           break;
                       }
                   }
                   else
                   {
                       // child element found.
                       foundChild = (T)child;
                       break;
                   }
               }
     
               return foundChild;
           }


    Thanks
    Regards
    Rene
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 12 Mar 2013 Link to this post

    Hi Rene,

    Please check out again the blog post I previously posted a link to. Using ChildrenOfType<T>() extension method, you can do something as follows: this.MyDataForm.ChildrenOfType<TextBox>().FirstOrDefault(t=>t.Name == "MyTextBox"); 

    Kind regards,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. ITA
    ITA avatar
    383 posts
    Member since:
    Aug 2009

    Posted 12 Mar 2013 Link to this post

    Hi Maya,

    i checked your link, but this does not work. My Textblock ist still null...

    thanks
    Regards
    Rene
  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 13 Mar 2013 Link to this post

    Hi Rene,

    Once I started creating a project demonstrating the case, I realized that you cannot get the TextBox in edit-ended event. At this time all the changes are committed and you do not have the edit template anymore. What you can try instead is to handle EditEnding event. For example:

    private void dataForm_EditEnding(object sender, Telerik.Windows.Controls.Data.DataForm.EditEndingEventArgs e)
        {
            if (e.EditAction == Telerik.Windows.Controls.Data.DataForm.EditAction.Commit)
            {
                var currentClub = this.dataForm.CurrentItem as Club;
                var textBox = this.dataForm.ChildrenOfType<TextBox>().FirstOrDefault(t => t.Name == "MyTextBox");
     
            }
        }

    Will that meet your requirements ?  

    All the best,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. ITA
    ITA avatar
    383 posts
    Member since:
    Aug 2009

    Posted 14 Mar 2013 Link to this post

    hi,

    thanks a lot, i solved the problem myself.

    thanks so far.
    best regards
    rene
Back to Top
UI for WPF is Visual Studio 2017 Ready