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
. How do I know what object the ComboBox
needs to refer to?
Here is my 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:
taskList.ItemsSource = tasks;
tasks being:
ObservableCollection<Classes.Task> tasks { get; set; }
Here is the Task 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?
Here is what the UI looks like:
Trying to bind the combobox to the Status property
You can try to set a data list for property ItemsSource
of SfComboBox
and bind a field to property SelectedItem
of SfComboBox
.
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
:
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
)
//add SelectedItem here
private string _selectedItem;
public string SelectedItem
{
get => _selectedItem;
set => SetProperty(ref _selectedItem, value);
}
The whole code of 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.