【问题标题】:How to change BG color for ListView ViewCell ContextActions如何更改 ListView ViewCell ContextActions 的 BG 颜色
【发布时间】:2019-02-18 21:54:20
【问题描述】:

希望你做得很好,

我用三个上下文操作创建了 ListView,在这里我想为每个 MenuItem 设置不同的背景颜色,如下所示 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,

请提前给我建议谢谢...

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    您可以通过编写一个转换器来实现这一点,该转换器将获取类型并发送与该类型相关的颜色。

    首先在你的对象中添加一个枚举

    public enum ActionType
    {
        Delete = 0,
        Archive = 1,
        Cancel = 2
    };
    

    添加一个根据您的操作类型返回颜色的转换器

    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 中

    <Convert:ActionTypeConverter x:Key="ActionTypeConverter"></Convert:ActionTypeConverter>
    

    然后你可以在你的 xaml 文件中添加这个转换器

    <Label BackgroundColor="{Binding ActionType, Converter={StaticResource ActionTypeConverter}}"/>
    

    它非常适合我,我希望它会起作用。

    【讨论】:

    • 感谢您的回复,我想为 MenuItem 设置 BackgroundColor 而不是整个项目
    • 您可以将转换器添加到您的菜单项中
    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多