Xamarin Forms Listview Datatemplate可见绑定不起作用吗?

时间:2020-07-22 18:40:03

标签: listview xamarin.forms

我有一个使用DataTemplate的ListView。在数据模板中,我有一些StackLayouts。 ListView ItemsSource设置为“类别”的列表(类别是我的类),并且这些StackLayouts之一的IsVisible属性绑定到Category.IsVisible。 ClassId也被绑定,因此在点击手势时,我找出哪个Category项目是ListView项目后面的项目,并将其设置为IsVisible。但这不起作用,并且StackLayout仍然可见

<ListView  
     SeparatorColor="#888888"
     SeparatorVisibility="Default"
     BackgroundColor="White"  
     SelectionMode="None"
     HasUnevenRows="True" 
     x:Name="mainListView">

     <ListView.Header>
         <ContentView 
                Padding="0,5">

                    <Label 
                        Margin="0,0,0,15"
                        FontSize="20" 
                        TextColor="#FF4081"            
                        Text="WINDOW CLEANING"/>
                    <!--TextColor="#FF4081"-->
                    
                </ContentView>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem 
                                Clicked="OnDelete" 
                                CommandParameter="{Binding .}" 
                                Text="Delete" 
                                IsDestructive="True" />
                        </ViewCell.ContextActions>
                        <StackLayout
                            ClassId="{Binding Id}"
                            Orientation="Vertical">
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer
                                    Tapped="Category_Tapped"
                                    NumberOfTapsRequired="1" />
                            </StackLayout.GestureRecognizers>
                            <StackLayout 
                                BackgroundColor="#e0e0e0"
                                Orientation="Horizontal"
                                VerticalOptions="FillAndExpand" 
                                Padding="5" 
                                Grid.Column="0">

                                <Image
                                    Source="{Binding ImageSource}" />

                                <Label 
                                    Margin="10,0,0,0"
                                    VerticalOptions="Center"
                                    HorizontalOptions="StartAndExpand"
                                    TextColor="#FF4081"
                                    FontSize="17"  
                                    Text="{Binding Name}" />

                                <Label 
                                    Margin="0,0,10,0"
                                    VerticalOptions="Center"
                                    HorizontalOptions="End"
                                    TextColor="DarkCyan"
                                    Font="Bold, 20" 
                                    Text="{Binding ShortedAmount}" />
                            </StackLayout>
                            <StackLayout 
                                Orientation="Horizontal"
                                IsVisible="{Binding IsVisible, Mode=OneWayToSource}">

                                <Label 
                                    HorizontalOptions="StartAndExpand"
                                    Margin="5,10,0,20"
                                    TextColor="DarkCyan"
                                    Font="Bold, 16"
                                    Text="{Binding Text}"/>

                                <Label 
                                    HorizontalTextAlignment="End"
                                    HorizontalOptions="End"
                                    Margin="0,10,10,20"
                                    TextColor="DarkCyan"
                                    Font="Bold, 16"
                                    Text="{Binding Amount}"/>
                                
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

代码段后面的代码:

    mainListView.ItemsSource = CategoryFactory.Categories;

    private void Category_Tapped(object sender, EventArgs e)
    {
        string id = ((StackLayout)sender).ClassId;
        id = id.Replace("CAT", "");
        int i = Convert.ToInt32(id);
        CategoryFactory.Categories[i].IsVisible = !CategoryFactory.Categories[i].IsVisible;
    }

我想你明白了。我想在运行时创建一个可扩展列表

1 个答案:

答案 0 :(得分:1)

您的模型/视图模型类必须实现INotifyPropertyChanged才能将属性更改通知给UI