Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Showing different DataTemplates depending on bindings.

$
0
0

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}" />

Viewing all articles
Browse latest Browse all 89864

Trending Articles