Setting content dynamically

2 posts, 1 answers
  1. Mrn
    Mrn avatar
    29 posts
    Member since:
    Apr 2011

    Posted 07 Jun 2011 Link to this post

    Hello,

    Is it possible to set the content of an Expander in code behind? For example, a certain grid or stackpanel with its own children controls... something like this:

    <telerik:RadExpander Grid.Row="0" Header="{Binding Strings.Parameters, Source={StaticResource LocalizedStrings}}" Name="radExpanderParameters" Width="Auto" IsExpanded="True" >
                           <Grid Name="gridType" Grid.Row="0" Grid.Column="1" VerticalAlignment="Stretch" ShowGridLines="False">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="2*" />
                                     </Grid.RowDefinitions>
      
                                    <StackPanel Grid.Row="0" Margin="10" VerticalAlignment="Center">
                                        <RadioButton Margin="3" Content="Type 1" Height="16" GroupName="QueryType" x:Name="radioButton1"
                                                    IsChecked="{Binding QueryType, Mode=TwoWay, ConverterParameter=current, Converter={StaticResource RadioButtonConverter}}" />
                                        <RadioButton Margin="3" Content="Type 2" Height="16" GroupName="QueryType" Name="radioButton2"
                                                    IsChecked="{Binding QueryType, Mode=TwoWay, ConverterParameter=historical, Converter={StaticResource RadioButtonConverter}}" />
                                        <RadioButton Margin="3" Content="Type 3" Height="16" GroupName="QueryType" Name="radioButton3"
                                                    IsChecked="{Binding QueryType, Mode=TwoWay, ConverterParameter=prediction, Converter={StaticResource RadioButtonConverter}}" />
                                       </StackPanel>
      
                                    <StackPanel Grid.Row="1" Margin="10">
                                    </StackPanel>
      
                            </Grid>
                        </telerik:RadExpander>

    but I'd like to set the Grid in code behind instead.

    Thanks in advance!
  2. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 10 Jun 2011 Link to this post

    Hi Mrn,

    Evrything that is possible is XAML is possible in code behind. The opposite is not true.
    Your code from xaml should be similar to this C#:
    private void BuildExpanderOnTheFly()
    {
        RadExpander expander = new RadExpander();
        Grid grid = new Grid();
        grid.VerticalAlignment = VerticalAlignment.Stretch;
        grid.ShowGridLines = false;
        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) { } });
        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(2, GridUnitType.Star) { } });
        StackPanel panel = new StackPanel();
        RadRadioButton button = new RadRadioButton() { IsChecked = true, Content="Buuuuuton" };
        panel.Children.Add(button);
        grid.Children.Add(panel);
        expander.Content = grid;
        this.LayoutRoot.Children.Add(expander);
    }



    All the best,
    Petar Mladenov
    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
  3. DevCraft banner
Back to Top