簡體   English   中英

在 WPF 中選擇時如何在列表框中顯示列表成員

[英]How to show members of a list in listbox when selected in WPF

我對編程還很陌生,所以請原諒我的無知。

我的 WPF 應用程序中有 2 個類和 2 個列表框,用於添加患者和病房。 我的一個班級(病房)包括其他 class(患者)的列表作為屬性。

我已經想出如何將患者作為列表添加到病房。 現在我正在努力的部分:我需要能夠根據在該病房列表中選擇的病房將患者添加到特定病房,並在單獨的列表框中顯示該病房的患者。

我創建了一個選擇更改事件,以便在選擇病房時顯示患者。 我只是不知道如何將第二個列表框的來源更改為病房的患者。

我在下面附上了應用程序的代碼和屏幕截圖。 非常感謝任何幫助讓它工作的幫助。 謝謝。

截圖: [1]: https://i.stack.imgur.com/omILR.png

public partial class MainWindow : Window
{
    public ObservableCollection<Ward> ward = new ObservableCollection<Ward>();
    public ObservableCollection<Patient> patient = new ObservableCollection<Patient>();


    public MainWindow()
    {
        InitializeComponent();
    }

    private void sliderCapacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        tblkCapacity.Text = string.Format("{0:F0}", sliderCapacity.Value);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        btnAddWard.IsEnabled = false;
        btnAddPatient.IsEnabled = false;
        Ward w1 = new Ward("Marx Brothers", 3);
        Ward w2 = new Ward("Addams Family", 7);

        ward.Add(w1);
        ward.Add(w2);

        Patient p1 = new Patient("Chico", 67, BloodType.A);
        Patient p2 = new Patient("Graucho", 57, BloodType.AB);
        Patient p3 = new Patient("Harpo", 46, BloodType.B);

        w1.Patients.Add(p1);
        w1.Patients.Add(p2);
        w1.Patients.Add(p3);

        //display on screen
        lbxWards.ItemsSource = ward;

        //begin ward list count at 2
        Ward.NumberOfWards++;
        Ward.NumberOfWards++;
        int totalWards = Ward.NumberOfWards;
        tblkNumberOfWards.Text = string.Format("({0})", totalWards);
    }

    private void lbxWards_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //need to make patients listbox (lbxPatients) populate with patients of selected ward when hovered
 
    }

患者 Class:

public class Patient
    {
        public string PatientName { get; set; }

        public DateTime DOB { get; set; }

        public static int Age { get; set; }

        public BloodType BloodType { get; set; }

        public static int NumberOfPatients { get; set; }


        public Patient(string patientName, DateTime dob, int age, BloodType bloodType)
        {
            PatientName = patientName;
            DOB = dob;
            Age = age;
            BloodType = bloodType;
        }

        public Patient(string patientName, int age, BloodType bloodType)
        {
            PatientName = patientName;
            Age = age;
            BloodType = bloodType;
        }

        public Patient()
        {
            
        }

        public override string ToString()
        {
            return string.Format("{0} ({1}) Type: {2}", PatientName, Age, BloodType);
        }

    }

病房 Class

public class Ward
    {
        public List<Patient> Patients { get; set; }
        public string WardName { get; set; }
        public double Capacity { get; set; }
        public static int NumberOfWards { get; set; }

        public Ward(string wardName, double capacity)
        {
            WardName = wardName;
            Capacity = capacity;
            Patients = new List<Patient>();
        }

        public Ward()
        {
            Patients = new List<Patient>();
        }

        public override string ToString()
        {
            return string.Format("{0} Ward \t (Limit: {1})", WardName, Capacity);
        }

    }

使用 MVVM 而不是代碼隱藏。 給你的 main window 一個MainWindowViewModelDataContext 像這樣

// BaseViewModel class for implementing INotifyPropertyChanged.
// This is a terribly simple implementation.  There are better ones.

public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    protected RaisePropertyChanged(string name) =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name);
}

// MainWindowViewModel. An instance of this should be set, at startup as 
// the DataContext of the MainWindow

public class MainWindowViewModel : BaseViewModel
{
    public ObservableCollection<Ward> Wards { get; } = new();
    private Ward _currentWard;
    public Ward CurrentWard
    {
        get => _currentWard;
        set
        {
            if (value == _currentWard)
                return;

             // Current ward changed.  Raise the event.

            _currentWard = value;
            RaisePropertyChanged(nameof(CurrentWard));
        }
    }
}

那么MainWindow中的 XAML 可能看起來像這樣(我假設一個包含行和列的網格。我還假設這個 XAML 的當前默認DataContextMainWindowViewModel的一個實例

<!-- Main list of wards on the left -->
<ListBox x:Name="WardListBox" 
    ItemsSource="{Binding Wards}"
    SelectedItem="{Binding CurrentWard}"
    Grid.Row="0" Grid.Column="0"
    />

<!-- List of patients in the currently selected ward on the right -->

<ListBox x:Name="PatientListBox"
    ItemsSource="{Binding CurrentWard.Patients}"
    Grid.Row="0" Grid.Column="1"
    />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM