43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Windows;
|
||
using YuncangTrace.LocalService.Services;
|
||
|
||
namespace YuncangTrace.LocalService;
|
||
|
||
/// <summary>切换主体的小弹窗(列表来自 `GET /api/web/subjects`)</summary>
|
||
public partial class SubjectPickerWindow : Window
|
||
{
|
||
public SubjectPickerWindow(IReadOnlyList<ApiSubject> subjects, int currentId)
|
||
{
|
||
InitializeComponent();
|
||
|
||
SubjectList.ItemsSource = subjects;
|
||
var current = subjects.FirstOrDefault(item => item.Id == currentId);
|
||
SubjectList.SelectedItem = current ?? subjects.FirstOrDefault(item => !item.Expired) ?? subjects.FirstOrDefault();
|
||
}
|
||
|
||
/// <summary>用户选中的主体(确定后有值)</summary>
|
||
public ApiSubject? SelectedSubject { get; private set; }
|
||
|
||
private void Ok_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
if (SubjectList.SelectedItem is not ApiSubject subject)
|
||
{
|
||
MessageBox.Show(this, "请先选择一个主体", "切换主体", MessageBoxButton.OK, MessageBoxImage.Information);
|
||
return;
|
||
}
|
||
|
||
SelectedSubject = subject;
|
||
DialogResult = true;
|
||
}
|
||
|
||
private void Cancel_Click(object sender, RoutedEventArgs e) => DialogResult = false;
|
||
|
||
private void SubjectList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
|
||
{
|
||
if (SubjectList.SelectedItem is ApiSubject) Ok_Click(sender, e);
|
||
}
|
||
}
|