Reputation: 35
I have elements of a List
inside a StackLayout
in Binding
. Inside the DataTemplate
I have a Label
with Gray TextColor
. At the press of a button I would like the color of the TextColor
to become the color of a resource
I have within the application. How could I do?
c#
public class Giorni
{
public string Color {get;set;}
}
List<Giorni> listgiorni = new List<Giorni>
{
new Giorni{},
}
BindableLayout,SetItemsSource(StackLayout, listgiorni);
XAML:
<Application.Resources>
<Color x:Key="AppPrimaryColor">#1976D2</Color>
</ApplicationResource>
.....
<StackLayout>
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label TextColor="Gray" Text="Hello"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
Upvotes: 0
Views: 115
Reputation: 919
If I understood you correctly, you want to bind Color
from Giorni
to Label's TextColor
.
If that is the case, you need to implement INotifyPropertyChanged
in Giorni, and then add binding in XAML <Label TextColor="{Binding Color}" Text="Hello"/>
Here's the complete sample app with button changing the color of the label:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.MainPage">
<StackLayout>
<StackLayout x:Name="MyStackLayout">
<BindableLayout.ItemTemplate >
<DataTemplate>
<Label TextColor="{Binding Color}" Text="Hello"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
<Button Clicked="OnButtonClicked" Text="Change Color!"/>
</StackLayout>
</ContentPage>
public class Giorni:INotifyPropertyChanged
{
private string _color;
public string Color
{
get { return _color; }
set { _color = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public partial class MainPage : ContentPage
{
List<Giorni> listgiorni = new List<Giorni>
{
new Giorni{},
};
public MainPage()
{
InitializeComponent();
listgiorni[0].Color = "Gray";
BindableLayout.SetItemsSource(MyStackLayout, listgiorni);
}
void OnButtonClicked(object sender, EventArgs args)
{
listgiorni[0].Color = "Red";
}
}
EDIT: After clarification of resource
, here's the edited OnButtonClicked
.
Since your resource is Color
, you need to change it to string
appropriately, because Giorni's Color is string
.
void OnButtonClicked(object sender, EventArgs args)
{
Color color = (Color)Application.Current.Resources["AppPrimaryColor"];
int red = (int)(color.R * 255);
int green = (int)(color.G * 255);
int blue = (int)(color.B * 255);
listgiorni[0].Color = String.Format("#{0:X2}{1:X2}{2:X2}", red, green, blue );
}
A better solution would be to change your Giorni's Color property from string
to Color
type.
Then in OnButtonClicked
you can just put:
void OnButtonClicked(object sender, EventArgs args)
{
listgiorni[0].Color = (Color)Application.Current.Resources["AppPrimaryColor"];
}
Upvotes: 1