在点击事件功能的Listview内的图像上的ListView中隐藏和显示网格

时间:2019-05-07 08:38:52

标签: xaml xamarin xamarin.forms

在列表视图内单击图像时,我想隐藏一个位于ListView.GroupHeaderTemplate中的网格。我在该网格上也进行了绑定-IsVisible =“ {Binding ShowGrid}”。我已经从https://stackoverflow.com/a/55274297/10223206的这篇文章中获得了参考。但是Struck,可见性并没有改变。我正在共享我的代码以更好地理解 查看模型-

  private bool _ShowGrid = false;
    public bool ShowGrid
    {
        get => _ShowGrid;
        set
        {
            _ShowGrid = value;
            RaisePropertyChanged();
        }
    }

在cs:_(图像拍头的图像名称-downarrow.png)

    private void TapGestureRecognizer_Tapped_1(object sender, EventArgs e)
    {
        try
        { 

            Image image = sender as Image;
            string source = image.Source as FileImageSource;  //Getting the name of source as string
            if (String.Equals(source, "downarrow.png"))
            {
                image.Source = "uparrow.png";
                viewModel.ShowGrid = false;
            }
            else
            {
                image.Source = "downarrow.png";
                viewModel.ShowGrid = true;

            }

        }
        catch(Exception ex)
        {
            var m = ex.Message;
        }


    }

Xaml-

  <ListView x:Name="MyListView"   IsGroupingEnabled="true" Footer=" "
                  HasUnevenRows="True" ItemSelected="MyListView_ItemSelected" 
                  SeparatorColor="Transparent" VerticalOptions="FillAndExpand"
                  SeparatorVisibility="None" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>

                            <Grid IsVisible="{Binding ShowGrid}" >


                                <Grid RowSpacing="0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="40" />
                                    </Grid.RowDefinitions>
                                <Image Grid.Column="0" Aspect="AspectFit"  HeightRequest="20" WidthRequest="30" 
                                                       HorizontalOptions="CenterAndExpand" 
                                                       VerticalOptions="CenterAndExpand" 
                                                       Source="checked.png" />
                                <Label Grid.Column="1" Text="{Binding SatusName}" FontSize="10"
                                                       HorizontalOptions="StartAndExpand"
                                                       VerticalOptions="CenterAndExpand" />
                                <Label Grid.Column="2" Text="{Binding Date}" FontSize="10"
                                                       HorizontalOptions="StartAndExpand"
                                                       VerticalOptions="CenterAndExpand" />
                                </Grid>

                            </Grid>


                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

            <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell>

                            <Grid RowSpacing="0" ColumnSpacing="0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition  Width="1*"/>
                                    <ColumnDefinition  Width="1*"/>
                                    <ColumnDefinition  Width="50"/>
                                </Grid.ColumnDefinitions>


                                <Label Grid.Row="0" Grid.Column="0"  VerticalOptions="CenterAndExpand"
                                       Text="{Binding personName}" TextColor="{StaticResource PrimaryBlue}" 
                                       FontSize="Medium" FontAttributes="Bold"/>


                                <Label Grid.Row="0" Grid.Column="1"  
                                       Text="{Binding Amount}"  VerticalOptions="CenterAndExpand"
                                       TextColor="{StaticResource PrimaryBlue}" 
                                       FontSize="Medium" />

                                <Image Source="downarrow.png" Grid.Row="0" Grid.Column="2" x:Name="ArrowImage"
                                       HeightRequest="20" WidthRequest="30"
                                       HorizontalOptions="EndAndExpand" VerticalOptions="StartAndExpand" >
                                    <Image.GestureRecognizers>
                                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1"/>
                                    </Image.GestureRecognizers>
                                </Image>
                            </Grid>

                    </ViewCell>
                </DataTemplate>
            </ListView.GroupHeaderTemplate>

但是什么都没用,它没有在IMAGE CLICK上隐藏Grid。请帮助我找到解决方案。

.cs

 grouped = new ObservableCollection<GroupedOrderModel>();
            var person1Group = new GroupedOrderModel() { personName = "Personal loan" ,Amount="100"};
            var person2Group = new GroupedOrderModel() { personName = "Car Loan",Amount="300" };
            var person3Group = new GroupedOrderModel() { personName = "Rent Loan",Amount="400" };


            person1Group.Add(new StaffLoanStatus() { SatusName = "Approved", Date = "23-01-2019" });
            person1Group.Add(new StaffLoanStatus() { SatusName = "Pending", Date = "20-01-2019" });
            person1Group.Add(new StaffLoanStatus() { SatusName = "Declined", Date = "19-01-2019" });


            person2Group.Add(new StaffLoanStatus() { SatusName = "Approved", Date = "23-01-2019" });
            person2Group.Add(new StaffLoanStatus() { SatusName = "Pending", Date = "20-01-2019" });


            grouped.Add(person1Group);
            grouped.Add(person2Group);

            //Person3 without OrderModel
            grouped.Add(person3Group);

            MyListView.ItemsSource = grouped;

enter image description here

1 个答案:

答案 0 :(得分:0)

BindingContext不能正常工作是因为ListViews ItemsSource是否将上下文绑定到DataTemplate尝试执行的操作之外的任何内容:

IsVisible="{Binding Path=BindingContext.ShowGrid, Source={x:Reference MyListView}}"