Change TextColor and Background in DataTemplate

Can someone explain me how I can change the color of the labels MenuPoint and Subtitle in C#?

<ListView x:Name="MenuList" SelectedItem="{Binding SelectedMenu, Mode=TwoWay}" ItemsSource="{Binding MenuGrouped}" IsGroupingEnabled="True" GroupDisplayBinding="{Binding Key}" HasUnevenRows="true"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Grid x:Name ="GridMenu"> <Grid.Padding> <OnPlatform x:TypeArguments="Thickness"> <OnPlatform.iOS>10,5,10,5</OnPlatform.iOS> <OnPlatform.Android>10,5,10,5</OnPlatform.Android> <OnPlatform.WinPhone>0,0,0,25</OnPlatform.WinPhone> </OnPlatform> </Grid.Padding> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <controls:CircleImage Source="{Binding url}" Aspect="AspectFill"> <controls:CircleImage.VerticalOptions BorderColor="#3498DB" BorderThickness="2" Aspect="AspectFill"> </controls:CircleImage.VerticalOptions> <controls:CircleImage.WidthRequest> <OnPlatform x:TypeArguments="x:Double" iOS="55" Android="55" WinPhone="75"/> </controls:CircleImage.WidthRequest> <controls:CircleImage.HeightRequest> <OnPlatform x:TypeArguments="x:Double" iOS="55" Android="55" WinPhone="75"/> </controls:CircleImage.HeightRequest> </controls:CircleImage> <StackLayout Grid.Column="1" Padding="8" Spacing="4" VerticalOptions="Center"> <Label x:Name ="MenuPoint" Text="{Binding Title}" FontSize="Medium" TextColor="#3498DB" LineBreakMode="NoWrap"/> <Label x:Name ="Subtitle" Text="{Binding SubTitle}" FontSize="Small" LineBreakMode="WordWrap"/> </StackLayout> </Grid> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>

I cant call them in the constructor. Maybe because I use a ViewModel to load the menu.

Best Answer

  • Luis_dmLuis_dm US ✭✭✭
    edited December 2016 Accepted Answer

    Well, first I recommend that you use Application.Resource, this is in your App.xaml.

    Example for Grid.

        <Application.Resources>
            <ResourceDictionary>
    
     <Color x:Key="ColorNegro">#000000</Color>
    
        <!-- Padding -->
        <OnPlatform x:Key="PaddingPlataformas"
        x:TypeArguments="Thickness"
                          iOS="10,5,10,5"
                          Android="10,5,10,5"
                          WinPhone="0,0,0,25"
                          ></OnPlatform>
    
           <ResourceDictionary>
        </Application.Resources>
    

    In you xaml:

    <Grid x:Name ="GridMenu" Padding="{StaticResource PaddingPlataformas}">
            ...
            ...
            ..
            ...
            </Grid>
    

    Now, if you use two label like Title and Subtitle, i recommend you use "TextCell"

    TextCell in xaml:

    <TextCell Text="Title" Detail="Subtitle " TextColor="{StaticResource ColorNegro}" DetailColor="{StaticResource ColorNegro}"></TextCell>

    Label and listview color :

    In C#:

    MenuPoint.TextColor = Color.Black;
    Listview.BackgroundColor = Color.Black;
    

    In Xaml:

    <ListView BackgroundColor ="{StaticResource ColorNegro}"/>
    <Label TextColor="{StaticResource ColorNegro}">
    

Answers

  • Luis_dmLuis_dm USMember ✭✭✭
    edited December 2016 Accepted Answer

    Well, first I recommend that you use Application.Resource, this is in your App.xaml.

    Example for Grid.

        <Application.Resources>
            <ResourceDictionary>
    
     <Color x:Key="ColorNegro">#000000</Color>
    
        <!-- Padding -->
        <OnPlatform x:Key="PaddingPlataformas"
        x:TypeArguments="Thickness"
                          iOS="10,5,10,5"
                          Android="10,5,10,5"
                          WinPhone="0,0,0,25"
                          ></OnPlatform>
    
           <ResourceDictionary>
        </Application.Resources>
    

    In you xaml:

    <Grid x:Name ="GridMenu" Padding="{StaticResource PaddingPlataformas}">
            ...
            ...
            ..
            ...
            </Grid>
    

    Now, if you use two label like Title and Subtitle, i recommend you use "TextCell"

    TextCell in xaml:

    <TextCell Text="Title" Detail="Subtitle " TextColor="{StaticResource ColorNegro}" DetailColor="{StaticResource ColorNegro}"></TextCell>

    Label and listview color :

    In C#:

    MenuPoint.TextColor = Color.Black;
    Listview.BackgroundColor = Color.Black;
    

    In Xaml:

    <ListView BackgroundColor ="{StaticResource ColorNegro}"/>
    <Label TextColor="{StaticResource ColorNegro}">
    
Sign In or Register to comment.