XAML 如何在CollectionView中拥有相应的对象?

628mspwn  于 2022-12-31  发布在  其他
关注(0)|答案(1)|浏览(135)

我尝试使用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的示例?
下面是用户界面的外观:

ou6hu8tu

ou6hu8tu1#

正在尝试将组合框绑定到Status属性
您可以尝试为SfComboBox的属性ItemsSource设置数据列表,并将字段绑定到SfComboBox的属性SelectedItem
假设你想把departments绑定到SfComboBoxItemsSource,那么我们需要添加一个字段(例如SelectedItem)来绑定到SfComboBox的属性SelectedItem
然后我们需要为MyTask.cs实现接口INotifyPropertyChanged,并添加字段SelectedItem(为了避免与项目中的Task类冲突,我将其命名为MyTask)。

//add SelectedItem here

        private string _selectedItem;
        public string SelectedItem
        {
            get => _selectedItem;
            set => SetProperty(ref _selectedItem, value);
        }

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);
            }
        }
    }

那么我们可以这样使用:

<editors:SfComboBox BackgroundColor="Black" TextColor="Green" 
                         DropDownIconColor="Green" 
                         WidthRequest="250"
                         ItemsSource="{Binding departments}"    
                         SelectedItem="{Binding SelectedItem}"
                                            />
    • 注:**

然后,如果我们更改SfComboBox的选项,SelectedItem的值也将自动更新。

相关问题