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

changing templated textbox width at runtime

2 Answers 77 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
john
Top achievements
Rank 1
john asked on 18 Jan 2011, 07:49 PM

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 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 21 Jan 2011, 10:02 AM
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>>
0
IxReveal
Top achievements
Rank 1
answered on 21 Jan 2011, 02:59 PM
Thanks, I should have thought of that my bad
Tags
TreeView
Asked by
john
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
IxReveal
Top achievements
Rank 1
Share this question
or