【问题标题】:Hide rows in listview only for particular listitems in xamarin仅针对 xamarin 中的特定列表项隐藏列表视图中的行
【发布时间】:2019-03-07 16:54:31
【问题描述】:

我的 xaml 中有一个分组的 listView。

这是 listView 对象:

附件:

public string TCPNumber { get; set; }
public string AttachmentName { get; set; }
public int FileType { get; set; }

这些组是基于 FileType 值的 X 和 Y 名称。 每个列表项显示上述 3 项的值。 我想隐藏文件类型值为 2 的 Y 组列表项中的 TCPNumber 项。

下面是我的 xaml:

<ContentPage.Resources>
    <ResourceDictionary>
        <converter:TCPGridVisibleConverter x:Key="TCPGridVisible" />
    </ResourceDictionary>
<ContentPage.Resources>

<ListView x:Name="lvTCPs" HasUnevenRows="True"
<ListView.GroupHeaderTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding Key}"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.RowDefinitions>
<RowDefinition Height="{Binding Path=FileType, Source={x:Reference 
Name=AttachmentsTabPage}, Converter={StaticResource TCPGridVisible}}"> 
</RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*"></ColumnDefinition>
                                <ColumnDefinition Width="2*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
</ListView>

这是我试图隐藏 ViewCell 的第一行的方式,但没有成功。 有什么帮助吗?

【问题讨论】:

  • 如果你想隐藏一整行,我认为首先不将它包含在 ItemsSource 中会更容易
  • 我只想在 listitem 的 FileType 值为 2 时隐藏该行,否则我需要填充该行
  • 那么在构建 ItemsSource 时为什么不过滤掉 FileType=2?
  • 实际上我的要求不是删除列表项。我想删除/隐藏 fileType 值为 2 的每个列表项的第一行。如何使用 itemsource 过滤掉
  • 您确实需要编辑您的问题以澄清您的意思是 ViewCell Grid 的行,而不是 ListView 的行

标签: listview xamarin mvvm xamarin.forms


【解决方案1】:

我写了一个关于在文件类型值为 2 的 Y 组列表项中隐藏 TCPNumber 项的演示。

首先,我在列表视图项的不同行中设置了TCPNumber,AttachmentName,FileType。当 filetype 值为 2 时,第一行的TCPNumber 将被隐藏,如下图所示。

这是我的 xaml。

   <StackLayout>
    <ListView x:Name="lvTCPs" HasUnevenRows="True" >
                <ListView.GroupHeaderTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label Text="KEY"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.RowDefinitions>

                                <RowDefinition Height="{Binding FileType, Converter={local:TCPGridVisibleConverter} }"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="1*"></ColumnDefinition>
                                    <ColumnDefinition Width="2*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>


                        <Label Text="{Binding TCPNumber}" Grid.Row="0" Grid.Column="0" />
                        <Label Text="{Binding AttachmentName}" Grid.Row="1" Grid.Column="0" />
                        <Label Text="{Binding FileType}" Grid.Row="2" Grid.Column="0" />

                    </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>


</StackLayout>

这是我的TCPGridVisibleConverter

    public class TCPGridVisibleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (Equals(value, null))
            return new GridLength(0);

        var status = value.ToString();

        switch (status)
        {
            case ("2"):
                {
                    return new GridLength(0);
                }
            default:
                {

                    return new GridLength(1, GridUnitType.Auto);
                }
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

有我的数据。

        List<TCP> list = new List<TCP>();
        list.Add(new TCP("1001","abc",2));
        list.Add(new TCP("1002", "bca", 1));
        list.Add(new TCP("1003", "bca", 2));
        list.Add(new TCP("1004", "abc", 1));
        list.Add(new TCP("1005", "abc", 1));
        lvTCPs.ItemsSource = list;

这是你的模型。

    public class TCP
{
    public TCP(string TCPNumber, string AttachmentName, int FileType)
    {
        this.TCPNumber = TCPNumber;
        this.AttachmentName = AttachmentName;
        this.FileType = FileType;

    }
    public string TCPNumber { get; set; }
    public string AttachmentName { get; set; }
    public int FileType { get; set; }
}

希望对你有帮助。

【讨论】:

  • 是的,它有帮助。谢谢。
猜你喜欢
  • 2022-01-07
  • 1970-01-01
  • 2019-12-31
  • 2013-02-25
  • 2019-08-14
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多