所以我有這個listview,我想使它按字母順序排序。做一個人知道你是否可以做到這一點?如果你知道如何做到這一點,那麼給我看看代碼。尋求幫助!如何按字母順序排序我的列表視圖?
這是我Xaml.cs代碼:
namespace App2
{
public partial class MainPage : ContentPage
{
List<Kontakter> kontakter = new List<Kontakter>
{
new Kontakter
{
Fuldenavn = "Anja Birkelund (ANBI)",
Tlfnr = 24212504
},
new Kontakter
{
Fuldenavn = "Morten Jensen (MOJ)",
Tlfnr = 24838149
},
new Kontakter
{
Fuldenavn = "Thomas Duvall Pedersen (TPD)",
Tlfnr = 61706767
},
new Kontakter
{
Fuldenavn = "Svend-Erik Dejbjerg (SD)",
Tlfnr = 20116644
}
};
public MainPage()
{
InitializeComponent();
NameslistView.ItemsSource = kontakter;
}
private void MainSearchBar_SearchButtonPressed(object sender, EventArgs e)
{
var keyword = MainSearchBar.Text;
NameslistView.ItemsSource = kontakter.Where(obj => (obj.Fuldenavn.Contains(keyword) || obj.Tlfnr.ToString().Contains(keyword)));
}
}
}
這是我Xmal位代碼:
<ListView x:Name="NameslistView" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Fuldenavn}" />
<Label Text="{Binding Tlfnr}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
嘿克里斯蒂安F,看看這個https://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object – Danieboy
可能的重複[如何通過在對象中的屬性排序列表(https://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object) –