tags:

views:

1107

answers:

2

I'm defining a datagrid's RowDetailsTemplate in the following way:

RowDetailsTemplate="{StaticResource defaultTemplate}"

where

<UserControl.Resources>
    <DataTemplate x:Key="defaultTemplate">
        <StackPanel>
            <TextBlock Text="default" x:Name="_txt" />
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="otherTemplate">
        <StackPanel>
            <TextBlock Text="other" x:Name="_txt" />
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

Is there a way to programatically define which of the two above DataTemplates a given row is to use (perhaps in the LoadingRowDetails() event)?

+1  A: 

You can add the following code in your LoadingRowDetails event, obviously replacing my useless If condition with your own:

    If 1 = 1 Then
        e.Row.DetailsTemplate = CType(Resources("defaultTemplate"), DataTemplate)
    Else
        e.Row.DetailsTemplate = CType(Resources("otherTemplate"), DataTemplate)
    End If
Tom
A: 

First: Thanks a lot Tom. It saved my day (week/month) :-)

And in C#:


if (1 == 1)
{
   e.Row.DetailsTemplate = (DataTemplate) Resources["defaultTemplate"];
}
else
{
   e.Row.DetailsTemplate = (DataTemplate) Resources["otherTemplate"];
}

To add more power to this solution the following msdn link shows how to create controls at runtime.

Klinger