C# 如何在xamarin表单的listview内部标签中动态设置textcolor
如何在listview中动态设置标签的文本颜色。需要根据用户的选择在整个应用程序中设置标签的文本颜色。我已经在App.Xaml中声明了样式。有一个类样式,在该类中声明应用程序的颜色。目前正在从那里获取颜色。但我需要为标签设置2种颜色,并根据用户的选择选择颜色 App.XAMLC# 如何在xamarin表单的listview内部标签中动态设置textcolor,c#,xamarin.forms,themes,C#,Xamarin.forms,Themes,如何在listview中动态设置标签的文本颜色。需要根据用户的选择在整个应用程序中设置标签的文本颜色。我已经在App.Xaml中声明了样式。有一个类样式,在该类中声明应用程序的颜色。目前正在从那里获取颜色。但我需要为标签设置2种颜色,并根据用户的选择选择颜色 App.XAML <ResourceDictionary> <Style TargetType="Label" x:Key="label_style">
<ResourceDictionary>
<Style TargetType="Label" x:Key="label_style">
<Setter Property="TextColor" Value="{x:Static local:Style.LabelLightColor}"></Setter>
</Style>
</ResourceDictionary>
MainPage.xaml
<Label Text="Welcome to Xamarin.Forms!"
Style="{DynamicResource label_style}"/>
如果您需要使用样式来实现这一点,那么有一个要做 在内容页中创建样式。参考资料:
<ContentPage.Resources>
<Style x:Key="labelGreenStyle" TargetType="Label">
<Setter Property="TextColor" Value="Green" />
</Style>
<Style x:Key="labelRedStyle" TargetType="Label">
<Setter Property="TextColor" Value="Red" />
</Style>
</ContentPage.Resources>
public class ContactViewModel
{
public List<Contacts> MyList { set; get; }
public ContactViewModel()
{
MyList = new List<Contacts>();
MyList.Add(new Contacts() { Address = "1", eMail = "1111@11.com", MyColor = Color.Red });
MyList.Add(new Contacts() { Address = "2", eMail = "2222@22.com" });
MyList.Add(new Contacts() { Address = "3", eMail = "3333@33.com" });
MyList.Add(new Contacts() { Address = "4", eMail = "4444@44.com" });
MyList.Add(new Contacts() { Address = "5", eMail = "5555@55.com" });
}
}
其效果是:
此外,您还可以在Xaml中为TextColor
属性绑定Color值,然后修改模型的数据可以动态更改TextColor
例如,Contacts.cs项如下所示:
public class Contacts: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Address { get; set; }
public string eMail { get; set; }
private Color myColor;
public Color MyColor
{
set
{
if (myColor != value)
{
myColor = value;
OnPropertyChanged("MyColor");
}
}
get
{
return myColor;
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
然后我们可以创建ContactViewModel.cs:
<ContentPage.Resources>
<Style x:Key="labelGreenStyle" TargetType="Label">
<Setter Property="TextColor" Value="Green" />
</Style>
<Style x:Key="labelRedStyle" TargetType="Label">
<Setter Property="TextColor" Value="Red" />
</Style>
</ContentPage.Resources>
public class ContactViewModel
{
public List<Contacts> MyList { set; get; }
public ContactViewModel()
{
MyList = new List<Contacts>();
MyList.Add(new Contacts() { Address = "1", eMail = "1111@11.com", MyColor = Color.Red });
MyList.Add(new Contacts() { Address = "2", eMail = "2222@22.com" });
MyList.Add(new Contacts() { Address = "3", eMail = "3333@33.com" });
MyList.Add(new Contacts() { Address = "4", eMail = "4444@44.com" });
MyList.Add(new Contacts() { Address = "5", eMail = "5555@55.com" });
}
}
其效果是:
从样式声明中删除该键,该样式将应用于应用程序中的所有标签,并从主页标签中删除样式嗨,我已更新了答案,您可以在有时间时查看。如果回复有帮助,请不要忘记接受它作为答案(单击✔ 在这个答案的左上角)投票,它将帮助其他有类似问题的人。
public class ContactViewModel
{
public List<Contacts> MyList { set; get; }
public ContactViewModel()
{
MyList = new List<Contacts>();
MyList.Add(new Contacts() { Address = "1", eMail = "1111@11.com", MyColor = Color.Red });
MyList.Add(new Contacts() { Address = "2", eMail = "2222@22.com" });
MyList.Add(new Contacts() { Address = "3", eMail = "3333@33.com" });
MyList.Add(new Contacts() { Address = "4", eMail = "4444@44.com" });
MyList.Add(new Contacts() { Address = "5", eMail = "5555@55.com" });
}
}
<ListView x:Name="MyListView"
ItemsSource="{Binding MyList}"
ItemSelected="MyListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal"
VerticalOptions="Center">
<Label Text="Hello World" TextColor="{Binding MyColor}"
HorizontalOptions="StartAndExpand"></Label>
<Label Text="{Binding Address}"
HorizontalOptions="CenterAndExpand"></Label>
<Label Text="{Binding eMail}"
HorizontalOptions="CenterAndExpand"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public partial class PageListView : ContentPage
{
public PageListView()
{
InitializeComponent();
BindingContext = new ContactViewModel();
}
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as Contacts;
item.MyColor = Color.Green;
}
}