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

How-To create RadGridView.RowDetailsTemplate programatically

5 Answers 160 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard Harrigan
Top achievements
Rank 1
Richard Harrigan asked on 31 Dec 2011, 03:39 AM

Hi,

I would like to dynamically build a DataTemplate and am looking for help on how to do it.

I tried and failed with the following:

I loaded a working DataTemplates xaml into a string.

<DataTemplate>
    -----
    -----
    -----
</DataTemplate>

I executed the following and got this error
No default namespace has been declared Line 1 Pos 14

String xaml = GetXaml();         // A function that I wrote that created the string
DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);  // Failed on this line
myGrid.RowDetailsTemplate = dt;

Can you please set me straight on how to do this.

Thanks
Rich

5 Answers, 1 is accepted

Sort by
0
Richard Harrigan
Top achievements
Rank 1
answered on 31 Dec 2011, 06:43 PM
Hi,

I elimated the namespace error message by adding xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  to the DataTemplate tag.

I then crashed on a 4004.... See screen shot
0
Nick
Telerik team
answered on 02 Jan 2012, 04:00 PM
Hello Richard,

Based on the information that you provided there is no way to be certain about what is going wrong. However if you are using any other namespaces in the template you will probably have to add them too. I.e. if you are using telerik:RadButton, you have to add xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
to the DataTemplate tag. Does this work for you? if not, would it be possible to provide more detailed information about the project? 

All the best,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Richard Harrigan
Top achievements
Rank 1
answered on 03 Jan 2012, 12:36 AM

Hi Nik

The following is the original Xaml with  <telerik.RadGridView.RowDetailsTemplate> commented out. It is followed by the dynamic Xaml that was generated.  I loaded the DataTemplate as follows:

myGrid RowDetailsTemplate = (DataTemplate)XanlReader.Load(xaml);

I added all the namespaces from the original Xaml. Am I adding the dynamic xaml to the RadGridView properly?  I have no idea what to do next and am hoping that if you don't see something obvious you could create a very simple example of dynamically creating a RowDetailsTemplate.  The reason I need this is that I am trying to build a adhoc query builder where the end-user could specify how the grid should be rendered.

<UserControl x:Class="RadGridView_SL4_AR_25.MainPage"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:my="clr-namespace:RadGridView_SL4_AR_25"
             xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
             mc:Ignorable="d"
             d:DesignHeight="700"
             d:DesignWidth="700">
 
    <UserControl.Resources>
        <my:MyViewModel x:Key="MyViewModel" />
    </UserControl.Resources>
 
    <Grid x:Name="LayoutRoot"
          Background="White"
          DataContext="{StaticResource MyViewModel}">
         
         
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="350" />
        </Grid.ColumnDefinitions>
 
        <telerik:RadGridView Name="myGrid"
                             ItemsSource="{Binding GridItems, Mode=TwoWay}"
                             SelectionChanged="myGrid_SelectionChanged"
                             IsSynchronizedWithCurrentItem="True"
                             RowIndicatorVisibility="Visible"
                             RowDetailsVisibilityMode="VisibleWhenSelected"
                             CanUserSortColumns="True"
                             IsReadOnly="True"
                             ShowGroupPanel="True"
                             Loaded="myGrid_Loaded"
                             AutoGenerateColumns="False">
<!--
            <telerik:RadGridView.RowDetailsTemplate>        
                <DataTemplate>
                     
                    <Grid Width="Auto"
                          HorizontalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
 
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
 
                        <TextBlock Text="Name: "
                                   Margin="75,10,0,0"
                                   FontWeight="Bold"
                                   FontSize="14"
                                   Grid.Row="0"
                                   Grid.Column="0" />
                        <TextBlock Text="{Binding Columns[0]}"
                                   Margin="5,10,0,0"
                                   FontSize="14"
                                   Grid.Row="0"
                                   Grid.Column="1" />
                         
                        <TextBlock Text="Street: "
                                   Margin="75,0,0,0"
                                   FontWeight="Bold"
                                   FontSize="14"
                                   Grid.Row="1"
                                   Grid.Column="0" />
                        <TextBlock Text="{Binding Columns[1]}"
                                   Margin="5,0,0,0"
                                   FontSize="14"
                                   Grid.Row="1"
                                   Grid.Column="1" />
                        <TextBlock Text="City: "
                                   Margin="75,0,0,0"
                                   FontWeight="Bold"
                                   FontSize="14"
                                   Grid.Row="2"
                                   Grid.Column="0" />
                        <TextBlock Text="{Binding Columns[2]}"
                                   Margin="5,0,0,0"
                                   FontSize="14"
                                   Grid.Row="2"
                                   Grid.Column="1" />
                        <TextBlock Text="State: "
                                   Margin="75,0,0,0"
                                   FontWeight="Bold"
                                   FontSize="14"
                                   Grid.Row="3"
                                   Grid.Column="0" />
                        <TextBlock Text="{Binding Columns[3]}"
                                   Margin="5,0,0,0"
                                   FontSize="14"
                                   Grid.Row="3"
                                   Grid.Column="3" />
                        <TextBlock Text="Zip Code: "
                                   Margin="75,0,0,0"
                                   FontWeight="Bold"
                                   FontSize="14"
                                   Grid.Row="4"
                                   Grid.Column="0" />
                        <TextBlock Text="{Binding Columns[4]}"
                                   Margin="5,0,0,10"
                                   FontSize="14"
                                   Grid.Row="4"
                                   Grid.Column="1" />
                         
                    </Grid>
                </DataTemplate>
                 
            </telerik:RadGridView.RowDetailsTemplate>
            -->
            
        </telerik:RadGridView>
 
        <telerik:RadPropertyGrid x:Name="RadPropertyGrid1"
                                 Item="{Binding SelectedItem, ElementName=myGrid, Mode=TwoWay}"
                                 Margin="8"
                                 LabelColumnWidth="130"
                                 SearchBoxVisibility="Collapsed"
                                 Grid.Column="1"
                                 AutoGeneratePropertyDefinitions="False">
        </telerik:RadPropertyGrid>
    </Grid>
</UserControl>

Dynamically loaded Xaml:

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls" xmlns:my="clr-namespace:RadGridView_SL4_AR_25">
- <Grid Width="Auto" HorizontalAlignment="Stretch">
- <Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="Name:" Margin="75,10,0,0" FontWeight="Bold" FontSize="14" Grid.Row="0" Grid.Column="0" />
<TextBlock Text="{Binding Columns[0]}" Margin="5,10,0,0" FontSize="14" Grid.Row="0" Grid.Column="1" />
<TextBlock Text="Street:" Margin="75,10,0,0" FontWeight="Bold" FontSize="14" Grid.Row="1" Grid.Column="0" />
<TextBlock Text="{Binding Columns[1]}" Margin="5,10,0,0" FontSize="14" Grid.Row="1" Grid.Column="1" />
<TextBlock Text="City:" Margin="75,10,0,0" FontWeight="Bold" FontSize="14" Grid.Row="2" Grid.Column="0" />
<TextBlock Text="{Binding Columns[2]}" Margin="5,10,0,0" FontSize="14" Grid.Row="2" Grid.Column="1" />
<TextBlock Text="State:" Margin="75,10,0,0" FontWeight="Bold" FontSize="14" Grid.Row="3" Grid.Column="0" />
<TextBlock Text="{Binding Columns[3]}" Margin="5,10,0,0" FontSize="14" Grid.Row="3" Grid.Column="1" />
<TextBlock Text="Zip Code:" Margin="75,10,0,0" FontWeight="Bold" FontSize="14" Grid.Row="4" Grid.Column="0" />
<TextBlock Text="{Binding Columns[4]}" Margin="5,10,0,10" FontSize="14" Grid.Row="4" Grid.Column="1" />
</Grid>
</DataTemplate>
0
Nick
Telerik team
answered on 03 Jan 2012, 10:23 AM
Hi Richard,

I tried to reproduce the problem but I was actually unable to. Here is the code I used:

private void radGridView_Loaded(object sender, RoutedEventArgs e)
        {
            this.radGridView.RowDetailsTemplate = (DataTemplate)XamlReader.Load(
 
<Grid Width=""Auto"" HorizontalAlignment=""Stretch"">
 
<Grid.RowDefinitions>
 
<RowDefinition /> 
 
<RowDefinition /> 
 
<RowDefinition /> 
 
<RowDefinition /> 
 
<RowDefinition /> 
 
</Grid.RowDefinitions>
 
<Grid.ColumnDefinitions>
 
<ColumnDefinition Width=""Auto"" />
 
<ColumnDefinition Width=""Auto"" />
 
</Grid.ColumnDefinitions>
 
<TextBlock Text=""Name:"" Margin=""75,10,0,0"" FontWeight=""Bold"" FontSize=""14"" Grid.Row=""0"" Grid.Column=""0"" /> 
 
<TextBlock Text=""{Binding Columns[0]}"" Margin=""5,10,0,0"" FontSize=""14"" Grid.Row=""0"" Grid.Column=""1"" /> 
 
<TextBlock Text=""Street:"" Margin=""75,10,0,0"" FontWeight=""Bold"" FontSize=""14"" Grid.Row=""1"" Grid.Column=""0"" /> 
 
<TextBlock Text=""{Binding Columns[1]}"" Margin=""5,10,0,0"" FontSize=""14"" Grid.Row=""1"" Grid.Column=""1"" /> 
 
<TextBlock Text=""City:"" Margin=""75,10,0,0"" FontWeight=""Bold"" FontSize=""14"" Grid.Row=""2"" Grid.Column=""0"" /> 
 
<TextBlock Text=""{Binding Columns[2]}"" Margin=""5,10,0,0"" FontSize=""14"" Grid.Row=""2"" Grid.Column=""1"" /> 
 
<TextBlock Text=""State:"" Margin=""75,10,0,0"" FontWeight=""Bold"" FontSize=""14"" Grid.Row=""3"" Grid.Column=""0"" /> 
 
<TextBlock Text=""{Binding Columns[3]}"" Margin=""5,10,0,0"" FontSize=""14"" Grid.Row=""3"" Grid.Column=""1"" /> 
 
<TextBlock Text=""Zip Code:"" Margin=""75,10,0,0"" FontWeight=""Bold"" FontSize=""14"" Grid.Row=""4"" Grid.Column=""0"" /> 
 
<TextBlock Text=""{Binding Columns[4]}"" Margin=""5,10,0,10"" FontSize=""14"" Grid.Row=""4"" Grid.Column=""1"" /> 
 
</Grid>
 
</DataTemplate>");
        }

Are you sure that the formatting of the Xaml string you are trying to parse is correct? Could you try the snippet I sent you, and see if it works for you?
Furthermore, I believe you may find more information on creating templates programmatically, in the MSDN and in StackOverflow.

Hope this helps!

Greetings,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Richard Harrigan
Top achievements
Rank 1
answered on 03 Jan 2012, 06:45 PM
Hi Nik,

That worked, outstanding.

Thanks

Rich

Tags
GridView
Asked by
Richard Harrigan
Top achievements
Rank 1
Answers by
Richard Harrigan
Top achievements
Rank 1
Nick
Telerik team
Share this question
or