簡體   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