我正在開發 Xamarin Forms 應用程式并嘗試在 Listview 中實作系結串列的功能。早些時候,我有一個包含“正常”型別的類,例如字串、長整數、整數等。
現在我還被要求添加串列(因此它是串列中的串列)并為其添加功能(特別是如果檢查給定的串列記錄,則應該獲取資訊的復選框,當然談論主串列中的嵌套專案)。
使用 BindableLayout,我實際上能夠將其設定為“只讀”,這意味著我現在可以看到每個串列項中的串列。問題是我無法將命令系結到該內部串列(我想這是因為現在的路徑不同)。
請記住,我已經使用分組來對這些專案進行分組。所以結構是這樣的:
按類中的屬性分組 -> ListView of Items -> 在每個 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、Commands 等獲取資料的方法。我唯一想念的是基本上處理這個嵌套項的 Command(同樣重要的是我需要同時獲取 PositionId 和 ItemText)。非常感謝任何幫助或建議,我現在為此苦苦掙扎了一段時間。
編輯:
With wonderful help given by both Gerald and CodingLumis I was finally able to understand what I was doing wrong, how should I bind it etc. Thank You very much!
uj5u.com熱心網友回復:
在得到 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;
}
非常感謝你們的幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/391101.html
標籤:c# .net xamarin xamarin.forms
