Search code examples
xamarinxamarin.formsxamarin.androidmonodevelopxamarin-studio

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>

I want to hide non-text places in collection view, but cannot use x: name in collection view. So if there is no data to write in the span part, I want to hide the stacklayout where my span is. Also, according to my assignment, I am not allowed to use MVVM. How can I access Collectionview? Can you please help me?


Solution

  • Create a object that has both count and product

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

    Bind that in stacklayout invisible and use value converter

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

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