如何在carousel视图中设置listview所选项目为null

时间:2017-06-27 04:16:03

标签: xamarin xamarin.forms

我在轮播视图中有设置列表视图但是归档集列表视图如何选择项目为空,现在当我点击列表视图项目中的任何项目轮播视图时,它显示了默认颜色,但我们需要删除该颜色。请提出任何想法。提前谢谢。

示例代码:

 <cv:CarouselView VerticalOptions = "FillAndExpand" HorizontalOptions = "FillAndExpand" Position = "{Binding DishCategory}" ItemSelected = "OnSwipeDishesHandler" ItemsSource = "{Binding RestaurantDishesList}" x:Name = "RestaurantDishesList">
                    <cv:CarouselView.HeightRequest>
                        <OnIdiom x:TypeArguments ="x:Double" Phone ="230" Tablet ="630"/>
                    </cv:CarouselView.HeightRequest>                  
                    <cv:CarouselView.ItemTemplate>
                        <DataTemplate>
                            <StackLayout Padding = "10,5,10,5" Orientation = "Vertical">
                                <ListView x:Name="RestaurantMenuListView" BackgroundColor="Transparent" ItemsSource="{Binding CategoryWiseDishes}" HasUnevenRows="true" SeparatorColor="#eeeeee">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <ViewCell>
                                                <StackLayout Padding="7">
                                                    <Grid Padding="2">
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="*"/>
                                                            <ColumnDefinition Width="Auto"/>
                                                        </Grid.ColumnDefinitions>
                                                        <StackLayout Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                                                            <StackLayout.GestureRecognizers>
                                                                <TapGestureRecognizer Tapped="OnMenuItemSelected"/>
                                                            </StackLayout.GestureRecognizers>
                                                            <Label Text="{Binding Name}" TextColor="Black">
                                                                <Label.FontSize>
                                                                    <OnIdiom x:TypeArguments ="x:Double" Phone ="15" Tablet ="20"/>
                                                                </Label.FontSize>
                                                            </Label>
                                                            <Label Text="{Binding Description}" TextColor="#323232">
                                                                <Label.FontSize>
                                                                    <OnIdiom x:TypeArguments ="x:Double" Phone ="11" Tablet ="16"/>
                                                                </Label.FontSize>
                                                            </Label>
                                                            <StackLayout Orientation="Horizontal">
                                                                <Label Text="£" TextColor="Black">
                                                                    <Label.FontSize>
                                                                        <OnIdiom x:TypeArguments ="x:Double" Phone ="14" Tablet ="19"/>
                                                                    </Label.FontSize>
                                                                </Label>
                                                                <Label Text="{Binding Cost}" TextColor="Black">
                                                                    <Label.FontSize>
                                                                        <OnIdiom x:TypeArguments ="x:Double" Phone ="14" Tablet ="19"/>
                                                                    </Label.FontSize>
                                                                </Label>
                                                            </StackLayout>
                                                        </StackLayout>
                                                        <StackLayout Grid.Column="1" Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                                                            <Image Source="minus.png">
                                                                <Image.WidthRequest>
                                                                    <OnIdiom x:TypeArguments ="x:Double" Phone ="25" Tablet ="35"/>
                                                                </Image.WidthRequest>
                                                                <Image.HeightRequest>
                                                                    <OnIdiom x:TypeArguments ="x:Double" Phone ="25" Tablet ="35"/>
                                                                </Image.HeightRequest>
                                                                <Image.GestureRecognizers>
                                                                    <TapGestureRecognizer Tapped="RemoveItemBtnClicked"/>
                                                                </Image.GestureRecognizers>
                                                            </Image>
                                                          <Label Text="{Binding TotalQuantity}" HorizontalOptions="Center" VerticalOptions="Center" TextColor="Black">
                                                            <Label.FontSize>
                                                              <OnIdiom x:TypeArguments="x:Double">
                                                                <OnIdiom.Phone>
                                                                  <OnPlatform x:TypeArguments="x:Double" iOS="13" Android="13" WinPhone="13" />
                                                                </OnIdiom.Phone>
                                                                <OnIdiom.Tablet>
                                                                  <OnPlatform x:TypeArguments="x:Double" iOS="15" Android="15" WinPhone="15" />
                                                                </OnIdiom.Tablet>
                                                              </OnIdiom>
                                                            </Label.FontSize>
                                                          </Label>
                                                            <Image Source="add.png">
                                                                <Image.WidthRequest>
                                                                    <OnIdiom x:TypeArguments ="x:Double" Phone ="25" Tablet ="35"/>
                                                                </Image.WidthRequest>
                                                                <Image.HeightRequest>
                                                                    <OnIdiom x:TypeArguments ="x:Double" Phone ="25" Tablet ="35"/>
                                                                </Image.HeightRequest>
                                                                <Image.GestureRecognizers>
                                                                    <TapGestureRecognizer Tapped="AddItemBtnClicked"/>
                                                                </Image.GestureRecognizers>
                                                            </Image>
                                                        </StackLayout>
                                                    </Grid>
                                                </StackLayout>
                                            </ViewCell>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>
                            </StackLayout>
                        </DataTemplate>
                    </cv:CarouselView.ItemTemplate>
                </cv:CarouselView>    

当我点击总数量标签时,它会显示行中的默认颜色。需要删除该颜色。

2 个答案:

答案 0 :(得分:1)

在您的xaml文件中....

<ListView x:Name="RestaurantMenuListView" BackgroundColor="Transparent" ItemsSource="{Binding CategoryWiseDishes}" HasUnevenRows="true" SeparatorColor="#eeeeee" ItemTapped="OnItemTapped">

然后在你的xaml.cs文件....

public void OnItemTapped(object sender, ItemTappedEventArgs e)
    {
        ((ListView)sender).SelectedItem = null;
    }

答案 1 :(得分:0)

如果您使用MVVM而不是“隐藏代码”,请访问本教程。

您应该使用两个Bindings或转到本教程。

http://www.geojorgx.com/programacion/collection-view-and-the-horrible-itemselectednull/

public ICommand SelectionCommand => new Command(ItemSelected);

        private async void ItemSelected()

        {

            if (Selection != null)

            {

                var persondetaildata = Selection.Name;

                await Shell.Current.GoToAsync($"//DetailPage?persondetaildata={persondetaildata}");

                Selection = null;

            }

        }