【问题标题】:How to set textcolor dynamically in label inside listview in xamarin forms如何在xamarin表单的listview内的标签中动态设置textcolor
【发布时间】:2020-11-03 14:51:08
【问题描述】:

如何在listview里面动态设置一个label的textcolor。需要根据用户的选择在整个app中设置label的文本颜色。我已经在 App.Xaml 中声明了样式。有一个类样式,在其中声明应用程序的颜色。目前从那里取颜色。但我需要为标签设置 2 种颜色并根据用户的选择选择颜色。

App.XAML

   <ResourceDictionary>
        <Style TargetType="Label" x:Key="label_style">
            <Setter Property="TextColor" Value="{x:Static local:Style.LabelLightColor}"></Setter>
        </Style>
    </ResourceDictionary>

样式类

 public class Style
{

    public static Color LabelLightColor = Color.Red;
 }

MainPage.xaml

 <Label Text="Welcome to Xamarin.Forms!"
         Style="{DynamicResource label_style}"/>

【问题讨论】:

  • 从样式声明中删除键,该样式将应用于应用程序中的所有标签,并从主页上的标签中删除样式
  • 你好,我已经更新了一个答案,你有时间可以看看。如果回复有帮助,请不要忘记接受它作为答案(点击该答案左上角的✔)并投票,它将帮助其他有类似问题的人。

标签: c# xamarin.forms themes


【解决方案1】:

如果您需要使用 Style 来实现,可以使用 Dynamic Styles in Xamarin.Forms

ContentPage.Resources 中创建样式:

<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>

标签的代码如下:

<Label Text="{Binding eMail}" Style="{DynamicResource myLabelStyle}" HorizontalOptions="CenterAndExpand"></Label>
<Label Text="{Binding eMail}" Style="{DynamicResource myLabelStyle}" HorizontalOptions="CenterAndExpand"></Label>

然后可以通过Button点击事件修改TextColor如下:

private void RedButton_Clicked(object sender, EventArgs e)
{
    Resources["myLabelStyle"] = Resources["labelRedStyle"];
}

private void GreenButton_Clicked(object sender, EventArgs e)
{
    Resources["myLabelStyle"] = Resources["labelGreenStyle"];
}

效果:

另外,您还可以在Xaml中为TextColor属性绑定一个颜色值,然后修改模型的数据可以动态改变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

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代码如下:

<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;
    }
}

效果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多