访问 CollectionView 中的 object

Accessing the object in the CollectionView

<CollectionView Grid.Row="2" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                SelectionMode="None" ItemsSource="{Binding Order}" x:Name="lstOrders">
    <CollectionView.Header>
        <Label Text="Siparişler" TextColor="Black" FontSize="18"/>
    </CollectionView.Header>
    <CollectionView.ItemsLayout>
        <LinearItemsLayout Orientation="Vertical" ItemSpacing="20"/>
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <pv:PancakeView  BackgroundColor="White" VerticalOptions="StartAndExpand" 
                            HorizontalOptions="FillAndExpand">
                <Grid VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <BoxView BackgroundColor="Red" WidthRequest="3" HorizontalOptions="Start"
                             VerticalOptions="FillAndExpand"/>
                    <Expander Grid.Column="1">
                        <Expander.Header>
                            <Grid HorizontalOptions="FillAndExpand">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="3.5*"/>
                                </Grid.ColumnDefinitions>
                                <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
                                    <Label Text="{Binding OrderId}" TextColor="Black" FontSize="15"
                                           HorizontalOptions="Center"/>
                                </StackLayout>
                                <BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start" 
                                         VerticalOptions="FillAndExpand"/>
                                <StackLayout Grid.Column="2" HorizontalOptions="Start" VerticalOptions="Center" Margin="20">
                                    <Label Text="{Binding CompanyName}" TextColor="Black" FontSize="15"/>
                                    <Label Text="{Binding Sum}" TextColor="#2F3246" FontSize="12" Margin="0,-10,0,0"/>
                                </StackLayout>
                            </Grid>
                        </Expander.Header>
                        <Grid HorizontalOptions="FillAndExpand">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="3.5*"/>
                            </Grid.ColumnDefinitions>
                            
                            <BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start" 
                                     VerticalOptions="FillAndExpand"/>
                            <StackLayout Grid.Column="2" Spacing="10">
                                    <Label Text="Ürünler" TextColor="Black" Opacity="0.5" FontSize="12" Margin="20,0" x:Name="txtId"/>
                                    <StackLayout HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20">
                                        <Label TextColor="#2F3246" FontSize="12">
                                        <Label.FormattedText>
                                                <FormattedString>
                                                    <FormattedString.Spans>
                                                    <Span Text="{Binding Count}"/>
                                                    <Span Text=" - "/>
                                                    <Span Text="{Binding Product}" FontAttributes="Bold"/>
                                                </FormattedString.Spans>
                                            </FormattedString>
                                        </Label.FormattedText>
                                    </Label>
                                </StackLayout>
                                <StackLayout HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20">
                                    <Label TextColor="#2F3246" FontSize="12">
                                        <Label.FormattedText>
                                            <FormattedString>
                                                <FormattedString.Spans>
                                                    <Span Text="{Binding Count1}"/>
                                                    <Span Text=" - "/>
                                                    <Span Text="{Binding Product1}" FontAttributes="Bold"/>
                                                </FormattedString.Spans>
                                            </FormattedString>
                                        </Label.FormattedText>
                                    </Label>
                                </StackLayout>
                                <StackLayout HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20">
                                    <Label TextColor="#2F3246" FontSize="12">
                                        <Label.FormattedText>
                                            <FormattedString>
                                                <FormattedString.Spans>
                                                    <Span Text="{Binding Count2}"/>
                                                    <Span Text=" - "/>
                                                    <Span Text="{Binding Product2}" FontAttributes="Bold"/>
                                                </FormattedString.Spans>
                                            </FormattedString>
                                        </Label.FormattedText>
                                    </Label>
                                </StackLayout>
                                <StackLayout HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20">
                                    <Label TextColor="#2F3246" FontSize="12">
                                        <Label.FormattedText>
                                            <FormattedString>
                                                <FormattedString.Spans>
                                                    <Span Text="{Binding Count3}"/>
                                                    <Span Text=" - "/>
                                                    <Span Text="{Binding Product3}" FontAttributes="Bold"/>
                                                </FormattedString.Spans>
                                            </FormattedString>
                                        </Label.FormattedText>
                                    </Label>
                                </StackLayout>
                            </StackLayout>
                        </Grid>
                    </Expander>
                </Grid>
            </pv:PancakeView>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

我想在 collection 视图中隐藏 non-text 个地方,但不能在 collection 视图中使用 x: 名称。所以如果span部分没有数据可写,我想把我的span所在的stacklayout隐藏起来。另外,根据我的分配,我不允许使用 MVVM。如何访问收藏视图?你能帮帮我吗?

创建一个同时具有计数和产品的对象

public object[] CountProductObject { get; set; } = new object[] { Count,Product };

在 stacklayout 中绑定不可见并使用值转换器

<StackLayout HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20"
IsVisible={Binding CountProduct,Converter={StacticResource IsVisibleConverter}}>
                                        <Label TextColor="#2F3246" FontSize="12">
                                        <Label.FormattedText>
                                                <FormattedString>
                                                    <FormattedString.Spans>
                                                    <Span Text="{Binding Count}"/>
                                                    <Span Text=" - "/>
                                                    <Span Text="{Binding Product}" FontAttributes="Bold"/>
                                                </FormattedString.Spans>
                                            </FormattedString>
                                        </Label.FormattedText>
                                    </Label>
                                </StackLayout>

值转换器

public class IsVisibleConverter: IValueConverter
    {
        public object Convert(object value, Type targetType = null, object parameter = null, CultureInfo culture = null)
        {
            if (value == null)
                return false;
            else if(value is object[] objects)
{
// whatever is your logic
if(objects[0] != null && objects[1] != null)
return true;
}
                return ;
        }

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