我尝试使用CollectionView
动态创建的布局来显示一个类的一系列属性,所有属性都基于一个列表,我想让它成为Combobox
的属性之一。我怎么知道ComboBox
需要引用哪个对象?
下面是我的CollectionView
:
<CollectionView x:Name="taskList">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="models:Task">
<VerticalStackLayout Margin="15">
<Entry Text="{Binding name}" IsReadOnly="True" />
<Entry Text="{Binding departmentsString}" IsReadOnly="True"/>
<HorizontalStackLayout>
<inputs:SfComboBox BackgroundColor="Black" TextColor="Green" DropDownIconColor="Green"/>
<Entry Text="{Binding deadline}" IsReadOnly="True" />
<Entry Text="{Binding author.fullName}" IsReadOnly="True"/>
</HorizontalStackLayout>
<Entry Text="{Binding description}" IsReadOnly="True" />
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
它的ItemsSource声明如下:
taskList.ItemsSource = tasks;
任务是:
ObservableCollection<Classes.Task> tasks { get; set; }
下面是Task类:
public class Task
{
public Task(string name, List<string> departments, Status status, DateOnly deadline, Employee author, string description)
{
this.name = name;
this.departments = departments;
this.status = status;
this.deadline = deadline;
this.author = author;
this.description = description;
}
public string name { get; private set; }
public List<string> departments { get; private set; } = new List<string>();
public string departmentsString
{
get
{
string _ = "";
foreach (var department in departments)
{
_ += department + (department == departments.Last() ? "": ", ");
}
return _;
}
}
public Status status { get; private set; }
public DateOnly deadline { get; private set; }
public Employee? author { get; set; }
public string description { get; private set; }
public List<Employee> employees { get; private set; } = new List<Employee>();
public void AddEmployee(Employee employee)
{
if (departments.Contains(employee.department))
{
employees.Add(employee);
}
}
}
如何使它能够根据所更改的ComboBox
来确定类Task
的示例?
下面是用户界面的外观:
1条答案
按热度按时间ou6hu8tu1#
正在尝试将组合框绑定到Status属性
您可以尝试为
SfComboBox
的属性ItemsSource
设置数据列表,并将字段绑定到SfComboBox
的属性SelectedItem
。假设你想把
departments
绑定到SfComboBox
的ItemsSource
,那么我们需要添加一个字段(例如SelectedItem
)来绑定到SfComboBox
的属性SelectedItem
:然后我们需要为
MyTask.cs
实现接口INotifyPropertyChanged
,并添加字段SelectedItem
(为了避免与项目中的Task
类冲突,我将其命名为MyTask
)。MyTask
的完整代码那么我们可以这样使用:
然后,如果我们更改
SfComboBox
的选项,SelectedItem
的值也将自动更新。