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

run time changing the RadWord control

1 Answer 56 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 31 May 2011, 07:54 AM
Hello,

We want to be able to change the RadWord control at run time. I am sure this must be possible, but my snotty developer thinks its not possible. He tells me we are using RadWord as a control and we receive the structure for the new controls (textbox, combo box etc) during runtime and because of this we cannot add the extra controls as I would like to have happen.

The starting point is we want to add an extra row, which will be a permanent change to our version of the RadWord control as below
 <Grid.RowDefinitions>            
            <RowDefinition Height="Auto"/> //Ribbon
            <RowDefinition Height="200"/> //our row
            <RowDefinition Height="*"/> //RichTextBox
            <RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

Then at run time we want to populate the additional row. Please see the example in the attached picture where I have added 2 labels and 2 text blocks to show the author and the subject. Essentially, we want to add those labels and text box at run time. Is this possible please.

Regards
Steven

However, we want to add the control using C#

1 Answer, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 02 Jun 2011, 12:24 PM
Hi Steven,

You are right, it's possible to modify the structure of this control at runtime as you wish. The easier approach though is to modify first the Xaml of the the control (RadWord) and add this extra row wherever you like setting its Height to 0 and then at runtime you would need to set its height back to 200.

The other approach is a bit more complex. You can modify the grid structure entirely  in code. You need to use RowDefinitions.Insert method of the grid in order to do that.
I have prepared an example for you:

 - Here is the xaml part:
<Grid x:Name="LayoutRoot" Background="White" Height="500" Width="800" ShowGridLines="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition Height="50" />
        <RowDefinition Height="50" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
 
    <Button Grid.Row="0" Height="23" HorizontalAlignment="Left"
               Name="button1" Content="Button"  VerticalAlignment="Center" 
               Width="75" Click="button1_Click" />
    <TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Left"
               Name="textBlock1" Text="First Row"  VerticalAlignment="Top"/>
    <TextBlock Grid.Row="2" Height="23" HorizontalAlignment="Left"
               Name="textBlock2" Text="Second Row" VerticalAlignment="Top"/>
    <TextBlock Grid.Row="3" Height="23" HorizontalAlignment="Left"
               Name="textBlock3" Text="Third Row"  VerticalAlignment="Top"/>
</Grid>

- Here is the code behind part:
private void button1_Click(object sender, RoutedEventArgs e)
{
    // here we define on which index position we want to insert the new row
    int rowDefinitionIndex = 1;
    RowDefinition newRow = new RowDefinition()
    {
        Height = new GridLength(200)
    };
 
    LayoutRoot.RowDefinitions.Insert(rowDefinitionIndex, newRow);
 
    // in this loop we move the remaining grid rows with 1 index bellow.
    foreach (FrameworkElement element in LayoutRoot.Children.Where(c =>
        c is FrameworkElement))
    {
        // here we get the row index of the current element
        int rowIndex = Grid.GetRow(element);
        if (rowIndex >= rowDefinitionIndex)
        {
            // here we set the new row index of the current element to +1
            Grid.SetRow(element, rowIndex + 1);
        }
    }
 
    // new element is defined which will be inserted
    // in the new row that we already added
    TextBlock myTextBlock = new TextBlock();
    // the seconds are added to the text, so we can distinguish each new row.
    myTextBlock.Text = "Some text at here! Current seconds: " +
        DateTime.Now.Second.ToString();
    // here we set on which row this TextBlock will be added
    myTextBlock.SetValue(Grid.RowProperty, rowDefinitionIndex);
 
    // here we add the TextBlock to the childrens of the Grid.
    LayoutRoot.Children.Add(myTextBlock);
}

When you press the button, the new row is inserted at the row index "rowDefinitionIndex". In the example this variable is set to 1. Please note the count of the indexes is starting from 0.

I hope this helps.

Best wishes,
Mihail
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Steven
Top achievements
Rank 1
Answers by
Mihail
Telerik team
Share this question
or