C# 将数据模板中标签的文本颜色与资源关联
我在C# 将数据模板中标签的文本颜色与资源关联,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我在绑定中的堆栈布局中有列表的元素。在DataTemplate中,我有一个标签,带有灰色TextColor。按下按钮后,我希望TextColor的颜色变成我在应用程序中拥有的资源的颜色。我怎么办 c# 公共类Giorni { 公共字符串颜色{get;set;} } List listgiorni=新列表 { 新乔尼{}, } BindableLayout,SetItemsSource(StackLayout,listgiorni); XAML: <Application.Resou
绑定中的堆栈布局中有列表的元素。在DataTemplate
中,我有一个标签
,带有灰色TextColor
。按下按钮后,我希望TextColor
的颜色变成我在应用程序中拥有的资源的颜色。我怎么办
c#
公共类Giorni
{
公共字符串颜色{get;set;}
}
List listgiorni=新列表
{
新乔尼{},
}
BindableLayout,SetItemsSource(StackLayout,listgiorni);
XAML:
<Application.Resources>
<Color x:Key="AppPrimaryColor">#1976D2</Color>
</ApplicationResource>
#1976D2
如果我理解正确,您希望将Color
从Giorni
绑定到标签的TextColor
。
如果是这种情况,您需要在Giorni中实现INotifyPropertyChanged
,然后在XAML中添加绑定
下面是一个完整的示例应用程序,其中按钮可更改标签的颜色:
<?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>
更好的解决方案是将Giorni的Color属性从string
更改为Color
类型。
然后在OnButtonClicked
中,您可以只放置:
void OnButtonClicked(object sender, EventArgs args)
{
listgiorni[0].Color = (Color)Application.Current.Resources["AppPrimaryColor"];
}
在您的示例中,您将文本从灰色更改为红色。取而代之的是,我希望它最初是灰色的,然后通过按钮,它将具有整个项目的共享颜色资源的颜色您的共享颜色资源是什么,您能在您的问题中添加它吗?我更新了代码,很抱歉,我已经更新了答案。如果这对你有效,请接受答案。如果没有,请说明问题所在。
<?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";
}
}
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 );
}
void OnButtonClicked(object sender, EventArgs args)
{
listgiorni[0].Color = (Color)Application.Current.Resources["AppPrimaryColor"];
}