如何更改ListView ViewCell ContextActions的BG颜色 [英] How to change BG color for ListView ViewCell ContextActions
问题描述
希望你做得很好,
我用三个上下文操作创建了ListView,在这里我想为每个MenuItem设置不同的背景颜色,如下所示 ViewCell:
I created ListView with three Context Actions, here I want to set different background color for each MenuItem like below ViewCell:
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PureSale.Core.Views.OrdersListTemplate">
<Grid Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackLayout Spacing="5">
<Label Text="{Binding Title}" FontAttributes="Bold" HorizontalOptions="StartAndExpand"/>
<Label Text="{Binding StartDate}" HorizontalOptions="StartAndExpand"/>
</StackLayout>
<Image Source="indicatorIconBlack.png" Grid.Column="1" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand"/>
</Grid>
</ViewCell>
public partial class OrdersListTemplate : ViewCell {
public OrdersListTemplate(){
InitializeComponent();
var deleteAction = new MenuItem { Text = "Delete", StyleId = "labelRedStyle" };
deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
deleteAction.Clicked += (sender, e) => {
};
var archiveAction = new MenuItem { Text = "Archive", IsDestructive = true};
archiveAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
archiveAction.Clicked += (sender, e) => {
};
var cancelAction = new MenuItem { Text = "Cancel" };
cancelAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
cancelAction.Clicked += (sender, e) => {
};
ContextActions.Add(cancelAction);
ContextActions.Add(archiveAction);
ContextActions.Add(deleteAction);
}
}
XAML
<ListView HasUnevenRows="true" ItemsSource="{Binding OrderItems}" ios:ListView.SeparatorStyle="FullWidth" SelectedItem="{Binding SelectedListItem}">
<ListView.ItemTemplate>
<DataTemplate>
<views:OrdersListTemplate/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
如何为菜单项设置StyledId,
How can I set StyledId for menu items,
请建议我,谢谢...
Please suggest me Thanks... in advance
推荐答案
您可以通过编写转换器来实现此目的,该转换器将获取类型并发送与该类型相关的颜色.
You can achieve this by writing a converter which will get the type and send the associated color with that type.
首先在您的对象中添加一个枚举
First add one enum in your object
public enum ActionType
{
Delete = 0,
Archive = 1,
Cancel = 2
};
添加一个根据您的操作类型返回颜色的转换器
Add one converter which returns the color on the basis of your action type
class FileIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
switch ((ActionType)value)
{
case ActionType.Delete:
return Color.Red;
case ActionType.Archive:
return Color.Yellow;
case ActionType.Canncel:
return Color.Gray;
}
}
return Color.Black;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
将此转换器条目添加到APP.xaml
Add this converter entry into to APP.xaml
<Convert:ActionTypeConverter x:Key="ActionTypeConverter"></Convert:ActionTypeConverter>
然后您可以将此转换器添加到您的xaml文件中
Then you can add this converter in your xaml file
<Label BackgroundColor="{Binding ActionType, Converter={StaticResource ActionTypeConverter}}"/>
它对我来说很完美,我希望这会起作用.
It works perfect for me, I hope this will work.
这篇关于如何更改ListView ViewCell ContextActions的BG颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!