简体   繁体   English

获取刷过的项目并设置其 BindingContext 空白 ListView

[英]Getting the swiped item and setting its BindingContext blanks ListView

I have a listview which works perfectly fine, until I try to get the swiped item by its binding context and it makes the listview appear blank.我有一个运行良好的列表视图,直到我尝试通过其绑定上下文获取已刷过的项目并且它使列表视图显示为空白。 In code behind this allows me to access the item's data but when I run my app, it makes the listview blank...在这背后的代码中,我可以访问项目的数据,但是当我运行我的应用程序时,它会使列表视图变为空白......

Listview列表显示


<ListView
                        x:Name="MyList"
                        Grid.Row="1"
                        HasUnevenRows="True"
                        HeightRequest="10"
                        BackgroundColor="White"
                        IsGroupingEnabled="True"
                        IsPullToRefreshEnabled="false"
                        ItemsSource="{Binding LogItems}"
                        IsRefreshing="{Binding IsBusy, Mode=OneWay}"
                        RefreshCommand="{Binding LoadLogsCommand}"
                        SeparatorVisibility="None"                        
                        >
                        <ListView.ItemTemplate>
                            <DataTemplate >
                                <ViewCell Height="70">
                                    <StackLayout Orientation="Vertical"  VerticalOptions="Start"  >

                                        <SwipeView SwipeStarted="SwipeView_SwipeStarted" x:Name="mySwipeView" >                                            

                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="5"/>
                                                    <RowDefinition Height="36"/>
                                                    <RowDefinition Height="36"/>
                                                    <RowDefinition Height="60"/>                                                
                                                </Grid.RowDefinitions>

                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="100"/>
                                                    <ColumnDefinition Width="20"/>
                                                    <ColumnDefinition Width="110"/>
                                                    <ColumnDefinition Width="20"/>
                                                    <ColumnDefinition Width="100"/>
                                                    <ColumnDefinition Width="36"/>
                                                
                                                </Grid.ColumnDefinitions>
                                                
                                                <Label
                                                       Grid.Row="1"
                                                       Grid.Column="0"
                                                       VerticalOptions="Center"
                                                       FontAttributes="None"                                       
                                                       Text="{Binding  .Name}"
                                                       TextColor="Black"
                                                       Margin="0, 0,0, 0"
                                                       Padding="20,10,0,0"
                                                       FontFamily="Hiragino Sans"
                                                       FontSize="14"
                                                       HeightRequest="53"                                      
                                                  />
...
                                                </Grid>
                                            </Grid>
                                            <SwipeView.RightItems>
                                                <SwipeItems Mode="Reveal" SwipeBehaviorOnInvoked="Close" >

                                                    <SwipeItemView Invoked="OnDeleteSwipeItemInvoked" >
                                                        <StackLayout Orientation="Vertical" WidthRequest="200" BackgroundColor="Red">
                                                            <StackLayout HorizontalOptions="Start">
                                                                <Image Source="deleteIcon3.png" HeightRequest="25" WidthRequest="25" Margin="70,15,0,0" />
                                                                <Label Text=" Delete" TextColor="White" FontSize="15" HorizontalOptions="CenterAndExpand" Margin="70,0,0,0"/>
                                                            </StackLayout>
                                                        </StackLayout>
                                                    </SwipeItemView>
                                                   </SwipeItems>
                                            </SwipeView.RightItems>
                                        </SwipeView>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>                              
                        </ListView.ItemTemplate>

Code Behind代码背后


private void SwipeView_SwipeStarted(object sender, SwipeItemView e, SwipedEventArgs f, SwipeChangingEventArgs g) //SwipeStartedEventArgs e)
        {
            // var theItem = (myLogsModel)f.SelectedItem;
           var item = sender as SwipeItem;

           var model = item.BindingContext as myLogsModel;

var x = model.Name; ...

}


Please could anyone help me with how to get the swiped item without effecting my listview?请任何人都可以帮助我如何在不影响我的列表视图的情况下获取刷过的项目?

From Xamarin.Forms SwipeView , we can see that SwipeStarted is fired when a swipe starts.Xamarin.Forms SwipeView ,我们可以看到 SwipeStarted 在滑动开始时被触发。 The SwipeStartedEventArgs object that accompanies this event has a SwipeDirection property, of type SwipeDirection.伴随此事件的 SwipeStartedEventArgs object 具有 SwipeDirection 类型的 SwipeDirection 属性。

So you can get current listview item from OnDeleteSwipeItemInvoked method.因此,您可以从 OnDeleteSwipeItemInvoked 方法获取当前列表视图项。

I do one sample that you can take a look:我做了一个样本,你可以看看:

<ListView ItemsSource="{Binding items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <SwipeView>
                                <SwipeView.RightItems>
                                    <SwipeItems>
                                        <SwipeItemView Invoked="SwipeItemView_Invoked">
                                            <StackLayout
                                                BackgroundColor="Red"
                                                Orientation="Vertical"
                                                WidthRequest="200">
                                                <StackLayout HorizontalOptions="Start">
                                                    <Image
                                                        Margin="70,15,0,0"
                                                        HeightRequest="25"
                                                        Source="delete.png"
                                                        WidthRequest="25" />
                                                    <Label
                                                        Margin="70,0,0,0"
                                                        FontSize="15"
                                                        HorizontalOptions="CenterAndExpand"
                                                        Text=" Delete"
                                                        TextColor="White" />
                                                </StackLayout>
                                            </StackLayout>
                                        </SwipeItemView>
                                      
                                    </SwipeItems>
                                </SwipeView.RightItems>
                                <!--  Content  -->
                                <SwipeView.Content>
                                    <Grid
                                        BackgroundColor="LightGray"
                                        HeightRequest="60"
                                        WidthRequest="300">
                                        <Label
                                            HorizontalOptions="Center"
                                            Text="{Binding title}"
                                            VerticalOptions="Center" />
                                    </Grid>
                                </SwipeView.Content>

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

public partial class Page4 : ContentPage
{
    public ObservableCollection<menuitem> items { get; set; }
    public Page4()
    {
        InitializeComponent();
        items = new ObservableCollection<menuitem>()
        {
            new menuitem(){title="title 1"},
            new menuitem(){title="title 2"},
            new menuitem(){title="title 3"},
            new menuitem(){title="title 4"},
            new menuitem(){title="title 5"},
            new menuitem(){title="title 6"}

        };
        this.BindingContext = this;
    }

   
    private void SwipeItemView_Invoked(object sender, EventArgs e)
    {
        SwipeItemView item = sender as SwipeItemView;
        menuitem model = item.BindingContext as menuitem;
      
    }
}

public class menuitem
{
    public string title { get; set; }
}

As Jason said, we also suggest you can use ListView context menu to do, don't need to use SwipView.正如 Jason 所说,我们也建议你可以使用 ListView 上下文菜单来做,不需要使用 SwipView。 Select one item and long press to get context menu, like the following screenshot. Select 一项并长按以获取上下文菜单,如下图所示。

在此处输入图像描述

 <ListView ItemsSource="{Binding items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem Clicked="OnMore" Text="More" />
                            <MenuItem Clicked="OnDelete" Text="Delete" />
                        </ViewCell.ContextActions>
                        <StackLayout>
                            <Label Text="{Binding title}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

  private void OnMore(object sender, EventArgs e)
    {
        MenuItem item = sender as MenuItem;
        menuitem model = item.BindingContext as menuitem;
    }

About ListView interactivity, please take a look:关于 ListView 交互性,请看:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/interactivity https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/interactivity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM