1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace GUI.GUIUtils
- {
- public class CheckGroup<UICheckableType> : LinkedList<WeakReference<UICheckableType>> where UICheckableType : Control, ICheckableUI
- {
- public void AddSafe(params UICheckableType[] checkable)
- {
- foreach (var it in checkable)
- {
- it.ResetClick();
- it.Click += (sender, _) => CheckedObject = (UICheckableType)sender;
- _ = AddFirst(new WeakReference<UICheckableType>(it));
- }
- }
- WeakReference<UICheckableType> checkedObjectReference;
- public UICheckableType CheckedObject
- {
- get
- {
- if (checkedObjectReference == null) return null;
- if (checkedObjectReference.TryGetTarget(out UICheckableType target)) return target;
- else checkedObjectReference = null;
- return null;
- }
- set
- {
- foreach (var it in this)
- if (it.TryGetTarget(out UICheckableType target))
- target.IsChecked = target == value;
- }
- }
- }
- }
|