如何使列表视图具有交替的背景颜色? Net Maui

发布于 2025-02-04 04:35:01 字数 51 浏览 3 评论 0原文

我正在尝试创建一个列表视图,背景颜色在列表中的每个条目都交替。有没有办法在毛伊岛这样做?

I am trying to create a list view where the background color alternates for each entry in the list. Is there a way to do this in MAUI?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑾夏年华 2025-02-11 04:35:02

为了解决这个问题,我在视图模型中添加了亚麻属属性,并使用了一个值转换器来设置颜色。

值转换器:

public class NumberToAlternatingColorValueConverter : IValueConverter
{
    public Color EvenNumberColor { get; init; } = Colors.Green;

    public Color OddNumberColor { get; init; } = Colors.Red;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is not int intValue
            ? Colors.Transparent
            : (intValue % 2) == 0
                ? EvenNumberColor
                : OddNumberColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException("NumberToAlternatingColorValueConverter.ConvertBack");
    }
}

注册值转换器

我在我的主资源词典中注册了值转换器,因此

<converter:NumberToAlternatingColorValueConverter
    x:Key="MagentaCyanRowColorConverter"
    EvenNumberColor="Magenta"
    OddNumberColor="Cyan" />

可以使用不同颜色的其他键注册一个以上的值转换器组合。

使用值转换器

,我在XAML中使用了绑定的值转换器,例如:

<StackLayout BindableLayout.ItemsSource="{Binding Summary.Lines}">
    <BindableLayout.ItemTemplate>
        <DataTemplate x:DataType="vm:SummaryLineViewModel">
            <Grid BackgroundColor="{Binding LineNumber, Converter={StaticResource MagentaCyanRowColorConverter}}">
                <Label Grid.Column="0" Text="{Binding Name}" />
                <Label Grid.Column="1" Text="{Binding Value}" />
            </Grid>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

To solve this problem I added a LineNumber property to my view model and used a value converter to set the color.

The value converter:

public class NumberToAlternatingColorValueConverter : IValueConverter
{
    public Color EvenNumberColor { get; init; } = Colors.Green;

    public Color OddNumberColor { get; init; } = Colors.Red;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is not int intValue
            ? Colors.Transparent
            : (intValue % 2) == 0
                ? EvenNumberColor
                : OddNumberColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException("NumberToAlternatingColorValueConverter.ConvertBack");
    }
}

Register the value converter

I registered the value converter in my main resource dictionary like so:

<converter:NumberToAlternatingColorValueConverter
    x:Key="MagentaCyanRowColorConverter"
    EvenNumberColor="Magenta"
    OddNumberColor="Cyan" />

You can register more than one value converter with a different key for different colour combinations.

Use the value converter

And I used the bound value converter in my XAML like so:

<StackLayout BindableLayout.ItemsSource="{Binding Summary.Lines}">
    <BindableLayout.ItemTemplate>
        <DataTemplate x:DataType="vm:SummaryLineViewModel">
            <Grid BackgroundColor="{Binding LineNumber, Converter={StaticResource MagentaCyanRowColorConverter}}">
                <Label Grid.Column="0" Text="{Binding Name}" />
                <Label Grid.Column="1" Text="{Binding Value}" />
            </Grid>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>
岁月苍老的讽刺 2025-02-11 04:35:02

这可以通过多种方式完成。通常建议的方法是通过a DatateMplatesElector

1.创建A DataTemplatesElector保存两个模板并根据项目的索引选择它们:

public class AlternateColorDataTemplateSelector: DataTemplateSelector
{
    public DataTemplate EvenTemplate { get; set; }
    public DataTemplate UnevenTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        // TODO: cast `Monkey` to your Item 
        return ((List<Monkey>)((ListView)container).ItemsSource).IndexOf(item as Monkey) % 2 == 0 ? EvenTemplate : UnevenTemplate;
    }
}

2。在XAML中,我们可以定义两个模板,一个具有替代颜色,一个具有正常颜色。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp0606.MainPage"
             xmlns:local="clr-namespace:MauiApp0606"
             >

    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="evenTemplate">
                <ViewCell>
                    <Grid BackgroundColor="White">
                        <Label Text="{Binding Name}" HorizontalOptions="Center" VerticalOptions="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="unevenTemplate">
                <ViewCell>
                    <Grid BackgroundColor="LightGray">
                        <Label Text="{Binding Name}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <local:AlternateColorDataTemplateSelector x:Key="alternateColorDataTemplateSelector"
                EvenTemplate="{StaticResource evenTemplate}"
                UnevenTemplate="{StaticResource unevenTemplate}" />
            
        </ResourceDictionary>
    </ContentPage.Resources>
 
        <VerticalStackLayout Spacing="25" Padding="30">

            <ListView ItemsSource="{Binding Monkeys}" ItemTemplate="{StaticResource alternateColorDataTemplateSelector}">

            </ListView>

        </VerticalStackLayout>
</ContentPage>

This can be done in multiple ways.And the method often recommended is through a DataTemplateSelector.

1.Create a DataTemplateSelector that holds two templates and selects them based on the index of the item:

public class AlternateColorDataTemplateSelector: DataTemplateSelector
{
    public DataTemplate EvenTemplate { get; set; }
    public DataTemplate UnevenTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        // TODO: cast `Monkey` to your Item 
        return ((List<Monkey>)((ListView)container).ItemsSource).IndexOf(item as Monkey) % 2 == 0 ? EvenTemplate : UnevenTemplate;
    }
}

2.In XAML, we can define two templates, one with the alternate color and one with the normal color.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp0606.MainPage"
             xmlns:local="clr-namespace:MauiApp0606"
             >

    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="evenTemplate">
                <ViewCell>
                    <Grid BackgroundColor="White">
                        <Label Text="{Binding Name}" HorizontalOptions="Center" VerticalOptions="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="unevenTemplate">
                <ViewCell>
                    <Grid BackgroundColor="LightGray">
                        <Label Text="{Binding Name}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <local:AlternateColorDataTemplateSelector x:Key="alternateColorDataTemplateSelector"
                EvenTemplate="{StaticResource evenTemplate}"
                UnevenTemplate="{StaticResource unevenTemplate}" />
            
        </ResourceDictionary>
    </ContentPage.Resources>
 
        <VerticalStackLayout Spacing="25" Padding="30">

            <ListView ItemsSource="{Binding Monkeys}" ItemTemplate="{StaticResource alternateColorDataTemplateSelector}">

            </ListView>

        </VerticalStackLayout>
</ContentPage>
爱给你人给你 2025-02-11 04:35:02

这个答案我们可以使用 datateMplatesElector 解决您的问题。

替代解决方案,

  1. 您可以在模型中使用属性,然后将其直接绑定到行背景。 (在您的情况下网格的背景颜色)


  2. 您可以使用触发器通过考虑逻辑来改变颜色。
    Xamarin> Xamarin form >

As in this answer we can used DataTemplateSelector to resolve your problem.

Alternative solutions,

  1. You can use a property in the model and straight a way bind it to the row background. (In your case Grid's Background color)

  2. You can use triggers make the color change by considering a logic.
    ( Xamarin Forms Triggers )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文