微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 BindableLayout 检索 ItemsSource

如何解决从 BindableLayout 检索 ItemsSource

我有一个带有 FlexLayoutBindableLayout

<FlexLayout BindableLayout.ItemsSource="{Binding CardItems}" x:Name="SourceLayout" Background="green"
                    Direction="Row" Wrap="Wrap">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <ContentView>
                            <Frame CornerRadius="20" Padding="0" WidthRequest="150" Margin="10"
                            HeightRequest="150"
                            BackgroundColor="{Binding .,Converter={StaticResource AlternateColorConverter},ConverterParameter={x:Reference SourceLayout}}">
                                <StackLayout>
                                    <StackLayout.GestureRecognizers>
                                        <TapGestureRecognizer Command="{Binding Path=BindingContext.CardItemNavCommand,Source={x:Reference SourceLayout}}"
                                                              CommandParameter="{Binding NavTarget}"/>
                                    </StackLayout.GestureRecognizers>
                                    <Label Text="{Binding Text}" TextColor="Black" FontSize="20" VerticalOptions="FillAndExpand"/>
                                    <Label Text="{Binding Text}" TextColor="Black" FontSize="20" VerticalOptions="FillAndExpand"/>
                                </StackLayout>
                            </Frame>
                        </ContentView>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </FlexLayout>

是否可以获取转换器内当前项目的索引,以便我可以相应地更改颜色?我知道这可以通过 ListView 实现,因为我可以访问 items 源属性,但无法从 BindableLayout 访问资源。

解决方法

是否可以获取转换器内当前项目的索引,以便我可以相应地更改颜色

BindableLayout 是一个静态类,因此我们无法从布局中获取它以获取 itemsSource。

对于这个函数,尝试在模型类中创建一个“标识符”属性并为背景颜色设置绑定。然后获取转换器类中的值,从数据集合中获取当前项的索引。根据索引指定背景颜色。

检查代码:

App.xaml.cs

public partial class App : Application
{
    public static TestPageViewModel viewModel;
    public App()
    {
        InitializeComponent();

        viewModel = new TestPageViewModel();

        MainPage = new NavigationPage(new TestPage());
    }
}

页面.xaml

<StackLayout BindableLayout.ItemsSource="{Binding DataCollection}" ...>
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Grid Padding="0,2,3,0" BackgroundColor="{Binding Identifier,Converter={StaticResource _converter}}">
                ...
            </Grid>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

页面.xaml.cs

public partial class TestPage : ContentPage
{
    public TestPage()
    {
        InitializeComponent();

        BindingContext = App.viewModel;
    }
}

模型类

public class TestPageModel
{
    public string Content { get; set; }

    public string Identifier { get; set; }
}

ValueConverter 类

public class TestPageValueConverter : IValueConverter
{
    public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
    {
        var index = GetValue(value);
        switch (index)
        {
            case 1:
                return Color.LightBlue;
                break;
            case 2:
                return Color.LightPink;
                break;
            case 3:
                return Color.LightYellow;
                break;

            default:
                break;
        }
        return Color.White;
    }

    public object ConvertBack(object value,CultureInfo culture)
    {
        return -1;
    }

    double GetValue(object value)
    {
        var viewModel = App.viewModel;
        foreach (var item in viewModel.DataCollection)
        {
            if (item.Identifier == (string)value)
            {
                return viewModel.DataCollection.IndexOf(item) + 1;
            }
        }
        return -1;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。