listview - 仅针对xamarin中的特定列表项在listview中隐藏行

标签 listview xamarin mvvm xamarin.forms

我的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的第一行的方法,但是没有成功。
有什么帮助吗?

最佳答案

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

首先,我在listview项目的不同行中设置TCPNumberAttachmentNameFileType。当文件类型值为2时,第一行的TCPNumber将被隐藏,如以下屏幕截图所示。 enter image description here

有我的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; }
}

希望它能对您有所帮助。

关于listview - 仅针对xamarin中的特定列表项在listview中隐藏行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028661/

相关文章:

android - 带有按钮的 ListView,但只有按钮被选中,而不是列表

azure - 将特定数据从 Azure 同步到 Xamarin(离线同步)

android - Xamarin Android 模拟器无法在 Visual Studio 2013 Ultimate 中完成启动

c# - 保存时使控制无效

WPF 绑定(bind)和资源查找复杂性

android - 渲染问题 渲染期间引发异常 : color and position arrays must be of equal length

android - 底部/顶部带有按钮的多字段 ListView 布局

java - 为什么测试时admob没有出现?我的代码有什么问题吗?

Azure 自定义视觉服务紧凑域

c# - 将窗口保持在顶部WPF