在我的 Xamarin 應用程式中ListView,ItemTemplate我必須在其中顯示一個包含 3 列的純文本串列。
第一個想法是使用 a GroupedListView,但在這種情況下,所有子條目都可以單獨選擇。那不是我想要的。該ListView專案應顯示為一個可選元素。
我在研究中發現的第二個想法是Gridview按代碼添加行,但這會破壞我的 MVVM 概念。我需要一個僅適用于資料系結的解決方案。
我有時讀到的第三件事是:ListView在ListView. 但這種想法的答案大多是:“永遠不要這樣做”。
任何其他想法我可以為此做些什么?我想要的是這樣的:
ListView Entry 1
04:13 Jhonny 3,24$
09:45 Some Long Nam... 8,23$
14:42 Mike 5,45$
----------------------------------------
ListView Entry 2
07:13 Jhonny 3,24$
11:22 Some Long Nam... 8,23$
18:42 Mike 5,45$
----------------------------------------
ListView Entry 3
05:13 Jhonny 3,24$
15:45 Some Long Nam... 8,23$
19:42 Mike 5,45$
----------------------------------------
Listview始終應將整個條目作為一個元素進行選擇。
uj5u.com熱心網友回復:
猜猜你可以使用 BindableLayout。你沒有指定型別,所以我做了一個虛擬型別。看法:
<CollectionView ItemsSource="{Binding GroupItems}" SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Vertical" Margin="5">
<Label Text="{Binding Title}"/>
<StackLayout BindableLayout.ItemsSource="{Binding Entries}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding Time}" HorizontalTextAlignment="Center"/>
<Label Text="{Binding Name}" Grid.Column="1" LineBreakMode="TailTruncation"/>
<Label Text="{Binding Amount}" Grid.Column="2" HorizontalTextAlignment="Center"/>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
代碼隱藏:
public ObservableCollection<GroupItem> GroupItems { get; set; } = new ObservableCollection<GroupItem>();
public MainPage()
{
InitializeComponent();
BindingContext = this;
GroupItem item1 = new GroupItem();
item1.Title = "Entry 1";
item1.Entries.Add(new Entry { Time = "10:13", Name = "Johnny", Amount = "10,24$" });
item1.Entries.Add(new Entry { Time = "12:11", Name = "Some long name that will be truncated", Amount = "20,14$" });
item1.Entries.Add(new Entry { Time = "10:13", Name = "Mike", Amount = "14,27$" });
GroupItems.Add(item1);
GroupItem item2 = new GroupItem();
item2.Title = "Entry 2";
item2.Entries.Add(new Entry { Time = "11:13", Name = "Finn", Amount = "10,24$" });
item2.Entries.Add(new Entry { Time = "14:11", Name = "Some long name that will be truncated", Amount = "20,14$" });
GroupItems.Add(item2);
GroupItem item3 = new GroupItem();
item3.Title = "Entry 3";
item3.Entries.Add(new Entry { Time = "11:13", Name = "Finn", Amount = "10,24$" });
item3.Entries.Add(new Entry { Time = "14:11", Name = "Some long name that will be truncated", Amount = "20,14$" });
item3.Entries.Add(new Entry { Time = "14:11", Name = "Martin", Amount = "50,15$" });
item3.Entries.Add(new Entry { Time = "14:11", Name = "Elon musk", Amount = "30,14$" });
GroupItems.Add(item3);
}
public class GroupItem
{
public string Title { get; set; }
public List<Entry> Entries { get; set; } = new List<Entry>();
}
public class Entry
{
public string Time { get; set; }
public string Name { get; set; }
public string Amount { get; set; }
}
這為您提供了動態數量的行的可能性,并且每個組都是可選的。
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424236.html
標籤:xml 列表显示 xamarin xamarin.forms
