Uras官网demo只展示string数组如何使用,如下:
前端
axaml<u:MultiComboBox Watermark="Please Select" Width="300" MaxHeight="200" SelectedItems="{Binding SelectedItems}" ItemsSource="{Binding Items}" > </u:MultiComboBox>
后端
C#using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace Ursa.Demo.ViewModels;
public class MultiComboBoxDemoViewModel: ObservableObject
{
public ObservableCollection<string> Items { get; set; }
public ObservableCollection<string> SelectedItems { get; set; }
public ICommand SelectAllCommand => new RelayCommand(() =>
{
SelectedItems.Clear();
foreach (var item in Items)
{
SelectedItems.Add(item);
}
});
public ICommand ClearAllCommand => new RelayCommand(() =>
{
SelectedItems.Clear();
});
public ICommand InvertSelectionCommand => new RelayCommand(() =>
{
var selectedItems = new List<string>(SelectedItems);
SelectedItems.Clear();
foreach (var item in Items)
{
if (!selectedItems.Contains(item))
{
SelectedItems.Add(item);
}
}
});
public MultiComboBoxDemoViewModel()
{
Items = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
"Item 6",
"Item 7",
"Item 8",
"Illinois",
"Indiana",
"Iowa",
"Kansas",
"Kentucky",
"Louisiana",
"Maine",
"Maryland",
"Massachusetts",
"Michigan",
"Minnesota",
"Mississippi",
"Missouri",
"Montana",
"Nebraska",
"Nevada",
"New Hampshire",
"New Jersey",
"New Mexico",
"New York",
"North Carolina",
"North Dakota",
"Ohio",
"Oklahoma",
"Oregon",
"Pennsylvania",
"Rhode Island",
};
SelectedItems = new ObservableCollection<string>();
}
}
前端
axaml<u:MultiComboBox Width="200" MaxHeight="200" SelectedItems="{Binding SelectedGroups}" ItemsSource="{Binding AlarmGroups}" > <u:MultiComboBox.SelectedItemTemplate> <DataTemplate x:DataType="vm:AlarmGroupViewModel"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}"/> </StackPanel> </DataTemplate> </u:MultiComboBox.SelectedItemTemplate> <u:MultiComboBox.ItemTemplate> <DataTemplate x:DataType="vm:AlarmGroupViewModel"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}"/> </StackPanel> </DataTemplate> </u:MultiComboBox.ItemTemplate> </u:MultiComboBox>
后端
C#using Avalonia.Collections;
using Avalonia.Controls.Notifications;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Cowain.Base.Helpers;
using Cowain.Base.ViewModels;
using Ke.Bee.Localization.Localizer.Abstractions;
using Plugin.Cowain.Driver.Abstractions;
using Plugin.Cowain.Driver.IServices;
using Plugin.Cowain.Driver.Services;
using SixLabors.Fonts.Tables.AdvancedTypographic;
using System.Collections.ObjectModel;
namespace Plugin.Cowain.Driver.ViewModels;
public partial class AlarmHistoryViewModel : PageViewModelBase
{
private readonly ILocalizer _l;
private readonly IAlarmService _alarmService;
private readonly IAlarmGroupService _alarmGroupService;
private readonly IAlarmLevelService _levelService;
public AlarmHistoryViewModel(ILocalizer localizer, IAlarmService alarmService, IAlarmGroupService alarmGroupService, IAlarmLevelService alarmLevelService)
{
PageSize = 40;
_l = localizer;
_alarmService = alarmService;
_alarmGroupService = alarmGroupService;
_levelService = alarmLevelService;
}
[ObservableProperty]
private int _totals;
[ObservableProperty]
private int _pageSize;
[ObservableProperty]
private int _pageIndex;
[ObservableProperty]
private ObservableCollection<AlarmViewModel>? _alarms = new();
[ObservableProperty] private DateTime? _startDate;
[ObservableProperty] private DateTime? _endDate;
[ObservableProperty] private IList<AlarmGroupViewModel>? _selectedGroups = new AvaloniaList<AlarmGroupViewModel>();
[ObservableProperty] private IList<AlarmLevelViewModel>? _selectedLevels = new AvaloniaList<AlarmLevelViewModel>();
[ObservableProperty]
private ObservableCollection<AlarmLevelViewModel>? _alarmLevels;
[ObservableProperty]
private ObservableCollection<AlarmGroupViewModel>? _alarmGroups;
[RelayCommand]
private async Task LoadedAsync()
{
//获取所有设备列表
var alarmLevels = await _levelService.GetAllAsync();
AlarmLevels = new ObservableCollection<AlarmLevelViewModel>(alarmLevels);
var alarmGroups = await _alarmGroupService.GetAllAsync();
AlarmGroups = new ObservableCollection<AlarmGroupViewModel>(alarmGroups);
}
[RelayCommand]
private async Task RefreshAsync()
{
if (StartDate == null)
{
NotificationHelper.ShowNormal(NotificationType.Warning, _l["AlarmHistory.Warning.StartDateIsNull"]);
return;
}
if (EndDate == null)
{
NotificationHelper.ShowNormal(NotificationType.Warning, _l["AlarmHistory.Warning.EndDateIsNull"]);
return;
}
List<int>? groups = SelectedGroups?.Select(g => g.Id).ToList();
List<int>? levels = SelectedLevels?.Select(g => g.Id).ToList();
var (data, count) = await _alarmService.GetAlarmAsync(PageIndex, PageSize, StartDate, EndDate, groups, levels);
Alarms?.Clear();
Totals = count;
if (count > 0)
{
foreach (var item in data)
{
// 设置报警组名称和等级名称
item.GroupName = AlarmGroups?.FirstOrDefault(g => g.Id == item.Group)?.Name ?? string.Empty;
item.LevelName = AlarmLevels?.FirstOrDefault(g => g.Id == item.Level)?.Name ?? string.Empty;
Alarms?.Add(item);
}
}
}
}
选中项一定要是IList接口
C# [ObservableProperty] private IList<AlarmGroupViewModel>? _selectedGroups = new AvaloniaList<AlarmGroupViewModel>();
本文作者:zhusenlin
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 版权所有:zhusenlin 许可协议。转载请注明出处!