diff --git a/CHANGELOG.md b/CHANGELOG.md
index acd6850..5b1ce01 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,7 @@ Fixes
- Fixed radio buttons not unchecking all other radio buttons with the same root element
- Fixed elements not being deselected when removed through RemoveChild
- Fixed elements sometimes staying hidden when they shouldn't in scrolling panels
+- Fixed elements' OnDeselected events not being raised when CanBeSelected is set to false while selected
Removals
- Marked old Draw and DrawTransformed overloads as obsolete in favor of SpriteBatchContext ones
diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs
index 803a622..0d3fc54 100644
--- a/MLEM.Ui/Elements/Element.cs
+++ b/MLEM.Ui/Elements/Element.cs
@@ -193,7 +193,14 @@ namespace MLEM.Ui.Elements {
/// Set this field to false to disallow the element from being selected.
/// An unselectable element is skipped by automatic navigation and its callback will never be called.
///
- public virtual bool CanBeSelected { get; set; } = true;
+ public virtual bool CanBeSelected {
+ get => this.canBeSelected;
+ set {
+ this.canBeSelected = value;
+ if (!this.canBeSelected && this.Root?.SelectedElement == this)
+ this.Root.SelectElement(null);
+ }
+ }
///
/// Set this field to false to disallow the element from reacting to being moused over.
///
@@ -429,6 +436,7 @@ namespace MLEM.Ui.Elements {
private int priority;
private StyleProp style;
private StyleProp childPadding;
+ private bool canBeSelected = true;
///
/// Creates a new element with the given anchor and size and sets up some default event reactions.
diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs
index ad05fae..81be1bb 100644
--- a/MLEM.Ui/UiControls.cs
+++ b/MLEM.Ui/UiControls.cs
@@ -355,8 +355,6 @@ namespace MLEM.Ui {
if (root == null)
return null;
this.selectedElements.TryGetValue(root.Name, out var element);
- if (element != null && !element.CanBeSelected)
- return null;
return element;
}