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

Find Textbox in EditDataTemplate

6 Answers 88 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
ITA
Top achievements
Rank 1
ITA asked on 12 Mar 2013, 01:00 PM
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

6 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 12 Mar 2013, 01:35 PM
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.

0
ITA
Top achievements
Rank 1
answered on 12 Mar 2013, 02:06 PM
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
0
Maya
Telerik team
answered on 12 Mar 2013, 02:29 PM
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.

0
ITA
Top achievements
Rank 1
answered on 12 Mar 2013, 02:40 PM
Hi Maya,

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

thanks
Regards
Rene
0
Maya
Telerik team
answered on 13 Mar 2013, 03:51 PM
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.

0
ITA
Top achievements
Rank 1
answered on 14 Mar 2013, 08:05 AM
hi,

thanks a lot, i solved the problem myself.

thanks so far.
best regards
rene
Tags
DataForm
Asked by
ITA
Top achievements
Rank 1
Answers by
Maya
Telerik team
ITA
Top achievements
Rank 1
Share this question
or