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"

                                          MinHeight="23">

                    <Grid.ColumnDefinitions>

                        <ColumnDefinition Width="*"/>

                        <ColumnDefinition Width="Auto"/>

                          <TextBox x:Name="ConceptDisplayTxtBlk"

                                                       Grid.Column="1"

                                                       HorizontalAlignment="Stretch"

                                                       Margin="0,3,0,0"

                                                       MinHeight="15"

                                                       Background="Transparent" BorderThickness="0"

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

                                                       Width="160"

                                                       TextWrapping="Wrap"

                                                       VerticalAlignment="Center"

                                                       GotFocus="ConceptEditTxtBx_GotFocus"

                                                       MouseLeftButtonDown="ConceptDisplayTextBlock_MouseLeftButtonDown"/>

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

     

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

     

  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 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"
     Loaded="ConceptDisplayTxtBlk_Loaded"
    ......
    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. DevCraft banner
  4. 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