[英]How can I have the respective object in a CollectionView?
I'm trying to use a dynamically created layout using CollectionView
to show a series of properties of a class, all based on a list and I want to make it so one of the properties is a Combobox
.我正在尝试使用使用
CollectionView
的动态创建的布局来显示 class 的一系列属性,所有属性都基于列表,我想使其中一个属性是Combobox
。 How do I know what object the ComboBox
needs to refer to?我怎么知道 object 和
ComboBox
需要引用什么?
Here is my CollectionView
:这是我的
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>
It has its ItemsSource declared like this:它的 ItemsSource 声明如下:
taskList.ItemsSource = tasks;
tasks being:任务是:
ObservableCollection<Classes.Task> tasks { get; set; }
Here is the Task class:这是任务 class:
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);
}
}
}
How do I make it so I can determine the instance of the class Task
depending on which ComboBox
I change?我该怎么做才能根据更改的
ComboBox
确定 class Task
的实例?
Here is what the UI looks like:这是用户界面的样子:
Trying to bind the combobox to the Status property
尝试将 combobox 绑定到 Status 属性
You can try to set a data list for property ItemsSource
of SfComboBox
and bind a field to property SelectedItem
of SfComboBox
.您可以尝试为
SfComboBox
的属性ItemsSource
设置数据列表,并将字段绑定到SfComboBox
的属性SelectedItem
。
Suppose you would bind departments
to the ItemsSource
of SfComboBox
, then we need to add a field (eg SelectedItem
) to bind to property SelectedItem
of SfComboBox
:假设您将
departments
绑定到SfComboBox
的ItemsSource
,那么我们需要添加一个字段(例如SelectedItem
)绑定到SfComboBox
的属性SelectedItem
:
Then we need to implement interface INotifyPropertyChanged
for MyTask.cs
and add field SelectedItem
.(To prevent conflicts with the Task
class in my project, I named it MyTask
)然后我们需要为
MyTask.cs
实现接口INotifyPropertyChanged
并添加字段SelectedItem
。(为了防止与我项目中的Task
class 冲突,我将其命名为MyTask
)
//add SelectedItem here
private string _selectedItem;
public string SelectedItem
{
get => _selectedItem;
set => SetProperty(ref _selectedItem, value);
}
The whole code of MyTask
MyTask
的全部代码
public class MyTask: INotifyPropertyChanged
{
public MyTask(string name, List<string> departments, int status, DateTime deadline, Employee author, string description)
{
this.name = name;
this.departments = departments;
this.status = status;
this.deadline = deadline;
this.author = author;
this.description = description;
}
//add SelectedItem here
private string _selectedItem;
public string SelectedItem
{
get => _selectedItem;
set => SetProperty(ref _selectedItem, value);
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
public string name { get; 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 int status { get; private set; }
public DateTime 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);
}
}
}
Then we can use like this:然后我们可以这样使用:
<editors:SfComboBox BackgroundColor="Black" TextColor="Green"
DropDownIconColor="Green"
WidthRequest="250"
ItemsSource="{Binding departments}"
SelectedItem="{Binding SelectedItem}"
/>
Note:笔记:
Then if we change the option of SfComboBox
, the value of SelectedItem
will also update automatically.然后如果我们更改
SfComboBox
的选项, SelectedItem
的值也会自动更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.