简体   繁体   English

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

[英]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绑定到SfComboBoxItemsSource ,那么我们需要添加一个字段(例如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.

相关问题 如何获取XAML中定义的CollectionView - How can I get the CollectionView that is defined in XAML 如何以编程方式获取类依赖项及其各自的文件位置? - How can I get class dependencies programmatically and their respective file locations? 如何替换可枚举集合的默认CollectionView? - How can I replace the default CollectionView of an enumerable collection? 如何在多个条件下使用 CollectionView 在网格中进行搜索? - How can I use search in the grid using CollectionView with multiple conditions? 如何验证文本框中的邮政编码,并将相应的州/市输出到其各自的标签 - How do I validate a zipcode in a textbox and have the corresponding state/city output to their respective labels 如果我有2个对象对象及其类型,我该如何获取它们的值? - If I have 2 object objects and their Type how can I get their value? 我怎么知道表单中的点是否有对象? - How can I know point in form have object or not? 在 C# 中创建对象时如何使用 if 语句? - How can i have an if statement when creating an object in c#? 我可以绑定到CollectionView的第一项吗? - Can I bind to the first item of a CollectionView? 轻按其各自的父级(列表视图的项目)后,如何更改标签的文本颜色? - How can I change the text color of a label after its respective parent(item of a listview) is tapped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM