changing templated textbox width at runtime

3 posts, 0 answers
  1. john
    john avatar
    6 posts
    Member since:
    Jan 2011

    Posted 18 Jan 2011 Link to this post

    we have a RadTreeView whose rows are described with a template e.g checkbox,TextBox a, Textbox B  ...however we need to be able to set the width of textbox at runtime. I tried to do the following:

    private void ConceptBankTreeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)


    RadContextMenu ctx = new RadContextMenu();

    RadTreeViewItem RowToBeDisplayed = e.PreparedItem as RadTreeViewItem;

    TextBox tagsDisplayTxtBlk = (from aTxtBlk in RowToBeDisplayed.GetVisualDescendants().OfType<TextBox>()

    where aTxtBlk.Name == "ConceptDisplayTxtBlk" 

    select aTxtBlk).FirstOrDefault();

    if (tagsDisplayTxtBlk != null)

           tagsDisplayTxtBlk.Width = 567;


    with XAML that looks like:

    <telerik:HierarchicalDataTemplate x:Key="ParentRow"

                     ItemsSource="{Binding ChildConcepts}" ItemTemplate="{StaticResource ChildRow}">

                <Grid HorizontalAlignment="Stretch"



                        <ColumnDefinition Width="*"/>

                        <ColumnDefinition Width="Auto"/>

                          <TextBox x:Name="ConceptDisplayTxtBlk"





                                                       Background="Transparent" BorderThickness="0"

                                                       Text="{Binding Display, Mode=OneWay}"






                        <!--<TextBox x:Name="ConceptEditTxtBx"


    tagsDisplayTxtBlk is always null,  Now the same code works after a right click operation.


  2. Petar Mladenov
    Petar Mladenov avatar
    3152 posts

    Posted 21 Jan 2011 Link to this post

    Hello john,

    In the moment you are trying to access the TextBox in the template of the RadTreeViewItem, it hasn't been loaded yet. So the best thing to do is to use the loaded event handler of the TextBox like so:
    <TextBox x:Name="ConceptDisplayTxtBlk"
    private void ConceptDisplayTxtBlk_Loaded(object sender, RoutedEventArgs e)
               (sender as TextBox).Width = 567;
    Hope this will fit in your scenario. On the other hand (in other situations) instead GetVisualDescendants().OfType() you can use ChildrenOfType<>() method.

    Feel free to ask if you need more info . We would be glad to help you.

    Kind regards,
    Petar Mladenov
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. IxReveal
    IxReveal avatar
    7 posts
    Member since:
    Jan 2011

    Posted 21 Jan 2011 Link to this post

    Thanks, I should have thought of that my bad
Back to Top