#c# #list #xamarin.forms #model
#c# #Список #xamarin.forms #Модель
Вопрос:
У меня есть пример одной модели
public class Phone
{
public string BrandName {get; set; }
public string Name {get; set; }
public decimal Price {get; set; }
}
Возможно ли преобразовать эту модель в качестве примера ниже?:
public class PhoneGroup : List<Phone>
{
public string BrandName { get; private set; }
public PhoneGroup(string brandName)
: base()
{
BrandName = brandName;
}
public PhoneGroup(string brandName, IEnumerable<Phone> source)
: base(source)
{
BrandName = brandName;
}
}
Группировка с использованием свойства brandName
Ответ №1:
Вы могли бы использовать сгруппированный ListView.
Модель:
public class Answer
{
public string Ds_DescricaoPergunta { get; set; }
}
public class AnswerList : List<Answer>
{
public string Questions { get; set; }
public List<Answer> Answers => this;
}
Xaml:
<ListView IsGroupingEnabled="true" ItemsSource="{Binding ListOfAnswers}">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Questions}" BackgroundColor="Purple"/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Ds_DescricaoPergunta}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Код, лежащий в основе:
public partial class MainPage : ContentPage
{
private List<AnswerList> _listOfAnswers;
public List<AnswerList> ListOfAnswers { get { return _listOfAnswers; } set { _listOfAnswers = value; base.OnPropertyChanged(); } }
public MainPage()
{
InitializeComponent();
var Q1List = new AnswerList()
{
new Answer(){ Ds_DescricaoPergunta="Q1_Answer1"},
new Answer(){ Ds_DescricaoPergunta="Q1_Answer2"},
new Answer(){ Ds_DescricaoPergunta="Q1_Answer3"},
};
Q1List.Questions = "Q1";
var Q2List = new AnswerList()
{
new Answer(){ Ds_DescricaoPergunta="Q2_Answer1"},
new Answer(){ Ds_DescricaoPergunta="Q2_Answer2"},
new Answer(){ Ds_DescricaoPergunta="Q2_Answer3"},
};
Q2List.Questions = "Q2";
var Q3List = new AnswerList()
{
new Answer(){ Ds_DescricaoPergunta="Q3_Answer1"},
new Answer(){ Ds_DescricaoPergunta="Q3_Answer2"},
};
Q3List.Questions = "Q3";
var Q4List = new AnswerList()
{
new Answer(){ Ds_DescricaoPergunta="Q4_Answer1"},
new Answer(){ Ds_DescricaoPergunta="Q4_Answer2"},
new Answer(){ Ds_DescricaoPergunta="Q4_Answer3"},
new Answer(){ Ds_DescricaoPergunta="Q4_Answer4"},
};
Q1List.Questions = "Q4";
var list = new List<AnswerList> { Q1List, Q2List, Q3List, Q4List };
ListOfAnswers = list;
this.BindingContext = this;
}
}