CH3OH_ 2020-02-27 16:18 采纳率: 66.7%
浏览 614

Xamarin.formslistview的颜色问题

 var node_name2 = new ListView() { WidthRequest = 240 };
            var node_check2 = new ListView() { WidthRequest = 50 };
            var node_need2 = new ListView() { WidthRequest = 30 };
            var node_date2 = new ListView() { WidthRequest = 400 };
            var node_person2 = new ListView() { WidthRequest = 180 };
            var node_yj2 = new ListView() { WidthRequest = 300 };
            var stackLayout = new StackLayout()
            {
                Orientation = StackOrientation.Horizontal
            };
            stackLayout.Children.Add(node_name2);
            stackLayout.Children.Add(node_need2);
            stackLayout.Children.Add(node_check2);
            stackLayout.Children.Add(node_person2);
            stackLayout.Children.Add(node_date2);
            stackLayout.Children.Add(node_yj2);

利用代码创建了5个listview,但是在运行的时候,5个listview里的数据字体都是粉红色(在android模拟器里运行,iOS还没试过)
页面设计里设置颜色有一个textcolor的属性,如下:

<ListView x:Name="MyListView"
            ItemsSource="{Binding Items}"
            ItemTapped="Handle_ItemTapped"
            CachingStrategy="RecycleElement">
        <d:ListView.ItemsSource>
            <x:Array Type="{x:Type x:String}">
            <x:String>Item 1</x:String>
            <x:String>Item 2</x:String>
            <x:String>Item 3</x:String>
            <x:String>Item 4</x:String>
            <x:String>Item 5</x:String>
        </x:Array>
    </d:ListView.ItemsSource>
    <!--Built in Cells-->
    <ListView.ItemTemplate>
        <DataTemplate>
                <TextCell Text="{Binding .}" d:Text="{Binding .}" TextColor="#000000" IsContextActionsLegacyModeEnabled="True" Height="0" />
            </DataTemplate>
    </ListView.ItemTemplate>

但是在代码编写listview的时候却不知道怎么利用这个属性,求了解的大神指点。

  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-07-25 19:43
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    在代码中设置ListView的字体颜色可以通过创建一个DataTemplate来实现。可以按照以下步骤进行修改:
    1. 创建一个自定义的Cell类,继承自TextCell,覆盖Text属性,并添加一个TextColor属性:
    using Xamarin.Forms;
    public class CustomTextCell : TextCell
    {
        public static readonly BindableProperty TextColorProperty = BindableProperty.Create("TextColor", typeof(Color), typeof(CustomTextCell), Color.Black);
        public Color TextColor
        {
            get { return (Color)GetValue(TextColorProperty); }
            set { SetValue(TextColorProperty, value); }
        }
    }
    
    1. 在代码中使用这个自定义的Cell类,设置TextColor属性:
    var node_name2 = new ListView() { WidthRequest = 240 };
    var node_check2 = new ListView() { WidthRequest = 50 };
    var node_need2 = new ListView() { WidthRequest = 30 };
    var node_date2 = new ListView() { WidthRequest = 400 };
    var node_person2 = new ListView() { WidthRequest = 180 };
    var node_yj2 = new ListView() { WidthRequest = 300 };
    var stackLayout = new StackLayout()
    {
        Orientation = StackOrientation.Horizontal
    };
    stackLayout.Children.Add(node_name2);
    stackLayout.Children.Add(node_need2);
    stackLayout.Children.Add(node_check2);
    stackLayout.Children.Add(node_person2);
    stackLayout.Children.Add(node_date2);
    stackLayout.Children.Add(node_yj2);
    DataTemplate dataTemplate = new DataTemplate(() =>
    {
        CustomTextCell cell = new CustomTextCell();
        cell.SetBinding(TextCell.TextProperty, ".");
        cell.SetBinding(CustomTextCell.TextColorProperty, "TextColor");
        return cell;
    });
    node_name2.ItemTemplate = dataTemplate;
    node_check2.ItemTemplate = dataTemplate;
    node_need2.ItemTemplate = dataTemplate;
    node_date2.ItemTemplate = dataTemplate;
    node_person2.ItemTemplate = dataTemplate;
    node_yj2.ItemTemplate = dataTemplate;
    
    1. 在绑定数据时,为每个项设置TextColor属性:
    List<string> items = new List<string> { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
    node_name2.ItemsSource = items.Select(x => new { Text = x, TextColor = Color.Pink });
    node_check2.ItemsSource = items.Select(x => new { Text = x, TextColor = Color.Pink });
    node_need2.ItemsSource = items.Select(x => new { Text = x, TextColor = Color.Pink });
    node_date2.ItemsSource = items.Select(x => new { Text = x, TextColor = Color.Pink });
    node_person2.ItemsSource = items.Select(x => new { Text = x, TextColor = Color.Pink });
    node_yj2.ItemsSource = items.Select(x => new { Text = x, TextColor = Color.Pink });
    

    这样就可以在代码中动态设置ListView中项的字体颜色了。

    评论

报告相同问题?