Home > Back-end >  Label TextColor Binding in DataTamplate xaml Xamarin
Label TextColor Binding in DataTamplate xaml Xamarin

Time:10-18

I do have a CollectionView with a binding for the text in the labels. This works without a problem. But now I want to add a binding for the Text Color and I don't know how to do it.

my xaml:

<CollectionView ItemsSource="{Binding List}" SelectionMode="None">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <ScrollView>
                <Grid Padding="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding AttributFromListObject}"  TextColor="{Binding TextColor}" Grid.Row="0" Grid.Column="0"/>
                    <
                </Grid>
            </ScrollView>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

I have set the bindingcontext in the codebehind. In a normal label without the DataTemplate, the binding TextColor is working. How can I tell the xaml that it should use the bindingcontext of the page for the TextColor?

Thanks for your help!

CodePudding user response:

@Json comment had the answer. Needed to add the following

xmlns:ViewModels="clr-namespace:AppName.ViewModels"

TextColor="{Binding Source={RelativeSource AncestorType={x:Type ViewModels:MyViewModel}}, Path=TextColor}"
  • Related