繁体   English   中英

我怎样才能在 CollectionView 中拥有相应的 object?

[英]How can I have the respective object in a CollectionView?

我正在尝试使用使用CollectionView的动态创建的布局来显示 class 的一系列属性,所有属性都基于列表,我想使其中一个属性是Combobox 我怎么知道 object 和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; }

这是任务 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);
        }
    }
}

我该怎么做才能根据更改的ComboBox确定 class Task的实例?

这是用户界面的样子:

尝试将 combobox 绑定到 Status 属性

您可以尝试为SfComboBox的属性ItemsSource设置数据列表,并将字段绑定到SfComboBox的属性SelectedItem

假设您将departments绑定到SfComboBoxItemsSource ,那么我们需要添加一个字段(例如SelectedItem )绑定到SfComboBox的属性SelectedItem

然后我们需要为MyTask.cs实现接口INotifyPropertyChanged并添加字段SelectedItem 。(为了防止与我项目中的Task class 冲突,我将其命名为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的值也会自动更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM