99 lines
3.3 KiB
C#
99 lines
3.3 KiB
C#
#nullable enable
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace UVC.UIToolkit
|
|
{
|
|
/// <summary>
|
|
/// 드롭다운 목록 속성 데이터 클래스입니다.
|
|
/// UI는 UTKDropdownPropertyItemView에서 담당합니다.
|
|
/// </summary>
|
|
public class UTKDropdownPropertyItem : UTKPropertyItemBase<string>
|
|
{
|
|
#region Fields
|
|
private List<string> _choices;
|
|
#endregion
|
|
|
|
#region Properties
|
|
/// <summary>속성 타입</summary>
|
|
public override UTKPropertyType PropertyType => UTKPropertyType.DropdownList;
|
|
|
|
/// <summary>선택 가능한 항목 목록</summary>
|
|
public List<string> Choices
|
|
{
|
|
get => _choices;
|
|
set => _choices = value ?? new List<string>();
|
|
}
|
|
#endregion
|
|
|
|
#region Constructor
|
|
/// <summary>
|
|
/// 드롭다운 속성을 생성합니다.
|
|
/// </summary>
|
|
/// <param name="id">고유 ID</param>
|
|
/// <param name="name">표시 이름</param>
|
|
/// <param name="choices">선택 항목 목록</param>
|
|
/// <param name="initialValue">초기 선택 값</param>
|
|
/// <param name="isReadOnly">읽기 전용 여부</param>
|
|
public UTKDropdownPropertyItem(string id, string name, List<string> choices, string initialValue = "", bool isReadOnly = false)
|
|
: base(id, name, initialValue)
|
|
{
|
|
_choices = choices ?? new List<string>();
|
|
|
|
// initialValue가 choices에 없으면 첫 번째 항목 선택
|
|
if (!string.IsNullOrEmpty(initialValue) && !_choices.Contains(initialValue) && _choices.Count > 0)
|
|
{
|
|
Value = _choices[0];
|
|
}
|
|
IsReadOnly = isReadOnly;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 드롭다운 속성을 생성합니다 (인덱스 기반).
|
|
/// </summary>
|
|
/// <param name="id">고유 ID</param>
|
|
/// <param name="name">표시 이름</param>
|
|
/// <param name="choices">선택 항목 목록</param>
|
|
/// <param name="selectedIndex">초기 선택 인덱스</param>
|
|
/// <param name="isReadOnly">읽기 전용 여부</param>
|
|
public UTKDropdownPropertyItem(string id, string name, IEnumerable<string> choices, int selectedIndex = 0, bool isReadOnly = false)
|
|
: base(id, name, string.Empty)
|
|
{
|
|
_choices = choices?.ToList() ?? new List<string>();
|
|
|
|
if (_choices.Count > 0 && selectedIndex >= 0 && selectedIndex < _choices.Count)
|
|
{
|
|
Value = _choices[selectedIndex];
|
|
}
|
|
else if (_choices.Count > 0)
|
|
{
|
|
Value = _choices[0];
|
|
}
|
|
IsReadOnly = isReadOnly;
|
|
}
|
|
#endregion
|
|
|
|
#region Public Methods
|
|
/// <summary>선택 항목 추가</summary>
|
|
public void AddChoice(string choice)
|
|
{
|
|
if (!_choices.Contains(choice))
|
|
{
|
|
_choices.Add(choice);
|
|
}
|
|
}
|
|
|
|
/// <summary>선택 항목 제거</summary>
|
|
public bool RemoveChoice(string choice)
|
|
{
|
|
bool removed = _choices.Remove(choice);
|
|
if (removed && Value == choice && _choices.Count > 0)
|
|
{
|
|
Value = _choices[0];
|
|
}
|
|
return removed;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|