BindableLayout list inside list 的命令

Commands of BindableLayout list inside list

我正在开发 Xamarin Forms 应用程序并尝试在 Listview 中实现绑定列表的功能。早些时候,我有 class 包含 'normal' 类型,例如字符串、长整型、整数等

现在我被要求添加列表(所以它是列表中的列表)并向其添加功能(特别是有复选框,如果给定的列表记录被选中,应该获取信息,当然谈论主内的嵌套项目列表)。

使用 BindableLayout,我能够真正做到 'read-only',这意味着我现在可以在每个列表项中看到列表。问题是我无法将命令绑定到该内部列表(我想那是因为现在路径不同)。

请记住,我已经使用分组来对这些项目进行分组。所以结构是这样的:

由 class 中的 属性 分组 -> Items 的 ListView -> 在每个 Item 记录中我有这个嵌套列表。

我不知道(如果可能的话)如何为这些内部项目设置路径。也许还有其他方法可以让它发挥作用。使用复选框从嵌套项目传递参数对我来说也很重要。

我的 XAML 看起来与此类似:

<ListView ItemsSource="{Binding Items}">
<...>
<ListView.ItemTemplate>
 <DataTemplate>
  <ViewCell>
   <ViewCell.View>
    <Label Text="{Binding ItemText}">
     <StackLayout BindableLayout.ItemsSource="{Binding Positions}"> 
      <BindableLayout.ItemTemplate>
       <DataTemplate>
        <Label Text="{Binding PositionId}">
        <CheckBox />
       </DataTemplate>
      </BindableLayout.ItemTemplate>
     </StackLayout>
    <ViewCell.View>
   <ViewCell>
  </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

物品型号:

public class Items
{
 public string ItemText {get; set;}
 public List<Positions> Positions {get; set;}
}

职位模型:

public class Positions
{
 public long? PositionId {get; set;}
}

创建此页面是通过像这样从上一页获取来完成的:

public override Page GivePage ()
    {
        ContentPage view = new ItemsView();
        var controller = new ItemsViewModel();
        view.BindingContext = controller;
        return view;
    }

在 ViewModel 中,我有从 API、命令等获取数据的方法。 我唯一想念的基本上是处理这个嵌套项目的命令(同样重要的是我需要同时获取 PositionId 和 ItemText)。 非常感谢任何帮助或建议,我已经为此苦苦挣扎了一段时间。

编辑:

在两位Gerald and CodingLumis的大力帮助下 我终于明白我做错了什么,我应该如何绑定它等等。非常感谢!

在得到 Gerald Versluis 和 CodingLumis 的帮助后,我能够解决我的问题并传递参数。我已经命名我的 ListView,例如“myList”,然后用复选框引用它。

我就是这样做的(根据 Gerald 的视频):

xmlns:behaviorsPack="clr-namespace:Xamarin.Forms.BehaviorsPack;assembly=Xamarin.Forms.BehaviorsPack"

<CheckBox>
 <CheckBox.Behaviors>
  <behaviorsPack:EventToCommandBehavior EventName="CheckedChanged"
   Command="{Binding Path=BindingContext.CheckedChangedCommand,
   Source={Reference myList}}"
   CommandParameter="{Binding .}"/>
 </CheckBox.Behaviors>
</Checkbox>

行为包仅用于将 CheckedChanged 事件处理为命令。

在我的 ViewModel 中,它看起来像:

// In class, added command
public ICommand CheckedChangedCommand { get; set; }    

// Then in ctor
CheckedChangedCommand = new Command<Positions>(CheckedChanged);

// and finally, method called by command
private async void CheckedChanged(object obj)
{
    var position = obj as Positions;
}

非常感谢你们的帮助!