在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"/>
                                </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,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,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,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,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>

我想在集合视图中隐藏非文本位置,但是不能在集合视图中使用x:name。因此,如果在span部分中没有要写入的数据,我想隐藏我span所在的stacklayout。另外,根据我的任务,我不允许使用MVVM。如何访问Collectionview?你能帮我吗?

解决方法

创建一个同时具有计数和乘积的对象

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

将其隐藏在堆栈布局中,并使用值转换器

<StackLayout HorizontalOptions="Start" VerticalOptions="Center" Margin="20,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>

ValueConverter

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;
        }
    }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...