表单ListView行索引

时间:2014-10-15 09:24:20

标签: listview xamarin xamarin.forms

我刚开始玩Xamarin.Forms,我对ListView有疑问,网上找不到答案。在下面的代码中,我需要在ItemTemplate中获取元素的索引或访问单元格中显示的ItemSource的方法,因为我需要构建显示的图像的路径。如果我可以同时拥有索引和元素,那就更好了。没有使用CustomRenderer的任何方式?

ListView listView = new ListView
{
    HasUnevenRows = true,

    // Source of data items.
    ItemsSource = DataManager.GetPeople(),

    ItemTemplate = new DataTemplate(() =>
    {

        // Create views with bindings for displaying each property.
        Label nameLabel = new Label();
        nameLabel.SetBinding(Label.TextProperty, "Name");
        nameLabel.TextColor = Color.White;

        var relativeLayout = new RelativeLayout {};    

        var webImage = new Image { Aspect = Aspect.AspectFill };
        image.HeightRequest = 200;
        image.Source = ImageSource.FromFile(Path.Combine(path, "/people/263/1.jpg"));
        relativeLayout.Children.Add(image,Constraint.Constant(0),Constraint.Constant(0));

        relativeLayout.Children.Add(nameLabel,
            Constraint.RelativeToParent((parent) => {
                return parent.Width / 2 - nameLabel.Width / 2;
            }),
            Constraint.RelativeToParent((parent) =>
            {
                return parent.Height - 20;
            }));

        // Return an assembled ViewCell.
        var viewCell = new ViewCell
        {
            View = relativeLayout
        };
        viewCell.Height = 200;
        relativeLayout.HeightRequest = 200;
        return viewCell;
    })
};

3 个答案:

答案 0 :(得分:3)

“元素”是ViewCell BindingContext

“index”是ItemsSource中元素的位置。如果您的ItemsSource设置为List<T>,则可以使用IndexOf()。如果它是真的IEnumerable()你需要更聪明一些。一种愚蠢的方式(对于短名单而言)可能是:

ItemsSource.ToList().IndexOf (BindingContext);

答案 1 :(得分:2)

这可以使用转换器完成。您可以从下面的示例中看到如何通过传递ListView作为引用来访问元素BindingContext(value)和索引。您可以使用转换器来构建图像路径并将其绑定到图像源。

public class ImageSourceIndexConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || parameter == null) return Color.White;
        var index = ((ListView) parameter).ItemsSource.Cast<object>().ToList().IndexOf(value);
        return ImageSource.FromFile(Path.Combine(path, "/people/"+index+"/1.jpg"));
    }

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

然后命名ListView并使用x:Reference

将其作为参数传递
<ListView x:Name="PeopleListView" ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}">
<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <Grid>
        <Image Source={Binding .,Converter={StaticResource StripedBackgroundIndexConverter}, ConverterParameter={x:Reference PeopleListView}}" />
      </Grid>
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>

答案 2 :(得分:0)

很棒的技巧。简短的回复不会让我分享代码。 我用你的技术制作了交替的半透明行。

public class AlternatingHighlightColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    Color rowcolor = Color.Transparent;
        if (value == null || parameter == null) return Color.White;
        var index = ((ListView)parameter).ItemsSource.Cast<object>().ToList().IndexOf(value);
        if (index % 2 == 0)
        {
            rowcolor = Color.FromHex("#55FFFFFF");
        }
        return rowcolor;
    }

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