Showing different DataTemplates depending on bindings.

Jake_PorterJake_Porter USMember ✭✭
edited July 2 in Xamarin.Forms

In WPF I could use ContentControl => ContentTemplate =>Style TargetType => DataTemplate. And use triggers to bind to and get different controls depending on the data that is bound too. Is there a way to do this in Xamarin Forms.
Simple example

<DataTemplate x:Key="Details">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <StackPanel>
                    <Label
                        Margin="0"
                        Padding="0"
                        HorizontalAlignment="Left"
                        Background="Transparent"
                        BorderThickness="0"
                        Content="{Binding DataContext.Blah, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" />           
                </StackPanel>
                <ListBox
                    Grid.Row="1"
                    ItemsSource="{Binding DataContext.Stuff, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                    Visibility="{Binding DataContext.MoreStuff, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Converter=      {               StaticResource boolToVisibilityConv}}" />
            </Grid>
        </DataTemplate>
<DataTemplate x:Key="Options">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <StackPanel>
                    <Label
                        Margin="0"
                        Padding="0"
                        HorizontalAlignment="Left"
                        Background="Transparent"
                        BorderThickness="0"
                        Content="{Binding DataContext.Blah, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" />           
                </StackPanel>
                <ListBox
                    Grid.Row="1"
                    ItemsSource="{Binding DataContext.Stuff2, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                    Visibility="{Binding DataContext.MoreStuff2, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Converter=     {               StaticResource boolToVisibilityConv}}" />
            </Grid>
        </DataTemplate>
 <Style x:Key="ContainerStyle" TargetType="ContentControl">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Body}" Value="Options">
                    <Setter Property="ContentTemplate" Value="{DynamicResource Options}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Body}" Value="Details">
                    <Setter Property="ContentTemplate" Value="{DynamicResource Details}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
<ContentControl Style="{StaticResource ContainerStyle}" />
Tagged:

Best Answer

Answers

  • Jake_PorterJake_Porter USMember ✭✭
    edited July 3

    @LandLu I will try this today thank you so much.

Sign In or Register to comment.