C# Xamarin列表视图中的绑定颜色
我以前检查过一些答案,但没有任何帮助。我正试图通过绑定StackLayout BackgroundColor来更改ListView中ViewCell的背景色。 现在看起来是这样的 每个单元格都应该填充不同的颜色,但它一点也不改变。背后的代码: OrderDatapage.XAML:C# Xamarin列表视图中的绑定颜色,c#,listview,xamarin,xamarin.forms,converter,C#,Listview,Xamarin,Xamarin.forms,Converter,我以前检查过一些答案,但没有任何帮助。我正试图通过绑定StackLayout BackgroundColor来更改ListView中ViewCell的背景色。 现在看起来是这样的 每个单元格都应该填充不同的颜色,但它一点也不改变。背后的代码: OrderDatapage.XAML: <ContentPage.Resources> <ResourceDictionary> <local:BackgroundConverter x:Key="Ba
<ContentPage.Resources>
<ResourceDictionary>
<local:BackgroundConverter x:Key="BackgroundConverter" />
</ResourceDictionary>
</ContentPage.Resources>
.
.
.
.
<StackLayout Orientation="Vertical">
<ListView x:Name="timetableList"
RowHeight="25"
SeparatorVisibility="Default"
Margin="0,0,0,10"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal"
VerticalOptions="FillAndExpand"
BackgroundColor="{Binding Paint, Converter={StaticResource BackgroundConverter}}">
<Label Text="{Binding Number}"
FontSize="Medium"
Margin="20,0,0,0"
TextColor="White"
BackgroundColor="Black"
/>
<Label Text="{Binding Title}"
FontSize="Default"
Margin="20,0,0,0"
TextColor="Black"
/>
<Label Text="{Binding Date}"
FontSize="Default"
HorizontalOptions="EndAndExpand"
Margin="0,0,40,0"
TextColor="Black"
/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
OrderDataPage.cs
List<TimetableItem> timeTableList { get; set; }
public OrderDataPage()
{
InitializeComponent();
timeTableList = new List<TimetableItem>();
var timetable1 = new TimetableItem() { Number = "1", Title = "Test1", Date = "20-12-2020", Paint = "#63FF20" };
var timetable2 = new TimetableItem() { Number = "2", Title = "Test2", Date = "20-12-2020", Paint = "#FFD933" };
var timetable3 = new TimetableItem() { Number = "3", Title = "Test3", Date = "20-12-2020", Paint = "#C0C0C0" };
timeTableList.Add(timetable1);
timeTableList.Add(timetable2);
timeTableList.Add(timetable3);
timetableList.ItemsSource = timeTableList;
}
List timeTableList{get;set;}
公共订单数据页()
{
初始化组件();
timeTableList=新列表();
var timetable1=new TimetableItem(){Number=“1”,Title=“Test1”,Date=“20-12-2020”,Paint=“#63FF20”};
var timetable2=new TimetableItem(){Number=“2”,Title=“Test2”,Date=“20-12-2020”,Paint=“#FFD933”};
var timetable3=new TimetableItem(){Number=“3”,Title=“Test3”,Date=“20-12-2020”,Paint=“#c0c0”};
timeTableList.Add(timetable1);
timeTableList.Add(timetable2);
时间表列表。添加(时间表3);
timetableList.ItemsSource=timetableList;
}
我认为您应该尝试将Label的BackGroundColor设置为“透明”以查看StackLayout背景色,但我不确定它是否有效。否则,可以将标签的背景颜色设置为特定颜色
在IValueConverter中,您应该检查“value”是否为null
if(value != null && value is string && (string)value != "")
return Color.FromHex(value.ToString());
else
return Color.Red;
更新
我对StackLayout的背景颜色进行了一些设置测试
slView.SetBinding(StackLayout.BackgroundColorProperty, "BackgroundColor");
其中BackgroundColor是一个字符串
List.Add(new Model { Description = "D1", Cost = 10.0, Qty = 1, BackgroundColor = "#9ac16e"});
List.Add(new Model { Description = "D2", Cost = 20.0, Qty = 2, BackgroundColor = "#8d0000" });
List.Add(new Model { Description = "D3", Cost = 30.0, Qty = 3, BackgroundColor = "#3a6cf6"});
而且它也可以在没有IValueConverter的情况下工作
唯一的问题是,当您选择行时,会松开并高亮显示
您可以找到一个repo您的代码看起来很好,您是否使用断点检查了Value convert是否被调用?将Label的BackGroundColor设置为“Transparent”对我来说是个好主意。非常感谢。
List.Add(new Model { Description = "D1", Cost = 10.0, Qty = 1, BackgroundColor = "#9ac16e"});
List.Add(new Model { Description = "D2", Cost = 20.0, Qty = 2, BackgroundColor = "#8d0000" });
List.Add(new Model { Description = "D3", Cost = 30.0, Qty = 3, BackgroundColor = "#3a6cf6"});